![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2008
Posts: 9
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,318
Rep Power: 5
![]() |
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
} |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jan 2008
Posts: 9
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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:
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#5 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 653
Rep Power: 4
![]() |
Re: C++ File Input/Output Assistance Please
Quote:
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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |