Thread: Simple Error?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    20

    Simple Error?

    Code:
    #include <stdio.h>
    
    int main ()
    {
      FILE * pFile;
      if( rename( "C:/test2", "C:/test1" ) != 0 )
        rename( "C:/test1", "C:/test2" );
        pFile = fopen ("C:/test1","w+");
        fputs ("1 2",pFile);
        fclose (pFile);
        puts( "Done." )
      else
        remove( "C:/test1" );
        rename( "C:/test2", "C:/test1" );
        puts( "Done." );
      return 0;
    Compiling under PellesC, there are three build errors and I have no idea as to why...

    error #2001: Syntax error: found 'else' - expecting ';'.
    error #2157: Unrecognized statement.
    error #2001: Syntax error: found 'remove' - expecting ';'.
    Does anyone mind telling me where I went wrong in the syntax usage or pointing me in the correct direction? According to the two books I've read I have done nothing wrong so I'm stumped. Sorry to bother you all.

    Thanks in advance.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well its a syntax error to be precise, In C statements end with ;

    Dumbed down: if statements when evaluated will run the from the first curly-brace { to the corresponding brace } OR if no braces exist it will run the first statement under it.

    So it should be:
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	FILE * pFile;
    	if(rename("C:/test2", "C:/test1") != 0)
    	{
    		rename("C:/test1", "C:/test2");
    		pFile = fopen("C:/test1", "w+");
    		fputs("1 2", pFile);
    		fclose(pFile);
    		puts("Done.");
    	}else{
    		remove( "C:/test1" );
    		rename( "C:/test2", "C:/test1" );
    		puts( "Done." );
    	}
    	return 0;
    }
    Also please try and indent/format your code, its dang-ugly.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Thank you very much zacs7, the code worked perfectly. I didn't know there needed to be to be braces between the if statements, I'm too used to pseudo :P

    I now have a secondary error however, the program's initial part in which it renames the initial file is to see if it the file which is being renamed in-fact exists, this however doesn't seem to trigger an error and to my knowledge the rename function shouldn't give you "0" as a response if it fails to find the file which is to be renamed. Does anyone know how to combat this?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The return value of rename() is zero upon success, non-zero on error.
    Kurt

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    rename will return non-zero if it failed to rename the file, and zero if it succeeds.

    renaming a file to check if it exists isn't the best way to do it, some non-specific methods include opening and closing the file (bad if the file is locked, or you don't have permissions). Or something OS specific.

    There's probably a few algorithms on cprogramming.com.

    Note: If you want to reference standard C functions use http://www.cplusplus.com/reference/clibrary/ or many others, or do a man &#37;func

    hth

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Quote Originally Posted by ZuK View Post
    The return value of rename() is zero upon success, non-zero on error.
    Kurt
    I just saw my mistake before I refreshed this thread :P Thank you very much however, you're all quite a lovely & helpful bunch.

    Take care & my regards

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Sorry for the double post but it appears that it did not work after-all. (should have compiled before i opened my mouth) Can anyone suggest another API that would offer what I need?

    EDIT: I tried it with fopen & it failed to do what I wanted

    Code:
    #include <stdio.h>
    
    int main ()
    {
    	FILE * pFile;
    	if(fopen("C:/test2", "r") != 0)
    	{
    		rename("C:/test1", "C:/test2");
    		pFile = fopen("C:/test1", "w+");
    		fputs("1 2", pFile);
    		fclose(pFile);
    		puts("Done.");
    	}else{
    		remove( "C:/test1" );
    		rename( "C:/test2", "C:/test1" );
    		puts( "Done." );
    	}
    	return 0;
    }
    Last edited by int3; 04-21-2007 at 06:23 AM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    	if(fopen("C:/test2", "r") != 0)
    Don't unterstand what you try to do, but this fopen() call is not a good idea. You never store the returnvalue so you have no way to close the file, and I think you cannot rename an open file.
    Kurt

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Quote Originally Posted by ZuK View Post
    Code:
    	if(fopen("C:/test2", "r") != 0)
    Don't unterstand what you try to do, but this fopen() call is not a good idea. You never store the returnvalue so you have no way to close the file, and I think you cannot rename an open file.
    Kurt
    Either way, it would end up going one way or another even though it wouldn't execute the renaming function. So before I can even consider that, I must get past the original IF statements.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by int3 View Post
    Either way, it would end up going one way or another even though it wouldn't execute the renaming function. So before I can even consider that, I must get past the original IF statements.
    fopen() returns nonzero on success.
    your code is like
    Code:
    if ( open "test2" succeedes )
       rename "test1" to "test2"
    Kurt

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Quote Originally Posted by ZuK View Post
    fopen() returns nonzero on success.
    your code is like
    Code:
    if ( open "test2" succeedes )
       rename "test1" to "test2"
    Kurt
    I inverted the IF conditions and I still get the same issue.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by int3 View Post
    I inverted the IF conditions and I still get the same issue.
    Abd what is the issue ? ( and don't say it doesn't work )
    Kurt

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Quote Originally Posted by ZuK View Post
    Abd what is the issue ? ( and don't say it doesn't work )
    Kurt
    The program initially looks to see if a backup of the file in question exists;

    Code:
    if(fopen("C:/test2", "r") != 0)
    If one doesn't then a backup is made and a new one is made with certain text;

    Code:
    rename("C:/test1", "C:/test2");
    pFile = fopen("C:/test1", "w+");
    fputs("1 2", pFile);
    fclose(pFile);
    puts("Done.");
    If a backup does exist then the backup is deleted & the original is restored;

    Code:
    remove( "C:/test1" );
    rename( "C:/test2", "C:/test1" );
    puts( "Done." );

  14. #14
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I understand that your code is either like this
    Code:
    if(fopen("C:/test2", "r") != 0) {
            // test2 is open now
    	rename("C:/test1", "C:/test2"); // this will fail ( cannot rename open file )
    	pFile = fopen("C:/test1", "w+");
    	fputs("1 2", pFile);
    	fclose(pFile);
    	puts("Done.");
    } 
    else {
            // test2 doesn't exist
    	remove( "C:/test1" );        
    	rename( "C:/test2", "C:/test1" ); // will fail because test2 doesn't exist
    	puts( "Done." );
    }
    or like this
    Code:
    if(fopen("C:/test2", "r") == 0) {
            // test2 doesn't exist
    	rename("C:/test1", "C:/test2"); 
    	pFile = fopen("C:/test1", "w+"); // will fail because test1 has just be renamed to test2
    	fputs("1 2", pFile);
    	fclose(pFile);
    	puts("Done.");
    } 
    else {
            // test2 is open
    	remove( "C:/test1" );        
    	rename( "C:/test2", "C:/test1" );// this will fail ( cannot rename open file )
    	puts( "Done." );
    }
    if your actual version is different. post it.
    Kurt

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    I posted the actual copy a few posts ago (#7)

    http://cboard.cprogramming.com/showp...12&postcount=7

    I'm already aware of the issues that I will have, I just can't seem to get past the initial IF sequence where it checks to see if the file even exists.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM