Thread: Reading & Writing Integers from a txt file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    19

    Angry Reading & Writing Integers from a txt file

    I'm trying to read the integers out of a text file called lane9.txt separated by a space which looks like this (I'm printing them first to see if the numbers are correct):

    8 split 1 9 1 spare 7 2 10 strike 8 2 spare 6 split 2 7 3 spare 10 strike 10 strike 10 turkey 8 1

    I just need the numbers and if it's a 10 that has to be read together as one integer. I'm using the fscanf function, but that's only returning 8. I think maybe the getc function would be better, but I can't get it to work. Help would be greatly appreciated.

    Here's my code so far:
    Code:
    //preprocessor
    #include <stdio.h>
    #include <stdlib.h>
    //prototypes (function announcement)
    
    //function main
    int main ()
    
     {
       // declarations and initialzations
            FILE * frData;
            int frameScore;
    
        //prompts and scans
            frData=fopen("lane9.txt", "r");
            if (!frData)
            {
                printf("could not open file\a\n");
                exit (101);
            }
    
    
        // calculation
          while ((fscanf(frData, "%d", &frameScore))==1)
            printf("%d", frameScore);
        // output
    
    
    return 0;
    }
    // more functions

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    [code=c]
    while ((fscanf(frData, "%d", &frameScore))==1)
    printf("%d", frameScore);
    [/code]

    The first time it'll read the 8, then it encounters the string "split" which isn't a number, and so will return 1 and you'll break out of your while loop.

    Best bet is to scan it as a string into a char buffer, see if it's the string "split", "strike" or "spare" and otherwise use atoi() or strtol() to parse it as a number.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    I see why it's breaking out of the loop now. I'm not too sure what you're talking about using a char buffer though. Is there a function I can use that will just read the numbers and whitespaces? That's what I was hoping I'd be able to do with getc, but I don't know how to set that up. I'd like to read a character and print it if it's an integer 0-9 or a space.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    19

    closing

    So I have figured out the reading and writing! However, do I need to close both files? When I run my code as shown below, the message "Error closing file comes up", but if I take out the close1 part, then I don't get that message. Should I close the file I'm reading from or is that unnecessary? If I do need to close it, where/how should I do it?

    Code:
    //preprocessor
    #include <stdio.h>
    #include <stdlib.h>
    //prototypes (function announcement)
    
    //function main
    int main ()
    
     {
       // declarations and initialzations
            FILE * frData;
            FILE*  fwClean;
            char c;
            int close1;
            int close2;
    
        //prompts and scans
            frData=fopen("lane9.txt", "r");
            if (!frData)
            {
                printf("could not open file\a\n");
                exit (101);
            }
    
            fwClean=fopen("CleanLane9.txt", "w");
    
            if (!(fwClean=fopen("CleanLane9.txt", "w")))
            {
            printf("Error opening CleanLane9.txt for writing");
            return(1);
            }
    
        // calculation
            do
            {
              c=getc(frData);
              if (isdigit(c))
                {
               printf("%c", c); // shows what values are being read and will be written.
               fputc(c,fwClean);
                }
              if (isspace(c))
                {
                printf("%c",c); //shows what values are being read and will be written.
                fputc(c,fwClean);
                }
            }
          while (c!=EOF);
    
        printf("\nAbove are the values in the new file\n");
        // closing
    
       close1=fclose(frData);
       if(close1=EOF)
       {
        printf("Error closing file\a\n");
        return 100;
       }
    
        close2=fclose(fwClean);
        if(close2==EOF)
        {
            printf("Error closing file\a\n");
            return 100;
        }
    
    return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Yes close the file.

    But why check for the close? If it doesn't close then what can you do about it anyway?

    Jim

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    Good point. I was just trying to imitate another program out of my textbook and they always test if actually closed.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
       close1=fclose(frData);
       if(close1=EOF)  /* <-- bug here ! */
       {
        printf("Error closing file\a\n");
        return 100;
       }
    
        close2=fclose(fwClean);
        if(close2==EOF)
        {
            printf("Error closing file\a\n");
            return 100;
        }
    easier is

    Code:
       if(fclose(frData))
       {
        printf("Error closing file\a\n");
        return 100;
       }
    
       if(fclose(fwClean))
       {
           printf("Error closing file\a\n");
           return 100;
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Lines from File Issue (integers and character strings)
    By kbfirebreather in forum C Programming
    Replies: 4
    Last Post: 10-17-2009, 02:02 PM
  2. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 03:28 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM