Thread: Compile error

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Compile error

    Could someone please tell me what's wrong with this code snippet? It runs perfectly well when compiled with MinGW, but selecting 'f' crashes the program when it is compiled with VS2010. I wasn't able to isolate the bug, but the files aren't removed or renamed.

    Code:
    		case 'f':
    			case 'F':
    			write(); //calls write() if (f) is selected
    
    			/* verifies file closure */
                if (fclose(f) != 0 || fclose(f_old) != 0)
                {
                    printf("\nFile close error.");
                    return 1; //return error message
                }
    
    			/* replaces original document with new one */
    			remove("database.txt"); //deletes original document
    			rename("database_temp.txt", "database.txt"); //renames document
    					
    			return 0; //program exits normally
    
    			default: //returns error message if input is none of the above
    			printf("Invalid input.\n");
    Here's the write() function:

    Code:
    int write ()
    {
    	int i; //declares counter
    
        /* cycles each seat variable to file */
        for (i = 1; i <= 30; i++)
        fprintf(f, "%s\n%s\n%s\n\n",
                seat[i].no, seat[i].fname, seat[i].lname);
    
    	return 0; //returns to main function
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What is the error?

    There is another function in the library called write() so that is probably why it is complaining. Change the name of the function.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I tried changing the function name but to no avail. I think the problem is compiler-specific, otherwise MinGW would have complained as well.

    Problem signature:
    Problem Event Name: APPCRASH
    Application Name: 2.exe
    Application Version: 0.0.0.0
    Application Timestamp: 4cfd0be9
    Fault Module Name: ntdll.dll
    Fault Module Version: 6.1.7600.16559
    Fault Module Timestamp: 4ba9b21e
    Exception Code: c0000005
    Exception Offset: 00046bf0
    OS Version: 6.1.7600.2.0.0.256.1
    Locale ID: 1033
    Additional Information 1: 0a9e
    Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
    Additional Information 3: 0a9e
    Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

    Read our privacy statement online:
    Windows 7 Privacy Statement - Microsoft Windows

    If the online privacy statement is not available, please read our privacy statement offline:
    C:\Windows\system32\en-US\erofflps.txt

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First off... putting code in switch statements is a BAD idea. Declaring variables in switch cases is an even bigger recipe for disaster. It makes debugging near impossible. It is far better to write functions to do these tasks and then call the function from the switch statement.

    Quote Originally Posted by 843 View Post

    Code:
    int write ()
    {
    	int i; //declares counter
    
        /* cycles each seat variable to file */
        for (i = 1; i <= 30; i++)
        fprintf(f, "%s\n%s\n%s\n\n",
                seat[i].no, seat[i].fname, seat[i].lname);
    
    	return 0; //returns to main function
    }
    Since you are not returning any useful information here, you should redeclare the write function as void write(void) and remove the return statement.

    Also it looks like you have the paramters for your rename() call backwards...

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Have you tried to debug using debugger yet?
    Code:
     fprintf(f, "%s\n%s\n%s\n\n",
                seat[i].no, seat[i].fname, seat[i].lname);
    Is f a valid FILE pointer?
    What about seat[i] ? Is sear[i].no integer or string?
    In C, for an array a[N], valid indices are 0-N-1.
    In your loop it's for(i = 1; i <= 30 ;i++)

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thanks for all the feedback. How do you exit a function in the middle of a code if the type is void?

    I haven't learned how to use a debugger yet, but the program works perfectly when compiled with MinGW. As for the loop condition, I started with 1 as seat[0] is empty due to certain seat arrangements.

    As for rename(), its argument is (old_name, new_name) right?
    Last edited by 843; 12-06-2010 at 10:48 AM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1) "return;" is still valid in a void function.

    2) MinGW and VC use different run-time libraries, and just in general do things differently. It is not at all surprising or uncommon (in fact it is very common) for an incorrect program to crash in one and just do something odd (or, worse, something that looks correct) in the other. Bayint's suggestion about checking your loop bounds ("i <= 30") looks like a very good place to start to me.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Except for the initialization, which includes 0, every single loop that cycles through the array variables uses the same condition. I'm not sure if there's anything else I can check.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    1) "return;" is still valid in a void function.
    But return 0; is not.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I renamed the file pointer from f to f_new and it worked!

    Any idea why?

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Probably because you're using icky global variables and you perhaps have multiple variables called f with different scopes and write isn't using the one you think it's using. Just a guess though, based on what code you did post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  3. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM