writing to files.

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

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    Please don't give me a hard time. Just look at the section of code and make some suggestions.
    Code:
    else if( ((strcmp(&Paverages[i*pagesize + 0*rowsize + 2*element],"C") == 0) &&
     (atoi(&Paverages[i*pagesize + 3*rowsize + (cols-1)*element]) + atoi(&Paverages[i*pagesize + 4*rowsize + (cols- 1)*element]) )  > 50))
    	{	
    	printf ("%d\n",atoi(&Paverages[i*pagesize + 3*rowsize + (cols-1)*element]) + atoi(&Paverages[i*pagesize + 4*rowsize + (cols- 1)*element]) );
    	p++;
    	fwrite(&Paverages[i*pagesize], sizeof(char), pagesize, fp);
    	fprintf(fc, "%s, %s\n", &Paverages[i*pagesize + element], &Paverages[i*pagesize + 2*element]);
    					
    	for (m = 0; m < cols; m++)
    		fprintf(fc, ",%s", &Paverages[i*pagesize + 3*rowsize + m*element]);
    		fprintf(fp, "\n");
    	for (m = 0; m < cols; m++)
    	       fprintf(fc, ",%s", &Paverages[i*pagesize + 4*rowsize + m*element]);
    		fprintf(fp, "\n");	
    	for (m = 0; m < cols; m++)				
    		fprintf(fc,",%d", atoi(&Paverages[i*pagesize + 3*rowsize + m*element]) + atoi(&Paverages[i*pagesize + 4*rowsize + m*element]));  				
    		fprintf(fc, "\n");		
    		}			
    				
    
    Data that should not gotten past the if statement and is not in fp is in fc.
    You can see that what is being tested is also being written when the condition is true. Its written even when the condition is false into fc and not in fp.
    Suggestions on where to look or what to do?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    1. Post the output from the printf.

    2. Post the code that declares all your variables and defined constants.

    3. Add the following in the block of the if, before your first printf(), then rerun it and post the output:

    Code:
    char * _s = &Paverages[i*pagesize + 0*rowsize + 2*element];
    printf ("%p %s\n", _s, _s );
    
    If it's not obvious, the string expression is copy-pasted from the first parameter of the strcmp() call in your if condition.
     
  3. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    writing

    The output of the printf is all greater that 50.
    The definitions are ok. I learned my lesson.
    #define pages 2000
    #define rows 10
    #define cols 39
    #define element 12
    #define pagesize (rows*cols*element)
    #define rowsize (cols*element)
    #define size (pages*rows*cols*element)
    char *Paverages;

    int i4, m, p;
    int i;

    Here's the output with the additional code
    writing to files
    0x100204938 C
    105
    0x100214928 C
    100
    0x10022c910 C
    432
    0x10023ed90 C
    101
    0x1002436b0 C
    51
    0x100247fd0 C
    95
    0x100266d68 C



    Code:
    	else if( ((strcmp(&Paverages[i*pagesize + 0*rowsize + 2*element],"C") == 0) &&
    				 (atoi(&Paverages[i*pagesize + 3*rowsize + (cols-1)*element]) + atoi(&Paverages[i*pagesize + 4*rowsize + (cols- 1)*element]) )  > atoi(argv[2])))
    		{	char * _s = &Paverages[i*pagesize + 0*rowsize + 2*element];
    			printf ("%p %s\n", _s, _s );
    						printf ("%d\n",atoi(&Paverages[i*pagesize + 3*rowsize + (cols-1)*element]) + atoi(&Paverages[i*pagesize + 4*rowsize + (cols- 1)*element]) );
    					p++;
    					fwrite(&Paverages[i*pagesize], sizeof(char), pagesize, fp);
    					
    					fprintf(fc, "%s, %s\n", &Paverages[i*pagesize + element], &Paverages[i*pagesize + 2*element]);
    					
    					for (m = 0; m < cols; m++)
    						fprintf(fc, ",%s", &Paverages[i*pagesize + 3*rowsize + m*element]);
    					fprintf(fp, "\n");
    					for (m = 0; m < cols; m++)
    						fprintf(fc, ",%s", &Paverages[i*pagesize + 4*rowsize + m*element]);
    					fprintf(fp, "\n");	
    					for (m = 0; m < cols; m++)				
    						fprintf(fc,",%d", atoi(&Paverages[i*pagesize + 3*rowsize + m*element]) + atoi(&Paverages[i*pagesize + 4*rowsize + m*element]));  //vol					
    					fprintf(fc, "\n");		
    				}			
    
     
  4. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #4
    chown

    I also discovered that when the second condition holds, I am putting an extra line feed in fp.
     
  5. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #5
    more code

    Most of the writing to Paverages takes place here.


    Code:
    
    if(strcmp(&Paverages[p*pagesize + 0*rowsize + 0*element], &Paverages[(p-1)*pagesize + 0*rowsize + 0*element]) !=0) 
    						{
    							pvalueb = 0;
    							pvalues = 0;
    							cvalueb = 0;
    							cvalues = 0;
    							
    						}
    						if (strcmp(arr[6],"P") == 0) 
    						{
    							pvalueb += atoi(arr[19]);
    							sprintf(&Paverages[p*pagesize + 1*rowsize + m*element],"%d", pvalueb); //contracts to buy  put
    							pvalues += atoi(arr[23]);
    							sprintf(&Paverages[p*pagesize + 2*rowsize + m*element],"%d", pvalues); 
    							
    						}
    						else 
    						{ 
    							cvalueb += atoi(arr[19]);
    							sprintf(&Paverages[p*pagesize + 3*rowsize + m*element],"%d", cvalueb); 
    							cvalues += atoi(arr[23]);
    							sprintf(&Paverages[p*pagesize + 4*rowsize + m*element],"%d", cvalues); 		
    						}
    						}	
    
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    In the code you've shown, you're not putting any line feeds in fp. You're only doing a single fwrite().
     
  7. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #7
    line feeds

    that's right. So how did they get there?
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    Some of your code somewhere is writing them.

    I have no sense of what structure or content your in-memory data has. It's somewhat conceivable, at least to me, that somewhere in all of that data there's a linefeed.
     
  9. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #9
    :confused:

    that won't explain the problem with the other file.

    the files I'm writing or the array I'm writing to. You have both those codes. DO you see anything. I also check a sorting routing; Nothing out of sorts there.

    more later.
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    I don't see anything obvious in your posted code, but frankly, the structure of the data is so non-obvious that I'm not sure I'd see a problem if there was one.

    There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.
    http://en.wikipedia.org/wiki/C._A._R._Hoare


    At a minimum, maybe add some debugging features to your file-writing code. Every time you fwrite() a block, precede it with a global sequence number in a recognizable form. Inspect the file and find the the sequence numbers that bracket the part with the extra line feed. Deduce where that code is, and go look there to narrow things down.

    Example of sequence numbering code (not compiled or tested).
    Code:
    static int sequenceNumber = 0;  // must be a global
    
    void writeSequenceNumber( FILE *f )
    {
      fprintf( f, "#$! seqnum: %d\n", sequenceNumber++ );
    }
    Then everywhere you have an fwrite():
    Code:
    writeSequenceNumber(fp); 
    fwrite(&Paverages[i*pagesize], sizeof(char), pagesize, fp);
    
     
  11. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #11
    fixed

    I separated the file writing into two loops. The solved one of the problems.
    The other problem was solve when I found the \n being printed to the wrong file.

    The preacher said, when he forget his glassed that "mine eyes are dim".
    Thanks.
     

Share This Page