You are here:

C/Reading in files

Advertisement


Question
I have two functions both are to read in information from a txt file and count how many records there are.  The first function

void determine_nr_employees(int* pNrEmployees)
{
   FILE* pEmployeeFile;
   int ID;
   char input_area[MAX_LINE];

   printf("Determining number of employees\n");

   pEmployeeFile = fopen("employees.txt", "r");
   if (pEmployeeFile == NULL)
   {
       fprintf (stderr, "Unable to open file employees.txt "
                        "in function to determine_nr_employees\n");
       abort();
   }

   *pNrEmployees = 0;
   while (fscanf(pEmployeeFile, "%d", &ID) == 1)
   {
       if (ID == -1)
       {
           (*pNrEmployees)++;
       }

       if (fgets (input_area, MAX_LINE, pEmployeeFile) ==NULL)
       {
           fprintf (stderr, "Error reading file employees.txt "
                            "in function determine_nr_employees\n");
           abort();
       }
   }

I have gotten to work, it is reading how many employees there are in a text file that contains info like 1 Davolio Nancy
2 Fuller Andrew
3 Leverling Janet
4 Peacock Margaret
5 Buchanan Steven
6 Suyama Michael
7 King Robert
8 Callahan Laura
9 Dodsworth Anne

I am having trouble with the second one.  I am trying to count how many orders there are and I have void determine_nr_orders(int* pNrOrders)
{
   FILE* pOrderFile;
   int order;
   nr_records =
   char input_area[MAX_LINE];

   printf("Determining number of orders\n");

   pOrderFile = fopen("orders.txt", "r");
   if (pOrderFile == NULL)
   {
       fprintf (stderr, "Unable to open file orders.txt "
                        "in function to determine_nr_orders\n");
       abort();
   }

   *pNrOrders = 0;
   while (fscanf(pOrderFile, " %d (%s %d) ", &order, &cust, &emp) == 1)

   {
       if (order == -1)
       {
           (*pNrOrders)++;
           order++;
       }

       if (fgets (input_area, MAX_LINE, pOrderFile) ==NULL)
       {
           fprintf (stderr, "Error reading file employees.txt "
                            "in function determine_nr_employees\n");
           abort();
       }
   }
   
   if (!feof(pOrderFile))
   {
       fprintf (stderr, "determine_nr_employees:  Error reading grades file\n");
       abort();
   }

   printf("%d orders found\n", order);
   fclose(pOrderFile);
}

that is reading from a text file that is displayed 3
11053 PICCO 2
11054 CACTU 8
11055 HILAA 7
11056 EASTC 8
11057 NORTS 3
11058 BLAUS 9
11059 RICAR 2
11060 FRANS 2
11061 GREAL 4
11062 REGGC 4
11063 HUNGO 3
11064 SAVEA 1
11065 LILAS 8
11066 WHITC 7
11067 DRACD 1
11068 QUEEN 8
11069 TORTU 1
11070 LEHMS 2
11071 LILAS 1
11072 ERNSH 4
11073 PERIC 2
11074 SIMOB 7
11075 RICSU 8
11076 BONAP 4
11077 RATTC 1

I have it displaying a count of how many orders which is the left column and when I compile it it gives me 11077 orders when in actuality there are really only 830 (i didnt display the whole text file, just a sampling).  I am unsure if I am reading in the file wrong?  Should I only be reading in one integer value, instead of trying to read in the 2 integer values and the string?  I am only trying to count the amount of integers there are.  If you could please provide any insight I would appreciate it.  Thank you for your time.

Answer
You are trying to:
> I have it displaying a count of how many orders which is the left column

But in the code, you are doing:
>  if (order == -1)

Your left column is never -1 and hence it will not execute the statements:
> (*pNrOrders)++;
> order++;

Also, every time it loops, it executes:
>  while (fscanf(pOrderFile, " %d (%s %d) ", &order, &cust, &emp) == 1)

Here it overwrites the previous value of order.
Hence even if it increments the value in the previous loops, it gets overwritten by the new value.

So, it is only retaining the last value of the first column and that is:
11077

Also in the statement:
> while (fscanf(pOrderFile, " %d (%s %d) ", &order, &cust, &emp) == 1)

why are you putting a parentheses before and after %s and %d?

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.