incrementing a variable while using fscanf

This is a discussion on incrementing a variable while using fscanf within the C Programming forums, part of the General Programming Boards category; i've been stuck on this for a while, and i've tried a variety of things, most of them resulting in ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    44

    incrementing a variable while using fscanf

    i've been stuck on this for a while, and i've tried a variety of things, most of them resulting in segfaults. i basically want to increment my variable "count" each time there's a new line in the file.

    please ignore my use of feof for now. i'll fix that later to make it EOF.

    Code:
    #include "student_functions.h"
    #define DIM 1000
    
    int main(int argc, char *argv[])
    {
            FILE *database;
            data student[DIM]; /*array of structs*/
            int i;
            int count = 0; /*number of students*/
    
            database = fopen(argv[1],"r");
    
            printf("Welcome to the Student Database \n");
            printf("First        Last         ID           GPA \n");
    
            while(!feof(database))
            {
                    for(i = 0; i < DIM; i++)
                    {
                            fscanf(database,"%s %s %d %f \n",student[i].firstname,
                                            student[i].lastname,&student[i].ID,&student[i].GPA);
                            count++;
                    }
                    count++;
    /*              if(fscanf(database,"%s %s %d %f \n",student[i].firstname,
                                    student[i].lastname,&student[i].ID,&student[i].GPA) == '\n')
                    {
                            count++;
                    }*/
            }
            for(i = 0; i < count; i++)
            {
                    printf("%-12s %-12s %-12d %-12.2f \n",student[i].firstname,        student[i].lastname,
                                    student[i].ID,student[i].GPA);
            }
            fclose(database);
    
            return 0;
    }
    sorry about how the orange came out in terms of indentation( the printf). it copied over weird and wouldn't let me fix it.

    "student_functions.h" is a header file that, for now, only defines my struct.
    Code:
    #include <stdio.h>
    
    typedef struct
    {
            int ID;
            float GPA;
            char firstname[12];
            char lastname[12];
    } data;
    so i know that if i have the count++ where it's green, it will give me a result of 1000. if it's where the color is red, it will be 1, and the blue is 0.

    thanks
    Last edited by synhyborex; 03-10-2011 at 05:49 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 12.2
    Editon't use foef to control loop. Read the above link.
    From your usage of fscanf, you should go read doc of the function. Esp what the function value indicates.
    Last edited by Bayint Naung; 03-10-2011 at 05:56 PM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include "student_functions.h"
    #define DIM 1000
    
    int main(int argc, char *argv[])
    {
            FILE *database;
            data student[DIM];
            int i;
            int count = 0;
    
             printf("Welcome to the Student Database \n");
            printf("First        Last         ID           GPA \n");
    
           database = fopen(argv[1],"r");
    
           while(!feof(database))
            {
                    for(i = 0; i < DIM; i++)
                    {
                            fscanf(database,"%s %s %d %f \n",student[i].firstname,
                                            student[i].lastname,&student[i].ID,&student[i].GPA);
                         count++;
                  }
            }
    
            fclose(database);
    
    
            for(i = 0; i < count; i++)
            {
               printf("%12s %12s %12d %12.2f \n",student[i].firstname, student[i].lastname,
                                                student[i].ID,student[i].GPA);        }
            return 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    thanks for fixing my fclose, tater. but if you look at the OP, where i put in the three examples of where i'd tried the count++, the place where you put it results in the value of count being 1000. i need a way to detect a '\n', at which point count increments.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by synhyborex View Post
    thanks for fixing my fclose, tater. but if you look at the OP, where i put in the three examples of where i'd tried the count++, the place where you put it results in the value of count being 1000. i need a way to detect a '\n', at which point count increments.
    The thing is you don't need to worry about line ends in a file with formatted lines, just grab the data with scanf(), up your count, and move on...

    For example:
    In a file where every line holds NAME LAST SCORE GID ... you have 4 items to read. You really don't care about anything else.

    Code:
    #include "student_functions.h"
    #define DIM 1000
    
    int main(int argc, char *argv[])
    {
            FILE *database;
            data student[DIM];
            int i = 0;
    
            printf("Welcome to the Student Database \n");
            printf("First        Last         ID           GPA \n");
    
           database = fopen(argv[1],"r");
    
           while(!feof(database))
            {
               fscanf(database,"%s %s %d %f \n",student[i].firstname,
                                            student[i].lastname,&student[i].ID,&student[i].GPA);
               i++;
             }
            fclose(database);
    
    
           return 0;
    }
    Last edited by CommonTater; 03-10-2011 at 06:10 PM.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    So what is count for???! Not number of record? What is expected value?
    Care to post your file? What's the records look like?

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    @bayint, count represents how many lines are in the file, or how many students are in the database.

    here's the file:
    Code:
    John Doe 1234 3.50
    Jane Smith 2543 3.51
    Bob Jones 1000 2.43
    that's the one i'm working with for now. there are other ones that are longer, as well. in this file, the expected value is 3. sorry for that confusion.

    @tater i understand what you're saying, but when i print the results to the screen using the last for loop i have, since count == 1000, it will print the first three lines correctly, and the other 997 are printed as gibberish, since there aren't 997 other values to take up those spots in memory. i want, in this case, to print only the first 3 lines of the file to the screen. the 1000 is just how many entries the database CAN hold.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Read using fscanf until it returns EOF or less than what you expect to return(read doc). And increment count. Then print that count record....
    Btw 1000*sizeof record may be not suitable as local variable.
    Last edited by Bayint Naung; 03-10-2011 at 06:22 PM.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    hey thanks you guys, but i figured it out. this is what i changed that while loop to:
    Code:
            for(i = 0; i < DIM; i++)
            {
                    fscanf(database,"%s %s %d %f \n",student[i].firstname,
                                    student[i].lastname,&student[i].ID,&student[i].GPA);
                    if(!feof(database))
                    {
                    count++;
                    }
            }
    oh and i initialized count to 1 instead of 0.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,797
    O_o

    Dude, why have you not read the link Bayint Naung posted? It would tell you how to do it without doubling the terminating condition.

    Soma

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    [QUOTE=synhyborex;1009623@tater i understand what you're saying, but when i print the results to the screen using the last for loop i have, since count == 1000, it will print the first three lines correctly, and the other 997 are printed as gibberish, since there aren't 997 other values to take up those spots in memory. i want, in this case, to print only the first 3 lines of the file to the screen. the 1000 is just how many entries the database CAN hold.[/QUOTE]

    That's because you are printing the whole array instead of just the used parts...

    In the example below i has your line count... so when printing only run your for() loop up to i, not the end of the array.

    Code:
    #include "student_functions.h"
    #define DIM 1000
    
    int main(int argc, char *argv[])
    {
            FILE *database;
            data student[DIM];
            int i = 0;
            int j;
    
            printf("Welcome to the Student Database \n");
            printf("First        Last         ID           GPA \n");
    
           database = fopen(argv[1],"r");
    
           while(!feof(database))
            {
               fscanf(database,"%s %s %d %f \n",student[i].firstname,
                                            student[i].lastname,&student[i].ID,&student[i].GPA);
               i++;
             }
            fclose(database);
    
           for (j=1; j < i; j++)    
              printf("%s %s %d %f \n",student[i].firstname,
                                            student[i].lastname,&student[i].ID,&student[i].GPA);
     
    
           return 0;
    }

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,797
    Code:
    for (j=1; j < i; j++)    
              printf("%s %s %d %f \n",student[i].firstname,
                                            student[i].lastname,&student[i].ID,&student[i].GPA);
    O_o

    You copied and pasted from the wrong source or something. You got the starting index wrong and screwed up the variables.

    [Edit]
    Oh, wait, you are the one ignoring me.

    And I'm still amused by it.
    [/Edit]

    Soma
    Last edited by phantomotap; 03-10-2011 at 08:47 PM. Reason: none of your business

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    @phantom like i said in the OP, i'll fix that stuff later. this is my first quarter learning C, so doubling the terminator doesn't top my priorities list. also, it doesn't double right now. might later, but till then not gonna worry.

    @tater you segfault, but definitely a better solution than mine. i'll fix it later. thanks =]
    Last edited by synhyborex; 03-10-2011 at 08:59 PM.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In the second loop, I should have changed the array index to j ... student[j].*
    Easily fixed.

    Quote Originally Posted by phantomotap View Post
    [Edit]
    Oh, wait, you are the one ignoring me.

    And I'm still amused by it.
    [/Edit]
    Soma
    You're an idiot.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    /me enters the room with a wiffle bat
    OK - enough squabble.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashes I suspect variable n.
    By Witch in forum C Programming
    Replies: 7
    Last Post: 04-01-2010, 03:31 AM
  2. Pointer Incrementing
    By zoowho in forum C Programming
    Replies: 4
    Last Post: 02-25-2010, 11:42 AM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM

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