Thread: Run Time error R6001

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    11

    Run Time error R6001

    i am using this piece of code in my program which reads characters from a file and operates on them

    Code:
    char ch;
    
    do 
    {
        ch=fgetc(FILE_NAME);
       
        switch(ch)
        
        /// some stuff
    } while (ch!=EOF)
    the code works fine but after the execution, i get the following runtime error

    Runtime error R6001
    -null pointer assignment

    what could be the possible reason?

    Tnx

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What is FILE_NAME? Is it a string? If so, that's wrong.

    Incidentally, your loop is incorrect. Something more like this would stop you from using EOF as a char:

    Code:
    while((ch=fget(pFile)) != EOF) /* Note:  pFile needs to be a valid FILE * */
    {
       ....
    }
    Also, ch needs to be of type int, not a char, because that's what fgetc() returns.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int fgetc( FILE *stream );

    1. ch should be int
    2. check if it is not EOF before using in the switch statement
    3. parameter for the fgetc should be FILE* and not filename
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And EOF doesn't fit into a char

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by zacs7 View Post
    And EOF doesn't fit into a char
    Actually, that's what I meant to say.

    I should have clarified my last statement. fgetc() returns an int because a char can't hold EOF, but an int can.....

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    11
    the loop statement does not solve the problem....
    FILE_NAME is a pointer not a string
    and i guess ch being char does not make any difference as ultimately i think you can even print an integer as a char whose ascii value is that int

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    11
    declaring ch as int doesn't solve the prob either.....

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by sakshamgupta View Post
    the loop statement does not solve the problem....
    It solves another obvious problem you might have come running back here for.

    Quote Originally Posted by sakshamgupta View Post
    FILE_NAME is a pointer not a string
    A pointer to what? A FILE object? The first element of a char array?

    Quote Originally Posted by sakshamgupta View Post
    and i guess ch being char does not make any difference as ultimately i think you can even print an integer as a char whose ascii value is that int
    If you use a char, you could get weird behavior in your code when you do receive EOF.

    I've seen it happen before.

    You need an int if you are expecting EOF.

    Quote Originally Posted by sakshamgupta View Post
    declaring ch as int doesn't solve the prob either.....
    Maybe not, but it definitely fixes another problem that should be addressed.

    I'd rather tell you everything wrong with your code now than wait for you to come back with one problem after another to be fixed in the order of your choosing.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    11
    thnx man for these advices....ill try to mend them next time on...
    but the problem still persists....
    and yes pointer to the file object

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The problem you speak of does not appear to be directly related to the code you posted.

    Editx2: It appears you're possibly using a really old DOS compiler. Is this the case? If so, you really should consider upgrading to a free Windows compiler.

    Incidentally, are you making sure that fopen() is not returning NULL?
    Last edited by MacGyver; 05-31-2007 at 04:48 AM.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sakshamgupta View Post
    the code works fine but after the execution, i get the following runtime error
    What does "after the execution" mean? After the execution of the block of code you posted, or after the execution of the entire program (i.e., does it crash right at the end, before it's supposed to exit)? Be more specific. The snippet you posted is too vague to help anything.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Because he's using a crappy DOS compiler which can only detect NULL pointer assignments by looking at the memory just after where NULL points to see if it's been written to.

    With a real operating system, the program would be dead as soon as it tried to dereference NULL (and a debugger would take you right to the culprit).

    But DOS only gives you a faint echo of past trouble, and you have to guess where it all went wrong. It certainly gives no help in trying to read via NULL, only write via NULL.
    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.

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Moving on, Why don't you get a good compiler as Salem suggested earlier?
    There are better DOS compilers... See the FAQ.

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    58
    When you used fopen

    did you do it like this?

    Code:
    fp = fopen("C:\File.txt","r");
    or like this:

    Code:
    fp = fopen("C:\\File.txt","r");

  15. #15
    Registered User
    Join Date
    May 2007
    Posts
    11
    thank you all for your replies....
    compiling the code in a new compiler solves the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Run time error?
    By shwetha_siddu in forum C Programming
    Replies: 6
    Last Post: 04-21-2009, 01:37 PM
  2. How to time how long a program takes to run?
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 08-18-2008, 07:50 PM
  3. Replies: 7
    Last Post: 11-26-2007, 01:11 PM
  4. Linked List Run Time Error
    By Frost Drake in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2006, 02:30 AM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM