getting values from text file twice

This is a discussion on getting values from text file twice within the C Programming forums, part of the General Programming Boards category; Hey there, I'm writing a program which is reading in values from a text file in two different parts. For ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    18

    Red face getting values from text file twice

    Hey there,

    I'm writing a program which is reading in values from a text file in two different parts.

    For part 1, I have used scanf to read in values up to line 3.

    For part two, I need to use all lines of the text file (5 lines) to make a calculation.

    How do I go about doing this?

    I've tried doing a second scanf but that isn't quite working.

    Thanks

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hi Tigertan,please post the code you have so far in order to see your attempt and make some comments

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,500
    Can you show your code so far?

    [edit]
    I was too slow and there are
    no prizes for second place :P
    [/edit]

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    18
    So I've got this:


    Code:
    while((scanf("%d,%d,%d", &a, &b, &c) == 3) && (i<3)){        i++;            
            printf("%d  %d  %d\n", a, b, c);
            }
    And now I want to look at the whole text file and print out the following

    Code:
    if (a > 4){
                number = number + 1;
                
            }
    printf("Number of lines where a is greater than 4 = %d", number);

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    scanf is used to request input from the user...for handling a file you should use fopen,fread,fgets,fclose...It is good practice to post all the code that must be shown to other people

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Also i suggest you to look at this example (this was posted by Adak(with some modifications),hope no copyright violations are provoked )

    /******************************ADAK's post********************************************** ********************/
    There are two common ways to read in all the data from a file:

    1) Use fgets(), in a while loop. When NULL is returned, the loop should end.

    2) Use fscanf(), in a while loop. When the count of objects stored is less than it should be, the loop should end.

    Here's a simple example program:

    Code:
    #include <stdio.h>
    #include <time.h>
     
    int main(void) {
       int i;
       char a[7][128];
       FILE *fp=fopen("Edelweiss.txt", "r");/*the name of the txt*/
       if(!fp)
    {printf("\nError: unable to open the file.\n"); return 1;}
        
     
       i=0;
     
       while((fgets(a[i],sizeof(a[i]), fp)) != NULL) {
          fputs(a[i++],stdout);
          //getchar();
       }
    /*
       //shows the use of fscanf() reading a line until it reaches a
       //newline, and reading all the data in a file, while it can store at
       //least one object.
       while((fscanf(fp, " %[^\n]s",a[i])) > 0) {
          printf("%s\n",a[i++]);
          //getchar();
       }
    */
       fclose(fp);
      
       return 0;
    }
    /*
    In a file named "Edelweiss.txt, put this into the programs directory before you run it:
          "Edelweiss, edelweiss, every morning you greet me.
          Small and white, clean and bright, you look happy to meet me.
          Blossom of snow may you bloom and grow, bloom and grow forever.
          Edelweiss, edelweiss, bless my homeland forever.
          Small and white, clean and bright, you look happy to meet me
          Blossom of snow, may you bloom and grow, bloom and grow forever.
          Edelweiss, edelweiss, bless my homeland forever."
    */
    If you use fgets(), you have the data in a string. You still need to use sscanf() to store the numbers out of the string, into your array. If the data is STRICTLY formatted - not user entered (which will always have errors), then fscanf() is perfectly fine to store the data into the array.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    << What, no royalties?? >> <grin, grin>

    *Somebody* has been digging around in here.

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    18
    I've been told I must use the scanf syntax for the initial part before printing the first 3 lines.

    Am I able to build on this in order to work out part two (using the whole text file)?
    Are all the lines "stored" in the system after the first scanf? Do I even NEED to be scanning through again>

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Tigertan View Post
    Are all the lines "stored" in the system after the first scanf? Do I even NEED to be scanning through again>
    scanf() just reads as much from stdin as it can according to the format string you provide. If your format string is "%d,%d,%d" it tries to read 3 decimal numbers separated by commas, and stops at the first character which isn't a digit. The rest (if there is one) stays in the input stream.

    You probably don't need to scan through it a second time (which is also not possible if you read from stdin). Just read the first three digits, process them (printing, calculating, ...) and then read the next three until you reach the end.

    If you want more help you have to show your current code.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading values from text file
    By a.mlw.walker in forum C Programming
    Replies: 5
    Last Post: 01-14-2012, 06:02 PM
  2. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 07:15 PM
  3. Replies: 6
    Last Post: 11-11-2006, 02:10 PM
  4. Finding Values within a text file
    By saeed01 in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2004, 05:13 PM
  5. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 03:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21