|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
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 -Nar |
|
#2
|
|||
|
|||
|
Hi,
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...notes/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"; ifstream inFile(filename); //check to see if opening the file was successful: if(!inFile) { cout<<"Failed to open file: "<<filename<<endl; return 1; } int number = 0; //read in the data until end of file is encountered: while(!inFile.eof()) { inFile>>number; //do something here with the data you read in before reading in more data. } Last edited by 7stud : February 23rd, 2003 at 05:00 AM. |
|
#3
|
|||
|
|||
|
c file proc
Thanks,
I used something like f = fopen(path to file,"r+t"); fscanf(f,"%d",number); 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. -Nar |
|
#4
|
||||
|
||||
|
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.
|
|
#5
|
|||
|
|||
|
Cleared it uot a bit
I tested for NULL, I found out it was NULL indeed when i tried fopen("c:\autoexec.bat","r"); however it gave way when i tried to access a file in the c:/tc/bin direvtory. I tried chdir("c:"); fopen("autoexec.bat","r"); it didn't work again... Any ideas |
|
#6
|
||||
|
||||
|
Double backslashes are your answer, my friend.
![]() fopen("c:\\autoexec.bat","r"); Also, try this: chdir("c:\\"); fopen("autoexec.bat","r"); and see if it works. |
|
#7
|
|||
|
|||
|
Hi,
I think your problem is with your filename fopen("c:\autoexec.bat","r"); 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: fopen("c:\\autoexec.bat","r"); |
|
#8
|
|||
|
|||
|
Thanks everyone!
-Nar |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > File opening procedures |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|