Hello,

I have a question regarding reading a txt file and then putting the info in a link list. the problem is that the file contains 3 things to read a letter, a username and a password in the below format:

S P2-output1.txt
A Hanna hdfjuju

first i must read a letter then depending on the letter i must do something.
for example if i read the letter A i must then read the next 2 words and add each to the link list. then i read the next line and so on.

here are the letters and what they do:
S: means to start a file named P2-output1.txt and if its already there to add to it
A: to add the username and password following the letter
V: to save from link list to the output file
P: to print
E: to exit


i only need to know the correct logic. what exactly should i do?
i know how to add to link list but i dont know how to read the first letter then continue reading after it and doing what the letter should do.

attached is the input file.

Thanks

Use fgetc to get the first character and then you can use the switch-case statement to work accordingly.

thank you but can you explain more how it will read the line after the fgetc
for example, if the letter was S. then what? how to read the file name and continue reading the next lines?

why your input file have no username and password in some cases.
If it is sure that your file will contain all the three data in each and every line, then you can read it like this

fscanf(fp,"%c%s%s",&c,&name,&password);

but if some of the line may not contain username and password then you better read whole line at once using

fscanf(fp,"%[^\n]s",s1);

and parse the line accordingly to get character username and password.

Your first suggestion is not workable because as previously explained the first letter can be one of several commands. But your second suggestion is the best way to write the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.