Thread: Why Isent My Code Executing?

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Why Isent My Code Executing?

    I have some code that writes to a file, if the file already exists it needs to update bytes 5-8. This is not happening tho, so I added some printfs to try and figure out whats going wrong:
    Code:
    int PolygonSave(Polygon p, const char* filename,  int append)
    {
    	FILE *file = fopen(filename, (append)? "a+b": "w+b");
    	
    	int num_poly = 1;;	
    
    	if(ftell(file) < 9) //Write header info	 
    	{
    		fwrite("POLY", 1, 4, file);
    		fwrite(&num_poly, sizeof(int), 1, file);
    	}
    	else // Read the number of polygons, increment & write back
    	{
    		fseek(file, 4, SEEK_SET);
    		fread(&num_poly, sizeof(int), 1, file);
    		num_poly++;
    		fseek(file, 4, SEEK_SET);
    		fwrite(&num_poly, sizeof(int), 1, file); // This should overwrite - hopefully
    		fseek(file, 0, SEEK_END);
    		printf("ISDOINGSOMETHING\n");
    	}
    	printf("FS: %u\n", ftell(file));	
    	printf("NP: %u\n", num_poly);
    Heres what I dont get: If the file exists then ftell gives the correct size, which will be >8. This means that the code in the else block should be executed; however, it never prints ISDOINGSOMETHING. I'm getting no errors or warnings with gcc -Wall -std=c99

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Add some printf() statements in the if part and make sure it's not executing that part. BTW, also add a check to see if the file has been opened or not.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Code:
           a+     Open for reading and appending (writing at end  of  file).   The
                  file is created if it does not exist.  The initial file position
                  for reading is at the beginning  of  the  file,  but  output  is
                  always appended to the end of the file.
    Even if you seek to the start, you still end up writing to the end of the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Add some printf() statements in the if part and make sure it's not executing that part. BTW, also add a check to see if the file has been opened or not.
    Ok I changed the code a little. Now it seems the problem was that ftell was returning 0 before appending to the file, but it returns the correct size after. I Still dont know how to fix it tho o_0 now I get all 0's.

    Even if you seek to the start, you still end up writing to the end of the file.
    That sounds like the answer, but then how can I open a file to read then append to? I cant seem to find a suitable option in the fopen manpage :/

    Edit: Ignore the above - its "r+"

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Sorry, just a question about the following code:
    Code:
    (append)? "a+b": "w+b"
    how does this work? is "append" a variable/macro constant that you set depending on the task(1 to append and 0 to overwrite)?
    I might not be a pro, but I'm usually right

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Its basically just a truth check. It would be the equivelant to writing:
    Code:
    if(append != 0)
        file = fopen(filename, "a+b");
    else
        file = fopen(filename, "w+b");
    For more info: http://en.wikipedia.org/wiki/%3F:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Giving instruction to an executing C code
    By darkwalk in forum C Programming
    Replies: 2
    Last Post: 10-24-2006, 12:01 PM
  2. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Executing C++ Code at Runtime
    By jdm in forum C++ Programming
    Replies: 14
    Last Post: 10-18-2004, 05:32 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM