Writing to a file

Discussion in 'Mac Programming' started by farmerdoug, Feb 20, 2010.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    Just some thoughts or ideas please.

    The following loop writes to a csv file. dates and times both equal three. Therefore when I open the csv file in excel there should be nothing past column 10. Then why, half way down the file, is a set of data put into columns 11 - 20? Since the input to the program is also csv files, I could be picking up an extra comma from the parsing routine. If anybody wants to throw out any ideas, it would be appreciated.
    Code:
    for ( i = 0; i  < n; i++)
        {
            fprintf(outputfile, ",%s ", &ratios[i*pagesize]);
            for (d = 1; d <=dates; d++)
                for ( t=0; t< times;t++) 
                    fprintf(outputfile,",%f",atof(&ratios[i*pagesize + (d)*rowsize + t*element]));				
    						
    	}
    
    
    char ** parse( char *record, char *delim)
    
    {
    	char **tarr;
    	char *p;
    	int i,fld=0;
    	if ( (p = (char *) calloc(element, sizeof(char))) == NULL)
    		printf("no memory for p");
    	tarr = (char **)calloc(100, sizeof(char*));
    	for (i = 0; i < 100; i ++)
    	{
    		if( (tarr[i] = (char*)calloc(element, sizeof(char))) == NULL)
    			printf("no memory allocated for arr\n");
    	}
    	
    	p = strtok(record,delim);
    	
    	while(p)
    	{	
    		strcpy(tarr[fld],p);
    		
    		fld++;
    		p=strtok('\0',delim);	
    	}
    
    	return (tarr);	
    }
    
    
    
    
    
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Seriously, this is exactly what a debugger is for. Just run it through gdb and you'll be able to see the values stored in all the variables and then'll be able to work out where it is going wrong.
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    What is there about "columns 11 - 20" that is also true about columns 1 - 10?

    The thing I noticed is that they both contain exactly 10 columns.

    So if your code somehow neglected to write a newline, you'd instantly have the appearance of "columns 11 - 20". However, it's really just one line (record) of data appended to another one (a data framing error).

    Unfortunately, you haven't posted any code that writes newlines, so any further analysis is impossible.
     
  4. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
  5. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #5
    thanks.
    I fixed it but don't know what happened. I was writing to an array from which data was eventually transfer to the csv file. It was wrong but it treated i = 0 as a separate case which wasn't necessary.
    With regard to debuggers, I'm trying to learn how to use them.

    doug.
     

Share This Page