Menu

Reading file gives windows error report

rookun
2007-09-17
2012-09-26
  • rookun

    rookun - 2007-09-17

    I've been trying to compile a program that I made a long time ago in Turbo C++. I'm trying to change it over to Dev C++ but every time I run it, I get the error reporting service. It happens which I call my read function.

    void load()
    {
    char ans;
    //Open file
    fp=fopen("c:/data.dta","rb");

    //If there is no file, request  creation
    if(fp==NULL)
    {
    printf("Database does not exist, do you want to create one? y/n");
        fflush(stdin);
        ans=getche();
        fclose(fp);
    
        if(ans=='n')
        {
            //If request denied, exit program
            printf("Program cannot create database\n\n");
            printf("Press any key to exit");
            getch();
            exit(1);
        }
    
        else
        {
            toprec=0;
            for(i=0;i<MAX;i++)
            {
                //Turn all idno numbers to -1
                dat[i].use= -1;
            }
            printf("\nDatabase has now been intialised");
        }
    }
    else
    {
    
    //Read file to structure
    rep=fread(dat,sizeof dat,1,fp);
    
        if(rep!=1)
        {
            //If file structure if different from program structure, display message
            printf("\n\nCannot read data, file may be corrupt.");
            printf("\nPress any key to exit");
            getch();
            exit(1);
        }
        else
        {
            toprec=-1;
    
            for(i=0;i<MAX;i++)
            {
                if(dat[i].use==0)
                {
                    toprec=i;
                    break;
                }
            }
            if(toprec==-1)
            {
                //If database is full then display message
                printf("Database full");
            }
    
        }
    }
    for(i=0;i<MAX;i++)
    {
        if(dat[i].use==-1)
        {
            //Turn toprec into records that have been read
            toprec=i;
            break;
        }
    }
    //Close file
    fclose(fp);
    

    }

    Dunno if that helps but yeh. I'm reading a .dat file into a structure. The program runs fine until I call this function. Can anyone help?

    Many thanks,

    Miles

     
    • rookun

      rookun - 2007-09-19

      Alright I got it working now. Moved the WORKING fclose and removed the null one.

      Thanks and sorry for missing your post.

       
    • Greg

      Greg - 2007-09-17

      whats the error you get. post your compiler log.

       
      • rookun

        rookun - 2007-09-17

        Compile log:

        Compiler: Default compiler
        Building Makefile: "J:\AddrBook\Makefile.win"
        Executing make...
        make.exe -f "J:\AddrBook\Makefile.win" all
        g++.exe main.o -o "AddressBook.exe" -L"J:/Dev-Cpp/lib" -L"J:/AddrBook"

        Execution terminated
        Compilation successful

        dat[] is a struct

        the load() function is running in a libary. I've tried shifting it into the main .cpp file and that doesn't work either.

         
    • Anonymous

      Anonymous - 2007-09-17

      ... and where and how is data defined?

      I hesitate to ask (because it sucks and lamost never works for me) but have you tried running the code in the debugger to see exactly where it halts (and hopefully, by variable inspection, why).

      You are unlikely to get a definitive answer for a runtime error from just a code fragment - you may need to post complete compilable/linkable code so that the problem can be reproduced. Try to reproduce it with a minimal test harness. Unfortunately in this case we'll also need a data file so you may have to include code to create one.

      Clifford

       
    • Anonymous

      Anonymous - 2007-09-17

      Sorry, .... where and how is 'dat[]' defined?

       
    • Greg

      Greg - 2007-09-17

      well it seems to be compiling fine so nothing there. what errors are you getting exactly when you run the program and when?

       
    • Anonymous

      Anonymous - 2007-09-17

      A couple of issues with your code:

      1) fflush(stdin);
      Calling fflush() on an imput stream is undefined behaviour. In the Microsoft C runtime (used by MinGW/GCC) it happens to work, but is non-portable.

      2) fclose(fp);
      Calling this when fp == NULL is and error; perhaps it is the error you are observing! The Microsoft C Runtime documentation for this function says: "If stream is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, fclose sets errno to EINVAL and returns EOF. It is recommended that the stream pointer always be checked prior to calling this function.". Admittedly it says that for the VC++ 2005 library, not VC++ 6.0 as used by MinGW, but it is an error nonetheless - and pointless, you cannot close something never opened!

      I suspect that (2) has resolved your issue, but given that this is 'remote control' problem resolution good information is key, with respect to the information you have provided:

      "dat[] is a struct" does not really answer my question. I also doubt it is true - it is an array of objects instantiated from a struct (but that is a technicality). What I really wanted to see was it's declaration - you have told me nothing useful. It is also 'evil' global data.

      "the load() function is running in a library": That is also hard to believe, the linker command line in your compiler log does not show any library being linked. Either way the compile log should be from a "Rebuild All"; but as this is a runtime error, it is of limited help, but it does provide information about your program's construction.

      It does not matter where load() exists, we cannot reproduce the error unless we can build the code. And moving it around is not likely to solve a runtime error (although it could conceivable hide or change the behaviour of one).

      It would be a good idea to build with the -Wall -Werror options to help ensure your code is 'clean'.

      "I get the error reporting service.": I am not sure what that means, presumably it emits some sort of error message - that information would be useful also - if only to clarify what you mean by "error reporting service". If it is as a result of calling fclose() with NULL, then since I have never tried that (because I know better), I will have never seen this - the point being is that you should not assume that we know what this is, and describe what you are seeing precisely.

      You never answered the question about the debugger - just because it never seems to work for me does not mean that it won't for you. If the error is due to fclose( NULL ) then the debugger will have got you to the precise line that failed.

      Clifford

       
    • rookun

      rookun - 2007-09-18

      Alright, I've tried the exact same code in Visual C++ 2005 and I've been able to narrow the problem down to the 'fclose(fp);' function. Any ideas on how to correct this?

      It works if I take it out but as you may know its bad coding practice.

       
      • Anonymous

        Anonymous - 2007-09-18

        Uh!? Are my posts invisible or something? See point (2) in my previous post - it may have saved you a lot of effort!

        You call fclose(fp) twice in your code - only one of them is correct - the one where fp is not NULL.

         
        • rookun

          rookun - 2007-09-19

          I've tried to remove it and it doesn't work. I've even tried opening the stream, loading then closing without all the other code and yet nothing. IF I have a file already there then it will work which defeats the idea of initialising it.

           
    • rookun

      rookun - 2007-09-19

      Meh, yeh sorry. Didn't see your other post for some reason. So as fp is NULL then so I stop calling fclose as you've mentioned nothing is being opened?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.