Thread: Silly problem

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Silly problem

    Help me please with the following code:

    if ((str = fopen("init1.txt", "r")) == NULL) {
    fprintf(stderr, "Cannot open init file 1\n"); getc(stdin);
    return 1;
    }//if

    The code actually is correct. However, when I am debuging the above I get the following messages

    - str 0x67161448 {_ptr=0x00000000 <Bad Ptr> _cnt=0 _base=0x00000000 <Bad Ptr> ...} _iobuf *
    + _ptr 0x00000000 <Bad Ptr> char *
    CXX0030: Error: expression cannot be evaluated
    _cnt 0 int
    - _base 0x00000000 <Bad Ptr> char *
    CXX0030: Error: expression cannot be evaluated
    _flag 1 int
    _file 3 int
    _charbuf 0 int
    _bufsiz 0 int
    - _tmpfname 0x00000000 <Bad Ptr> char *
    CXX0030: Error: expression cannot be evaluated

    I can't figure out what the problem is. The code works but I'd rather not read such a message.
    I use MSVS 2008.
    Thank you in advance!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    How is "str" declared?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    #include <stdio.h>
    #include <stdlib.h>

    integer main(void) {
    double a;
    FILE *str;

    if ((str = fopen("init1.txt", "r")) == NULL) {
    fprintf(stderr, "Cannot open init file 1\n"); getc(stdin);
    return 1;
    }//if
    fseek(str, 0, SEEK_SET);

    fscanf(str, "%d\n", &a);

    fclose(str);

    }//main

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please post your code in [code][/code] bbcode tags.

    Are you sure that you posted the code that you actually compiled? MSVC8 reports the errors:
    Code:
    (4) : error C2146: syntax error : missing ';' before identifier 'main'
    (4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    (4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    These are about your use of integer instead of int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    I am sorry, it was my fault. The program is a small part of the largest project. The following works as a console Win32 application from existing code.
    Code:
    typedef int integer;
    
    #include <stdio.h>
    #include <stdlib.h>
    
    integer main(void) {
    double a;
    FILE *str;//place the breakpoint here
    
    if ((str = fopen("init1.txt", "r")) == NULL) {
    fprintf(stderr, "Cannot open init file 1\n"); getc(stdin);
    return 1;
    }//if
    fseek(str, 0, SEEK_SET);
    
    fscanf(str, "%lf\n", &a);
    
    fclose(str);
    
    return 0;
    
    }//main
    The debugger replies as follows:

    - str 0x6a7c1448 {_ptr=0x00000000 <Bad Ptr> _cnt=0 _base=0x00000000 <Bad Ptr> ...} _iobuf *
    + _ptr 0x00000000 <Bad Ptr> char *
    CXX0030: Error: expression cannot be evaluated
    _cnt 0 int
    + _base 0x00000000 <Bad Ptr> char *
    CXX0030: Error: expression cannot be evaluated
    _flag 1 int
    _file 3 int
    _charbuf 0 int
    _bufsiz 0 int
    - _tmpfname 0x00000000 <Bad Ptr> char *
    CXX0030: Error: expression cannot be evaluated

    Once again thank you for the prompt response.
    Last edited by samolet4e; 08-01-2009 at 12:01 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, I think I understand: you can ignore those messages. If I read them correctly, they are just telling you that additional information that might otherwise be provided cannot be provided since the pointer is a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple, silly yet big problem....
    By punk in forum C++ Programming
    Replies: 7
    Last Post: 12-20-2007, 11:25 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM