File input output

Started by
5 comments, last by t2sherm 23 years, 11 months ago
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 ô¿ô
t2sherm ô¿ô
Advertisement
try this...you''re prolly doing a windows-ish program, but you should be able to adapt this....

#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....
Why does the Air Force need expensive new bombers? Have the people we've been bombing over the years been complaining? -George Wallace To make mistakes is human; to stumble is commonplace; to be able to laugh at yourself is maturity. -William Arthur Ward
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;
==============
/ // / |< <-
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;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

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

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

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..
Why does the Air Force need expensive new bombers? Have the people we've been bombing over the years been complaining? -George Wallace To make mistakes is human; to stumble is commonplace; to be able to laugh at yourself is maturity. -William Arthur Ward
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