The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
File opening procedures
Discuss File opening procedures in the C Programming forum on Dev Shed. File opening procedures C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 23rd, 2003, 02:08 AM
|
|
Member
|
|
Join Date: Feb 2003
Location: Yerevan, Armenia
Posts: 224
Time spent in forums: 21 h 1 m 30 sec
Reputation Power: 11
|
|
|
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
|

February 23rd, 2003, 02:55 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
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.
|

February 23rd, 2003, 05:09 AM
|
|
Member
|
|
Join Date: Feb 2003
Location: Yerevan, Armenia
Posts: 224
Time spent in forums: 21 h 1 m 30 sec
Reputation Power: 11
|
|
|
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
|

February 23rd, 2003, 08:40 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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, 12:24 PM
|
|
Member
|
|
Join Date: Feb 2003
Location: Yerevan, Armenia
Posts: 224
Time spent in forums: 21 h 1 m 30 sec
Reputation Power: 11
|
|
|
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
|

February 23rd, 2003, 01:50 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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.
|

February 23rd, 2003, 01:51 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
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");
|

February 24th, 2003, 02:40 AM
|
|
Member
|
|
Join Date: Feb 2003
Location: Yerevan, Armenia
Posts: 224
Time spent in forums: 21 h 1 m 30 sec
Reputation Power: 11
|
|
|
Thanks everyone!
-Nar
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|