Thread: File input/output question

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    File input/output question

    I have been slaving over this for hours. Why is the following code:

    Code:
    #include <stdio.h>
    
    
    struct players
    {
    	char name[100];
    	char position[100];
    	double points;
    	double steals;
    	double blocks;
    	double rebounds;
    };
    
    struct players nba[100];
    
    
    int main()
    {
    	FILE *ifp, *ofp;
    	char inputFile[100];
    	char outputFile[100];
    	int numberPlayers;
    	
    	printf("What is the name of the input file?\n");
    	scanf("%s", inputFile);
    
    	
    	printf("What is the name of the output file?\n");
    	scanf("%s", outputFile);
    	
    	ifp = fopen("players.txt", "r");
    	
    	fscanf(ifp, "%d", &numberPlayers);
    	
    	fclose(ifp);
    	fclose(ofp);	
    
    	return 0;
    }
    Returning this error:

    Code:
     0 [main] a 3060 handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
      10000 [main] a 3060 stackdump: Dumping stack trace to a.exe.stackdump
    
     ----jGRASP cygwin wedge2: process died on signal 11.
     ----jGRASP: operation complete.
    I know it must be something simple that I'm doing, but I just cannot seem to figure it out. If you guys could help me I'd love you forever.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Your runtime error is probably caused by the following statement. It has no corresponding fopen statement.

    Code:
    fclose(ofp);

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    I took that out, I get the same error.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Your code runs fine for me when I comment out the offending statement. If I uncomment the offending statement, I get a runtime error.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    It compiles, but when I run it (with the players.txt file that contains only "10") I get the error

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Did you check that the result of the fopen call was non-null? (that is, the file opened successfully)

    Bob is correct in that fclose(ofp) will result in undefined behaviour, since ofp has not been initialised.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I tried using "10" in players.txt and it works fine. The only other thing that comes to mind is that possibly the scanf's having a problem with the input variables. There may be some "junk" in these variables. So, you may want to clear them out prior to using them.

    Bob

    Code:
    char inputFile[100]= {0};
    char outputFile[100] = {0};

  8. #8
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    The contents of inputFile and outputFile don't matter, since the OP used scanf on them to write to them before any other use.

    As I said above, there are two problems with the program that could cause problems:

    1. As you said, fclose(ofp); where ofp is not initialised.
    2. Not checking that the fopen was successful.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    The ipf returns a 0. Is this considered null?

    I have the players.txt file in the same directory as the exe file. I know in visual basic it needs to be in a bin directory, is there something similar the that in C?
    Last edited by allplay; 11-14-2005 at 07:03 PM.

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Yes, it's NULL, which means the file didn't open successfully. When fopen fails, look at strerror(errno) to see why it failed. Example:
    Code:
    /* additional headers at the top */
    #include <string.h> /* for strerror */
    #include <errno.h> /* for errno */
    
    /* ... */
    
    ifp = fopen("player.txt", "r");
    if (ifp == NULL)
    {
        fprintf(stderr, "Could not open file, reason: %s\n", strerror(errno));
        exit(1);
    }

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    Sorry guys for waisting your time. Windows made the name players.txt.txt

    grrrr, I knew it was something stupid.

  12. #12
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Yup, Windows is pretty stupid. :-)

    Check the return anyway, like I suggested above, it's a good habit.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    Thanks for your help. I'll put it in my program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. segmentaion fault with File Input/Output
    By sara.stanley in forum C Programming
    Replies: 7
    Last Post: 04-04-2006, 03:57 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM