Closed Thread
Results 1 to 3 of 3

Thread: C Programming: Text File I/O

  1. #1
    noobiest09 is offline Newbie
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Unhappy C Programming: Text File I/O

    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:
    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]);
    }
    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    matio's Avatar
    matio is offline Learning Programmer
    Join Date
    Aug 2009
    Location
    UK
    Posts
    51
    Rep Power
    0

    Re: C Programming: Text File I/O

    I think you can replace the for with something like:
    Code:
    int i = 0;
    while(fscanf(fp,"&#37;s\n%s\n\n",scorename[i],scorescore[i]) != EOF){
        printf("%s\n%s\n\n",scorename[i],scorescore[i]);
        i++;
    }
    Don't know if that is what you want

  4. #3
    fread's Avatar
    fread is offline Programming God
    Join Date
    Nov 2008
    Location
    Caribbean
    Posts
    654
    Blog Entries
    1
    Rep Power
    16

    Re: C Programming: Text File I/O

    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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Batch file to change text in a txt file?
    By Scarness in forum General Programming
    Replies: 3
    Last Post: 05-22-2011, 06:28 AM
  2. How can I let the text inside a text file ...
    By xxxxjayxxx in forum Java Help
    Replies: 1
    Last Post: 03-13-2011, 04:31 AM
  3. converting text file to php help me
    By changoo in forum PHP Development
    Replies: 5
    Last Post: 11-22-2010, 11:57 PM
  4. How to save the text in a text file ???
    By kresh7 in forum Visual Basic Programming
    Replies: 1
    Last Post: 04-11-2010, 02:03 AM
  5. Formatting Text in a text file help?
    By Djanvk in forum C and C++
    Replies: 2
    Last Post: 08-17-2008, 09:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts