February 23rd, 2003, 03:08 AM
File opening procedures
I know this might sound stupid but I would like to know what functions I need to use for working with files.
I need functions that will work with c++, since I use fopen in c but I get errors when I switch to cpp. I also need functions that will work under any compiler.
Thanks for any help
February 23rd, 2003, 03:55 AM
Since C is a subset of C++, I'm not sure why your file I/O isn't working. Maybe you should post some simple code demonstrating the problem.(You can check here for an example of C File I/O: http://www.owlnet.rice.edu/~comp320/...otes/tut02-io/)
In C++ you can use streams for file I/O, but it's not quite as straight forward as giving you some function names. You have to create a pointer and assign it your file name, create an input file stream object with the pointer as the argument, and then use the object to read in the data.
const* char filename = "C:\\Data\\data1.txt";
//check to see if opening the file was successful:
cout<<"Failed to open file: "<<filename<<endl;
int number = 0;
//read in the data until end of file is encountered:
//do something here with the data you read in before reading in more data.
Last edited by 7stud; February 23rd, 2003 at 06:00 AM.
February 23rd, 2003, 06:09 AM
c file proc
I used something like
f = fopen(path to file,"r+t");
the stuff compiled ok, but after the app ran it gave an exception.
I use borlan c++ builder 5.0, and when i do the same code in a
c application it works fine.
February 23rd, 2003, 09:40 AM
Test the value of f returned by fopen(). If an error occured opening the file, the its value will be NULL. Using a NULL pointer is a leading cause of run-time errors.
February 23rd, 2003, 01:24 PM
Cleared it uot a bit
I tested for NULL, I found out it was NULL indeed when i tried
however it gave way when i tried to access a file in the
it didn't work again...
February 23rd, 2003, 02:50 PM
Double backslashes are your answer, my friend. :)
Also, try this:
and see if it works.
February 23rd, 2003, 02:51 PM
I think your problem is with your filename
A forward slash is an escape character, so you need to use "\\" when you want to denote a "\". My compiler(VC6) will give me a warning for the single "\", but it will compile, and when I run it, I get null for the pointer. This should work:
February 24th, 2003, 03:40 AM