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.
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-09-17
Sorry, .... where and how is 'dat[]' defined?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
well it seems to be compiling fine so nothing there. what errors are you getting exactly when you run the program and when?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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");
}
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
Alright I got it working now. Moved the WORKING fclose and removed the null one.
Thanks and sorry for missing your post.
whats the error you get. post your compiler log.
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.
... 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
Sorry, .... where and how is 'dat[]' defined?
well it seems to be compiling fine so nothing there. what errors are you getting exactly when you run the program and when?
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
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.
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.
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.
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?