Writing to a file

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

  1. farmerdoug macrumors 6502a

    Sep 16, 2008
    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.
    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);
    	return (tarr);	
  2. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    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

    Aug 9, 2009
    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


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

    Sep 16, 2008
    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.


Share This Page