Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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);	
}
 
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.
 
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?

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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.