Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to use "fread","write","seek",counter to write a interactive talk in C,save in a file?

write initial question and all approriate "questions" from a state machine to screen

also write all "questions" and keyboard input "answers" to a new disk file

read the keyboard input answer from the disk file,check a state machine and responds appropriately a new question

If console type in "Exit", then stop.

Sample output on screen is same as the created disk file, looks like below:

User: “Knock Knock”

Machine: “Who is There?”

User: “Mike.”

Machine: “Mike Who?”

User: “Mike who deliever a pizza.”

Machine: “Cool.”

...

User: “Exit”

Machine: “I am out”

My code:

int main()
{ FILE *fp=fopen("test","a"); char s[256]; scanf("[^\n]",s); While (s!="Exit"){ fwrite("User:"+s,10,25,fp); responde(s); } fwrite("User:"+,10,25,fp); fwrite("Machine: i am out",10,25,fp); exit(0);  }  void respond(char* s){ if(s=="Knock Knock"){ printf("Machine:who is there"); fwrite("MAchine:who is there,10,25,fp); } if???? {printf("Machine: %s,who\n+User:",s); fwrite(""Machine: %s,who\n+USer",10,25,fp); } if (???)
Posted
Updated 13-May-11 0:52am
v2

1 solution

Some thoughts:

In order to compare the arrays you need to make a loop in order to compare char by char or use strcmp[^] and char *.

fprintf should allow you to write the strings inside the file:
MIDL
FILE *fp;
fp=fopen("c:\\test.txt", "w");
fprintf(fp, "Testing...\n");



The design should be:

- Open the file.
- To make a state machine do a loop(1) and use switch or any other similar method...
- Create a function to write strings into the file (WSTF).
- Create a function to harvest strings from the user (RSFU).
- Inside that loop(1):
-- Write the right question.
-- stringAux = RSFU();
-- if (strcmp("exit", stringAux) {break loop}
-- else WSTF(stringAux);

and you are done...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900