Thread: Problems creating CSV file

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

    Problems creating CSV file

    I'm trying to read 400 numbers from a file with one single-digit number on each line. I need to read the numbers into a file with 20 on each line, separated by commas. When I run what I have now I get a bunch of garbage characters in my CSV file.


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

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Was there some reason you needed to start a new thread for this?

    Problems creating CSV file with column of numbers as input

    I already told you the easy way to do this... Load the entire file into memory at one time. Replace all the newlines with commas. If you want 20 per line, find every 20th comma and replace it with a newline... Write the file back out to disk, all in one piece.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    That's what I tried but when I try to just write it back to the file with the commas in it, I get a bunch of garbage written back and I'm wondering why.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cvvieth View Post
    That's what I tried but when I try to just write it back to the file with the commas in it, I get a bunch of garbage written back and I'm wondering why.
    Post that code please...

    If it is the code you posted in message #1, your mistake is trying to write the file back out character by character... But lets see where we're at first.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    I figured it out, I wasn't incrementing c when I wrote in the new line.


    I'm close to getting it to work, but when I write to the file now, the first line is correct with 20 numbers separated by commas but the next lines only have 19. I feel like this is an easy fix and I'm just missing something. Thanks very much for all the help. The output to the file looks like this. I'll post the code below.

    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

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        FILE *inputFile;
        FILE *CSVFile;
        char buff[800];
        int i=0;
        int c=0;
        int byte=0;
        
        inputFile= fopen("pix.txt", "r");
        CSVFile= fopen("csv.txt", "w");
        
        if(inputFile==NULL)
            printf("File not found");
        else
            fread(buff,sizeof(buff),1,inputFile);
            
        while(c<800){
            if(buff[c]=='\n')
            {
                buff[c]=',';
                c++;
                }
            else
                c++;
                }
              
        i=0;  
        c=0;
        while(c<800){
            while(i<20){
                if(buff[c]!=',')
                {
                    i++;
                    c++;
                }
            else
                {
                    c++;
                }
                }
        buff[c]='\n';
        i=0;
        }
                
        
            
        fputs(buff,CSVFile);
            
      system("PAUSE");    
      return 0;
    }
    Last edited by cvvieth; 11-26-2011 at 04:00 PM. Reason: Solved problem

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    From lines 33 to 47 above... it looks like you're incrementing your comma counter when there is no comma but not incrementing it when there is...

    Try moving line 37 down to line 41 and see what happens...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-25-2011, 06:42 PM
  2. Problems creating new line
    By cjohnson412 in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2008, 11:31 PM
  3. having problems with creating a file
    By jackmanplus in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2006, 08:47 PM
  4. creating a header file problems
    By ssjnamek in forum C++ Programming
    Replies: 13
    Last Post: 12-03-2003, 11:29 AM
  5. Problems creating a new class
    By nickname_changed in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2003, 01:47 AM