24

Hey everyone, I have just started to learn C++ and I wanted to know how to read and write to a text file. I have seen many examples but they have all been hard to understand/follow and they have all varied. I was hoping that someone here could help. I am a total beginner so I need clear instructions. Here is an example of what i'm trying to do:

#include <iostream> #include <fstream> using namespace std; string usreq, usr, yn, usrenter; int start () { cout << "Welcome..." int main () { cout << "Is this your first time using TEST" << endl; cin >> yn; if (yn == "y") { ofstream iusrfile; ofstream ousrfile; iusrfile.open("usrfile.txt", "w"); iusrfile >> usr; cout << iusrfile; iusrfile.close(); cout << "Please type your Username. \n"; cin >> usrenter; if (usrenter == usr) { start (); } } else { cout << "THAT IS NOT A REGISTERED USERNAME."; } return 0; } 
1
  • 4
    Which C++ book are you learning from? Commented May 18, 2011 at 22:52

3 Answers 3

48

Header files needed:

#include <iostream> #include <fstream> 

declare input file stream:

ifstream in("in.txt"); 

declare output file stream:

ofstream out("out.txt"); 

if you want to use variable for a file name, instead of hardcoding it, use this:

string file_name = "my_file.txt"; ifstream in2(file_name.c_str()); 

reading from file into variables (assume file has 2 int variables in):

int num1,num2; in >> num1 >> num2; 

or, reading a line a time from file:

string line; while(getline(in,line)){ //do something with the line } 

write variables back to the file:

out << num1 << num2; 

close the files:

in.close(); out.close(); 
Sign up to request clarification or add additional context in comments.

Comments

5

Default c++ mechanism for file IO is called streams. Streams can be of three flavors: input, output and inputoutput. Input streams act like sources of data. To read data from an input stream you use >> operator:

istream >> my_variable; //This code will read a value from stream into your variable. 

Operator >> acts different for different types. If in the example above my_variable was an int, then a number will be read from the strem, if my_variable was a string, then a word would be read, etc. You can read more then one value from the stream by writing istream >> a >> b >> c; where a, b and c would be your variables.

Output streams act like sink to which you can write your data. To write your data to a stream, use << operator.

ostream << my_variable; //This code will write a value from your variable into stream. 

As with input streams, you can write several values to the stream by writing something like this:

ostream << a << b << c; 

Obviously inputoutput streams can act as both.

In your code sample you use cout and cin stream objects. cout stands for console-output and cin for console-input. Those are predefined streams for interacting with default console.

To interact with files, you need to use ifstream and ofstream types. Similar to cin and cout, ifstream stands for input-file-stream and ofstream stands for output-file-stream.

Your code might look like this:

#include <iostream> #include <fstream> using namespace std; int start() { cout << "Welcome..."; // do fancy stuff return 0; } int main () { string usreq, usr, yn, usrenter; cout << "Is this your first time using TEST" << endl; cin >> yn; if (yn == "y") { ifstream iusrfile; ofstream ousrfile; iusrfile.open("usrfile.txt"); iusrfile >> usr; cout << iusrfile; // I'm not sure what are you trying to do here, perhaps print iusrfile contents? iusrfile.close(); cout << "Please type your Username. \n"; cin >> usrenter; if (usrenter == usr) { start (); } } else { cout << "THAT IS NOT A REGISTERED USERNAME."; } return 0; } 

For further reading you might want to look at c++ I/O reference

Comments

2

To read you should create an instance of ifsteam and not ofstream.

ifstream iusrfile; 

You should open the file in read mode.

iusrfile.open("usrfile.txt", ifstream::in); 

Also this statement is not correct.

cout<<iusrfile; 

If you are trying to print the data you read from the file you should do:

cout<<usr; 

You can read more about ifstream and its API here

3 Comments

ifstreams are always opened in input mode
I agree that there is no need to pass the mode as ifstream::in is default parameter to the ifstream::open, but I think its a good idea to explicitly mention it.
Oh yeah whoops, "ofstream iusrfile" was supposed to be "ifstream iusrfile". And thanks for the help on the cout statement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.