Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 19th, 2008, 5:05 PM   #1
Syntax_Error
Programmer
 
Syntax_Error's Avatar
 
Join Date: Jan 2008
Posts: 9
Rep Power: 0 Syntax_Error is on a distinguished road
C++ File Input/Output Assistance Please

I'm unsure exactly how to do file I/O in C++. I've been googling and found that fstream should do the trick, but when I start to use some tutorials, Dev-C++ complains about depreciated or antiquated headers. Can someone write up a little basic example of how I/O works. Like actually open an example text file and write to it or something. Just need something I can chuck into Dev-C++ and see if it still complains at me.

-Syntax_Error
Syntax_Error is offline   Reply With Quote
Old Jan 19th, 2008, 5:11 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Re: C++ File Input/Output Assistance Please

#include <iostream>   // basic iostream functionality
#include <fstream>    // file streams

int main()
{
    std::ofstream output("x.dat");
    output << "You are using old material\n";
    output.close();       // optional, fstream's implicitly closed as object is destructed
}
This will create a file named x.dat in the current working directory of your application .... wherever that is.
grumpy is offline   Reply With Quote
Old Jan 19th, 2008, 5:17 PM   #3
Syntax_Error
Programmer
 
Syntax_Error's Avatar
 
Join Date: Jan 2008
Posts: 9
Rep Power: 0 Syntax_Error is on a distinguished road
Re: C++ File Input/Output Assistance Please

Awesome, thanks. Worked well.

Now, with that out of the way, how would one go about opening said file back up, reading and looking for contents and if certain "settings" are set, altering code based on that.

for example... say I'm trying to do a simple "To-Do" application with flat file databasing for my "tasks"... how do I pull those back out, maybe sort based on a priority number or something, and show them to me. I can probably figure out how to edit/delete.
Syntax_Error is offline   Reply With Quote
Old Jan 19th, 2008, 11:29 PM   #4
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Re: C++ File Input/Output Assistance Please

You need to come up with your own file format. You can write and read int, doubles, and std::strings from the file. Don't have time to give a code example but a typical format is:

[Section]
Key = Value
Key2 = Value

[Section2]
Key = Value
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Jan 19th, 2008, 11:55 PM   #5
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: C++ File Input/Output Assistance Please

Quote:
Originally Posted by Syntax_Error View Post
for example... say I'm trying to do a simple "To-Do" application with flat file databasing for my "tasks"... how do I pull those back out, maybe sort based on a priority number or something, and show them to me. I can probably figure out how to edit/delete.
There are millions of ways to do that, but I would read/write binary structures because that is very fast. Reading/writing to text files is slooooow, but is ok if you don't want to do it very often.
struct data
{
    int a,b,c;
    char name[80];
    char something_else[80];
};
data array[16];
// now write a bunch of structures to a file
ofstream out("outfile.dat", ios::binary);
out.write(array, sizeof(array));

// read the array back into memory
ifstream in("outfile.dat", ios::binary);
in.read(array, sizeof(array));

There are lots of things you can do with the above, such as write them all out at one time as I did then read them back one at a time in random order if you want. The file is also very easy to update -- no need to rewrite the entire file like you have to do with text files.

The bad thing about the binary format illustrated above is that it is very difficult to change the file format. Lets say I want to add or remove something from that structure. To do that you would need to write a file conversion program that converts the data from the original structure format to the new one.
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem processing file into a char array csrocker101 C++ 1 May 9th, 2007 12:50 AM
add mutiple users to the smbpasswd file. Pizentios Bash / Shell Scripting 3 Oct 20th, 2005 1:48 PM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 10:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM
Structure char field to a disk file ehab_aziz2001 C++ 0 Feb 10th, 2005 3:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC