Thread: About text file I/O

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    About 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.
    Last edited by noobiest09; 03-20-2010 at 02:02 AM. Reason: Forgot the other bracket

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    fscanf returns a result - you should check it for success.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Quote Originally Posted by Salem View Post
    fscanf returns a result - you should check it for success.
    I'm sorry for being such a noob but how do I do that?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    You read the manual page for fscanf

    You then do
    SomeKindOfTypeName result = fscanf.....

    You then do
    if ( result == SomethingYouReadInTheManual )

    Presumably, you read something to figure out what %d etc means. Just keep reading the same thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    If you want to get the number of scores in a file try the followings.
    Code:
    #include<stdio.h>
    int main()
    {
            FILE *fp=popen("wc -l score.txt","r");
            int n;
            fscanf(fp,"%d",&n);
            printf("%d\n",n);
    }
    wc-l will return the number of lines in a file.
    popen will return the file pointer which is having the output of wc- l score.txt.
    Then get the number of lines using that file pointer.
    So finally 'n' will have the number of lines in a file.
    Then use that 'n' value in your for loop.
    Last edited by kiruthika; 03-20-2010 at 03:08 AM.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Answer for your first question :
    If you want to read a line from the file you have to use the function fgets.It will read the entire line . I thing you used fscanf there .It will read only the first word.

    Answer for the second question :
    As like salem said you have to check the return value of fscanf.If it returns EOF , you just stop reading it and come out from the loop.

    Code:
    #include <stdio.h>
    main()
    {
    FILE *fp=fopen("Score.txt", "r");
    char scorename[20];
    char scorescore[3];
    int i;
    for(i=0;i<30;i++){
    if(fscanf(fp,"%s\n%s\n\n",scorename,scorescore)==EOF)
            break;
    printf("%s\n%s\n\n",scorename,scorescore);
    }
    }

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Thanks! I'll try to do the things you guys suggested.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    Answer for your first question.

    If you used fscanf() to read content from the file, then use like the following.
    Code:
            fscanf(fp,"%[^\n]",s);

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Quote Originally Posted by karthigayan View Post
    Answer for your first question :
    If you want to read a line from the file you have to use the function fgets.It will read the entire line . I thing you used fscanf there .It will read only the first word.

    Answer for the second question :
    As like salem said you have to check the return value of fscanf.If it returns EOF , you just stop reading it and come out from the loop.

    Code:
    #include <stdio.h>
    main()
    {
    FILE *fp=fopen("Score.txt", "r");
    char scorename[20];
    char scorescore[3];
    int i;
    for(i=0;i<30;i++){
    if(fscanf(fp,"%s\n%s\n\n",scorename,scorescore)==EOF)
            break;
    printf("%s\n%s\n\n",scorename,scorescore);
    }
    }
    I tried this and it worked. Thanks a lot!

  10. #10
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    just a final note on fscanf:

    fscanf, reads a word and then another one.
    so if you have a file like:

    Message From File
    EOF
    and you use fscanf two times, you get first
    'Message' then 'From'. but if you have four
    'fscanf's then you get all the words. so fscanf
    reads a word and proceeds to the next one,
    when encountered the end of the line, it goes
    to the next one and gets the word of that line.
    hope it helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM