|
|
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. | Hello! This article descibes the basics of using < fstream> for file input and output. It won't go into the more advanced aspects of the topic, but is meant as an introduction to file input and output. Some C/C++ experience is expected.
#include < fstream> //This is the library that is included
#include < iostream>
using namespace std;
int main ()
{
fstream f1 ("Sasha.txt");
return 0;
}
What this does is create a variable f1 and sets it to "Sasha.txt." Now, let's set this variable to a string.
#include < fstream>
#include < iostream>
#include < string>
using namespace std;
int main ()
{
string dude = "Sasha.txt";
fstream f1 (dude.c_str);
return 0;
}
It should also be noted that if you want a file ONLY for input or ONLY for output you can do this:
#include < fstream>
#include < iostream>
#include < string>
using namespace std;
int main ()
{
string dude = "Sasha.txt";
ifstream f1 (dude.c_str); //Only input
ofstream f1 ("someFile.txt"); //Only output
return 0;
}
Now, let's move on to reading and writing files. The following code will read every character from file "someFile.txt" (This code will also output it.)
#include < fstream>
#include < iostream>
#include < string>
using namespace std;
int main ()
{
fstream f1 ("someFile.txt");
char yeah;
while (f1 >> yeah)
cout << yeah;
return 0;
}
The right hand operator works for input. Now for ouput...
#include < fstream>
#include < iostream>
#include < string>
using namespace std;
int main ()
{
fstream f1 ("someFile.txt");
char yeah = 'A';
for (int i = 0; i < 20; i++)
f1 << yeah;
return 0;
}
This will out the character 'A' multiple times. (20 to be exand.) As you can tell, the left hand operator is used for outputing. Now, I will cover another of inputing which will input spaces. I will show this using a simple encryption program. Th line you need to pay attention to is the first for loop with "f1.get(readTo)"
#include < fstream>
#include < iostream>
#include < vector>
using namespace std;
int main ()
{
fstream f1 ("yeah.txt");
vector < char> yeah;
char readTo;
yeah.push_back (1);
for (int i = 0; f1.get(readTo); i++, yeah.push_back (1))
{
yeah [i] = ~(readTo);
cout << yeah[i];
}
cout << endl;
for (int j = 0; j < i; j ++)
{
cout << (char) (~(yeah [j]));
}
return 0;
}
Well, that's it! Hope you enjoyed this! | |
Other 11 submission(s) by this author
|
|
|
Report Bad Submission |
|
|
Your Vote! |
See Voting Log |
|
Other User Comments |
11/25/2003 4:58:19 PM: not bad :)
|
|
Add Your Feedback! |
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. |
|