Advertisement

Simple File I/O

Started by December 28, 2000 04:25 PM
4 comments, last by mesHead 24 years ago
I was just wondering if someone could either show me how to open up a text file, make alterations to it, save it, etc. etc. or point me in the direction of some internet resources that could. BTW, im working out of VC++ 6.0 in the Win32 environment. (not sure if you really needed to know that, but I thought I''d say it anyway :D ) Thanks in advance ! -- mesHead.
mesHead
If you''re using VC++ 6, go into MSDN (the help library) and search for these functions.

fopen
fclose
fread
fwrite
fgets
fputs

just something to start you off.

-Mezz
Advertisement
if you know C++ output to and input from a file are very similar to writing to the screen. You can use cin and cout to do it if you are trying to write to a text file. And do you want to do random access I/O.

Well this is how you do it.

#include
#include //for file i/o

//to open a file for input

ifstream in;

in.open("filename.txt"); //"in" can be replaced by any name and filename.txt is the name of the file

char buf[500];
in >> buf; //to read from the file

in.close(); //to close the file

//for output

ofstream out;

out.open("filename.txt"); //same as above

//to open for both output and input
fstream both;

both.open("filename.txt");



hope that helped

Matthew
WebMaster
www.Matt-Land.com
It is foolish for a wise man to be silent, but wise for a fool.
if you know C++ output to and input from a file are very similar to writing to the screen. You can use cin and cout to do it if you are trying to write to a text file. And do you want to do random access I/O.

Well this is how you do it.

#include
#include //for file i/o

//to open a file for input

ifstream in;

in.open("filename.txt"); //"in" can be replaced by any name and filename.txt is the name of the file

char buf[500];
in >> buf; //to read from the file

in.close(); //to close the file

//for output

ofstream out;

out.open("filename.txt"); //same as above

out << buf;

//to open for both output and input
fstream both;

both.open("filename.txt");

both << buf;
botu >> buf;

hope that helped

Matthew
WebMaster
www.Matt-Land.com
It is foolish for a wise man to be silent, but wise for a fool.
If you want to go the straight "C" route, the above posts are perfect. If you want to try some C++ / MFC, try looking at the CFile, CStdioFile, and CString items in the MSDN. Here is quite snippit of opening a file with CStdioFile

  // Create our file object and open it for readingCStdioFile inFile("myfile.txt",CFile::modeRead);// Create our buffer. // A CString is just like a String in VB or a// String with no set limitCString buffer;// Read in until we hit a new lineinFile.ReadString(buffer);// Now buffer holds information from the text file// Lets reset our buffer to hold a sentence we want to outputbuffer = "my this is fun";// Now write our bufferinFile.WriteString(buffer);// We are all done, close the fileinFile.Close();  


Hope this helps.

Kevin =)

-----------------------------
kevin@mayday-anime.com
http://www.dainteractiveonline.com
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com
Ack, don''t use c++ stream i/o..it''s wicked slow Or so I read somewhere hehe...I reckon Paul Pedriana (a la Maxis) had some graphs showing this off.
http://www.ccnet.com/~paulp/

"This album was written, recorded and edited at Gröndal, Stockholm in the year of 2000. At this point in time money still ruled the world. Capitalistic thoughts were wide spread. From the sky filled with the fumes of a billionarie''s cigar to the deepest abyss drenched in nuclear waste. A rich kid was a happy kid, oh..dirty, filthy times. Let this be a reminder."
- Fireside, taken from back of the Elite album

This topic is closed to new replies.

Advertisement