Advertisement

i/o with .txt files.......

Started by November 05, 2000 03:32 PM
2 comments, last by jim bob 24 years, 2 months ago
Hello again. I had a question about input and output on .txt files. Could you show me some code (VC++) demonstrating how to read and output in an .exe from a .txt file. And also, how to write to a .txt from an .exe. Both reading and writing to and from a .txt file I should say using a Win32 Console App. Thanks in advance. Also, can and how do you do the exact same thing with a Win32 app? Both would be nice. Thanks a lot! I am not worthy of a sig!
I am not worthy of a sig! ;)
Hi,

    #include "fstream.h"void main(){  ifstream filein( "readme.txt" );  int somenumber;  filein >> somenumber;  filein.close();}    


Will open a text file called readme.txt, and read in an integer from it. If you wanted to read in strings, you need to make a character array big enough to hold the string being read in, and do a similar thing.

If you dont know how long the string is, then assuming your character array is 1024 bytes in size, you can go:

char MyArr[1024];

filein.getline( MyArr,1024 );

This is from memory - im not too sure about the above.



regards,

GeniX

www.cryo-genix.net
regards,GeniXwww.cryo-genix.net
Advertisement
Writing to a text file is just like using cout.
    ofstream myfile("file.txt",ios::out);myfile << "This will be written to the file" << endl;    



==================
/* todo: insert cool sig */
Martee
Magnum Games.NET
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
ofstream fout("outfile.txt");
ifstream fin("infile.txt");

void readafile()
{
char buffer[255];
fin>> buffer;
fin.close();
}

void writefile()
{
char buffer2[10] = {''1'',''1'',''1'',''1'',''1'',''1'',''1'',''1'',''1'',''1''};
fout<< buffer;
fout.close();
}

--------------------------

Whats a pretty girl like you doing in a dirty mind like mine?
Those who dance are considered insane by those who cannot hear the music.

This topic is closed to new replies.

Advertisement