Thread: CSV C Program

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    55

    CSV C Program

    Hey guys and gals,

    Below is my code, my input file, and the output. I can't figure out why it is adding an extra zero at the end though. Can anyone help me with this?


    CODE:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
    
     
    FILE *ofp;
    
    FILE *ifp;
    
    
    int input;
    int sum = 1;
    int output[60];
    char filename[50];
    
    
    //Opens the file being printed to
     
    ofp = fopen("output.dat", "w");
    
    if(ofp == NULL)
       {
    
        printf("Unable to open output.dat for writing\n");
        exit(1);
    
       }
    
    
    //Opens up the file with numbers in it
    
    ifp = fopen("pix.dat", "r");
    
    
    if(ifp == NULL)
       {
    
        printf("Unable to open pix.dat\n");
        exit(1);
    
       }
    
    
    
    int i = 0;
    
    //fscanf(ifp, "%d", &input);
    
    while ( !feof(ifp) )
    {
      fscanf(ifp, "%d", &input);
      fprintf(ofp, "%d,",input );
      //output[i] = input;
      if(sum%20==0)
    	{
    	  fprintf(ofp, "\n");
    	}
      i++;
      sum++;  
    }
    
    
    return 0;
    
    }
    INPUT FILE:
    Code:
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    7
    0
    0
    0
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    0
    0
    0
    0
    0
    0
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    0
    0
    0
    0
    0
    0
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    0
    0
    0
    0
    0
    0
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    0
    0
    0
    0
    0
    0
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    0
    0
    0
    0
    0
    0
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    0
    0
    0
    0
    0
    0
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    6
    0
    0
    0
    0
    0
    0
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    0
    0
    0
    0
    0
    0
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    0
    0
    0
    0
    0
    0
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    0
    0
    0
    0
    0
    0
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    0
    0
    0
    0
    0
    0
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    0
    0
    0
    0
    0
    0
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    4
    0
    0
    0
    0
    0
    0
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    0
    0
    0
    0
    0
    0
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    0
    0
    0
    0
    0
    0
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    0
    0
    0
    OUTPUT FILE
    Code:
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
    0,0,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,0,0,0,
    0,0,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,0,0,0,
    0,0,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,0,0,0,
    0,0,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,0,0,0,
    0,0,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,0,0,0,
    0,0,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,0,0,0,
    0,0,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,0,0,0,
    0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,
    0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,
    0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,
    0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,
    0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,
    0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,
    0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,
    0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,
    0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,
    0,


    NOTE NOTE NOTE NOTE NOTE NOTE NOTE
    ------------------------------------------------
    I know that it is frowned upon to use feof to control a loop. Please disregard this mistake ;P Thanks!!!
    Last edited by DuckCowMooQuack; 11-15-2011 at 11:36 AM.

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Wow...So in this case the use of feof() did cause the error! By removing the extra return character at the end it solved the problem of the extra "0" at the end.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > By removing the extra return character at the end it solved the problem of the extra "0" at the end.
    No, that just fudged it to work with your program.

    What you really need to be doing is paying attention to what people say, and also on what the return result of fscanf() is.

    Fumbling around, ignoring us because your code "mostly works" isn't going to help you in the long run.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 07:44 PM