3 Replies - 340 Views - Last Post: 26 January 2017 - 09:42 PM Rate Topic: -----

#1 simetra  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 26-January 17

New to C: Reading ints from an input file and printing them

Posted 26 January 2017 - 07:44 PM

I'm having problems with my first program in C. I'm supposed to read an undetermined amount of integers from an input file and print the positive integers to one file and the negatives to another file with the total counts at the bottom of each file.

I've got an input.txt file with these numbers separated by line breaks:
5
1
-22
-23
95
98

When I execute the program, it prints the 5 to the positives.txt but the rest of the numbers are 1s. For the negative file, it doesn't print any of the negatives.

#include <stdio.h>
#include <stdlib.h> // required for file I/O
#include <assert.h> // required for file I/O
 
int main(int argc, char *argv[])
{
   // Declarations
   FILE *inPtr   = fopen(argv[1], "r"); // declare and open input file
   FILE *posPtr  = NULL;        // positive number output file pointer
   FILE *negPtr  = NULL;        // negative number output file pointer
   int p         = 0;           // positive counter variable
   int n         = 0;           // negative counter variable
   int x;
 
   // Verify input file opened
   assert (inPtr != NULL);
 
   posPtr = fopen(argv[2], "w");  // open positive output file
   // FIXME assert (posPtr != NULL);       // verify file opened
 
   negPtr = fopen(argv[3], "w");   // open negative output file
   // FIXME assert (negPtr != NULL);       // verify file opened
 
   fscanf(inPtr, "%d", &x);            // read before loop
   while (!feof(inPtr))
   {
      if (x > 0)
         {
         fprintf(posPtr, "%d\n", x);   // print variable to positive file
     p = p + 1;                    // increase positive counter  
     }
      else if (x < 0)
     {
     fprintf(negPtr, "%d\n", x);   // print variable to negative file
     n = n + 1;            // increase negative counter
     }
 
      x = fscanf(inPtr, "%d", &x);     // read again
   }
   
   // Print final counts
   fprintf(posPtr, "The total number of positive integers: %d\n", p);
   fprintf(negPtr, "The total number of negative integers: %d\n", n);
 
   // Close the files
   fclose(inPtr);
   fclose(posPtr);
   fclose(negPtr);
 
   return 0;
}


I was getting warnings for the asserts so I commented them out for now.

Is This A Good Question/Topic? 0
  • +

Replies To: New to C: Reading ints from an input file and printing them

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1850
  • View blog
  • Posts: 6,646
  • Joined: 19-February 09

Re: New to C: Reading ints from an input file and printing them

Posted 26 January 2017 - 08:59 PM

Hi, welcome to DIC.

Generally, fscanf returns the number of items read. So have a look, at what is happening here :
38     x = fscanf(inPtr, "%d", &x);



Do you see what is wrong?

fscanf can return 1 if it reads one item, or 0 if it doesn't read a value, or -1 (EOF), I think, for an error. You could use the function in the while's conditional expression loop.

What warnings do you get for assert?
Was This Post Helpful? 0
  • +
  • -

#3 simetra  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 26-January 17

Re: New to C: Reading ints from an input file and printing them

Posted 26 January 2017 - 09:06 PM

View Post#define, on 26 January 2017 - 08:59 PM, said:

Hi, welcome to DIC.

Generally, fscanf returns the number of items read. So have a look, at what is happening here :
38     x = fscanf(inPtr, "%d", &x);



Do you see what is wrong?

fscanf can return 1 if it reads one item, or 0 if it doesn't read a value, or -1 (EOF), I think, for an error. You could use the function in the while's conditional expression loop.

What warnings do you get for assert?


Thanks! That helped.

I got the assert warnings worked out as well.

Not sure how to edit this to post problem solved.
Was This Post Helpful? 1
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1850
  • View blog
  • Posts: 6,646
  • Joined: 19-February 09

Re: New to C: Reading ints from an input file and printing them

Posted 26 January 2017 - 09:42 PM

There is likely no need as you have stated it solved in the post.

You get more edit rights after you have written about 15 posts.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1