Hi! I'm relatively new at programming so I'm having a bit of a problem.
My first question is, how do I print out long strings with spaces from a text file? I tried printing it out the normal way but only the first word gets printed out. My professor required us to print out bible passages from a text file.
For my second question, I have this code:
It's supposed to print out the scores stored in Score.txt. But say, I have only one score recorded. It prints out 29 junk data for the rest. I'm planning on replacing 30 with n so that it will print only the correct number of scores. But my problem is, how can I keep track of the number of scores inside the text file? I need to retain this even when restarting the program.Code:fp=fopen("Score.txt", "r"); for(i=0;i<30;i++){ fscanf(fp,"%s\n%s\n\n",scorename[i],scorescore[i]); printf("%s\n%s\n\n",scorename[i],scorescore[i]); }
I think you can replace the for with something like:
Don't know if that is what you wantCode:int i = 0; while(fscanf(fp,"%s\n%s\n\n",scorename[i],scorescore[i]) != EOF){ printf("%s\n%s\n\n",scorename[i],scorescore[i]); i++; }
Try to be clear on the fscanf function and it's return values and you may not need to know how many items are in the file. If you know how the data is structured it should not be that hard.
fscanf - C++ Reference
For example. if it looks like this
johnDoe 45
janeDoe 153
catDoe 5
You can use the return values from fscanf to know when to stop reading or if the read was valid. If the second value you read is a int. You should probably try %d and not %s.
I dont think you can put \n in a fscanf or its variations. That should probably be removed.
You can also have a read on EOF.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks