File input output
I am trying to output to a file and read in from that file in VC5. I looked in the help and found many different types of functions with types like FILE and other functions like cin and cout and i guess they can be used to write to a file. I am trying to write out a struct to a file. I know C++, but I''m not familiar will all of these functions, and I don''t know which one(s) to use and how to use them. I am used to VB where it is just a few lines of code and it will write out or read in files.
t2sherm ô¿ô
try this...you''re prolly doing a windows-ish program, but you should be able to adapt this....
Ok, real simple and crude code, but thats the bare bones of it....
#include // I/O stream#include // filestream headervoid main(){ostream Output; // Variable...istream Input;input.open("C:\file.in"); // open the input fileoutput.open("C:\file.out"); // open the output file//then read from the input like thisinput >> variable;//then write to output like this...output << variable;//then you MUST close the filesinput.close();output.close();return;}
Ok, real simple and crude code, but thats the bare bones of it....
Try this (C):
-------------------
#include <stdio.h>
FILE *outFile;
outFile=fopen("c:\\autoexec.bat", "r");
/* the second has to be mode; r for read, b for binary... it can be a combo like "w" */
fprintf(outFile, "Hi, your autoexec.bat is dead.\n");
fclose(outFile);
-----------------
or this (C++) :
-----------------
#include <fstream.h>
fstream outFile;
outFile.open("c:\\autoexec.bat", ios::out);
outFile << Your autoexec.bat is dead. In C++, even...";
outFile.close;
==============
-------------------
#include <stdio.h>
FILE *outFile;
outFile=fopen("c:\\autoexec.bat", "r");
/* the second has to be mode; r for read, b for binary... it can be a combo like "w" */
fprintf(outFile, "Hi, your autoexec.bat is dead.\n");
fclose(outFile);
-----------------
or this (C++) :
-----------------
#include <fstream.h>
fstream outFile;
outFile.open("c:\\autoexec.bat", ios::out);
outFile << Your autoexec.bat is dead. In C++, even...";
outFile.close;
==============
If I read abe_bcs''s post right, shouldn''t the file opening mode on the first example be "w"? Right now it is set up to read... Anyway, if you want a nice wrapper class for the C style FILE functions, email me. I wrote it myself (and everyone snickers: THAT''S a major accomplishment!)
--------------------
You are not a real programmer until you end all your sentences with semicolons;
--------------------
You are not a real programmer until you end all your sentences with semicolons;
Visit the ROAD Programming Website for more programming help.
quote:Original post by RoosterJM
#include // I/O stream
#include // filestream header
void main()
{
ostream Output; // Variable...
istream Input;
input.open("C:\file.in"); // open the input file
output.open("C:\file.out"); // open the output file
//then read from the input like this
input >> variable;
//then write to output like this...
output << variable;
//then you MUST close the files
input.close();
output.close();
return;
}
Actually, remember that you don''t always have to explicitly close() streams, as they close when they go out of scope
Depending on compilier...in VC++ they close, but in borland (yech!) they dont...
anywho...its a good habit..
anywho...its a good habit..
quote:Original post by RoosterJM
Depending on compilier...in VC++ they close, but in borland (yech!) they dont...
anywho...its a good habit..
Then I think that means Borland''s streams are broken I guess you could consider it good practice to close them yourself, but then that is the whole idea of objects... you can open a file with the constructor and the destructor should close it. *sigh*
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement