SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old February 23rd, 2003, 02:08 AM
Narek Narek is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Yerevan, Armenia
Posts: 224 Narek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 1 m 30 sec
Reputation Power: 6
Send a message via ICQ to Narek
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

Reply With Quote
  #2  
Old February 23rd, 2003, 02:55 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #3  
Old February 23rd, 2003, 05:09 AM
Narek Narek is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Yerevan, Armenia
Posts: 224 Narek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 1 m 30 sec
Reputation Power: 6
Send a message via ICQ to Narek
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

Reply With Quote
  #4  
Old February 23rd, 2003, 08:40 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,799 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 9 h 55 m 28 sec
Reputation Power: 437
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.

Reply With Quote
  #5  
Old February 23rd, 2003, 12:24 PM
Narek Narek is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Yerevan, Armenia
Posts: 224 Narek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 1 m 30 sec
Reputation Power: 6
Send a message via ICQ to Narek
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

Reply With Quote
  #6  
Old February 23rd, 2003, 01:50 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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.

Reply With Quote
  #7  
Old February 23rd, 2003, 01:51 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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");

Reply With Quote
  #8  
Old February 24th, 2003, 02:40 AM
Narek Narek is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Yerevan, Armenia
Posts: 224 Narek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 1 m 30 sec
Reputation Power: 6
Send a message via ICQ to Narek
Thanks everyone!
-Nar

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > File opening procedures


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway