Thread: Problems creating CSV file with column of numbers as input

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Problems creating CSV file with column of numbers as input

    I'm trying to create a CSV file with my input file being 400 numbers, one on each line. When I run the program I get strange output in my CSV file. Any help would be appreciated. Thank you very much!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        
    int main(int argc, char *argv[])
    {
        FILE *inputFile;
        FILE *CSVFile;
        char buff[400];
        int i=0;
        int c=0;
        
        
        
        inputFile= fopen("pix.txt", "r");
        CSVFile= fopen("csv.txt", "w");
        
        if(inputFile==NULL)
            printf("File not found");
         else
             while(c<400){
             buff[c]=getc(inputFile);
             c++;
             }
             
             i=0;
             c=0;
             
             while(c<400){
             putc(buff[c],CSVFile);
             c++;
             i++;
             if(i<20)
             {
                 putc(',',CSVFile);
             }
             else
             {
                 putc('\n',CSVFile);
                 i=0;
             }
             }
            
            
      
      system("PAUSE");    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Since you're going 1 character at a time and only have an array of 400 chars, can I assume that all the numbers in pix.txt are single digits? If they can be larger you need to rethink things -- you don't have enough room in the array and you'd print a comma between every digit.

    Assuming that they are just single digits...

    Quote Originally Posted by cvvieth
    one on each line.
    So the start of the input file might look like:

    1
    2
    3
    4

    The first character read is '1'
    The second character is the newline character, not '2'.

    Your code will put the newline into the array, taking up space (so not all the numbers will be read in), and causing a newline to be printed in the output file. You need to avoid putting the newlines in the array, or make the array twice the size and skip over newlines when writing the output. Don't do the latter

    Also if you read fewer than 400 numbers, your loop will print whatever junk uninitialised stuff is in the array.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... easy way... Load the whole file into memory using a single fread() call. Loop through it and replace all \n with , ... write to disk.

    Next job...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having problems with creating a file
    By jackmanplus in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2006, 08:47 PM
  2. Creating a file with random numbers question??
    By Hoser83 in forum C Programming
    Replies: 28
    Last Post: 02-16-2006, 02:11 PM
  3. Creating a multi-column listbox?
    By gflores in forum C# Programming
    Replies: 2
    Last Post: 01-09-2006, 04:23 AM
  4. Reading / input column of numbers into an array
    By boyfarrell in forum C Programming
    Replies: 6
    Last Post: 08-28-2005, 10:24 AM
  5. Getting an input stream to stop creating a file
    By Stevek in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2003, 04:45 PM