// basic file operations
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
// OPEN A TEXT FILE AND ENTER DATA
ofstream myfile;
myfile.open("example.txt");
if (myfile.is_open())
{
myfile << "Writing this to a file.\n";
myfile << "Writing second line to file.\n";
myfile << "Writing third line to file.\n";
}
else cout << "Unable to open file!" << endl;
myfile.close();
// READ A TEXT FILE AND CLOSE FILE AFTER READING
string line;
ifstream myfile;
myfile.open("example.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
getline(cin, line);
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file.\n";
return 0;
}
// basic file operations
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int pause; // Global variable for pause usage
int main()
{
// OPEN A TEXT FILE AND ENTER DATA
ofstream output_file;
output_file.open("example.txt");
if (output_file.is_open())
{
output_file << "Writing this to a file.\n";
output_file << "Writing second line to file.\n";
output_file << "Writing third line to file.\n";
}
else cout << "Unable to open file!" << endl;
//output_file.close(); <----- Closed at end of program
// READ A TEXT FILE AND CLOSE FILE AFTER READING
string line;
ifstream input_file;
input_file.open("example.txt");
if (input_file.is_open())
{
while (!input_file.eof())
{
getline(cin, line);
cout << line << '\n';
}
//input_file.close(); <----- Closed at end of program
}
else cout << "Unable to open file.\n";
// Close files
output_file.close();
input_file.close();
// Universal pause usage, similar to Window's style
// Window's version: System("pause");
cin >> pause; // System("PAUSE");
return 0;
}
Changed 'myfile' to 'output_file' and 'input_file'.
Give it a shot.
I changed "myfile" to "output_file" and "input_file" and no longer get an error message. The program ran. However, I now have a problem with the getline function. I changed it to getline(input_file, line) and it does not print any lines.
// Basic Input/Output Operations
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int pause; // Global variable for pause usage
int main()
{
//-----------------------------------------------------------------------------
// OPEN A TEXT FILE AND ENTER DATA
ofstream output_file;
output_file.open("example.txt", ios::beg | ios::trunc); // To file
if (output_file.is_open())
{
output_file << "Writing this to a file.\n";
output_file << "Writing second line to file.\n";
output_file << "Writing third line to file.\n";
}
else cout << "Unable to open file!" << endl; // Error message
output_file.close(); // Close file
//-----------------------------------------------------------------------------
// READ A TEXT FILE AND CLOSE FILE AFTER READING
string line;
ifstream input_file;
input_file.open("example.txt", ios::beg); // From file
cout << "Coming from file..." << endl << endl;
if (input_file.is_open())
{
while(input_file.good() && !input_file.eof()) // Error check
{
getline(input_file, line);
cout << line << '\n';
}
}//end if(file.open)
else cout << "Unable to open file.\n"; // Error message
input_file.close(); // Close file
//-----------------------------------------------------------------------------
// Universal pause usage, similar to Window's style
// Window's version: System("pause");
cin >> pause; // System("PAUSE");
return 0; // Success
}
Notes
I moved the closing of the files back to their original positions, I forgot that you were using the same file. I added, within your file openings, that the fstream read from the beginning on both, and for the output to file that it trunc, which means to delete the file, and re-write it (being a basic program, it shouldn't matter). As for the reading from file, I believe since you are reading from the same file, it left the flag at the end of the file, so you were reading in blank lines.
I put in error checking for the file at this line:
It's good programming practice to do so, checking if the file is good and not at the end.
A few other changes in formatting to make it pretty and it should run smoothly.
a. I did not use ios::beg because it is not one of the modes in the fstream::open documentation.
b. I did not use ios::trunc either. If there was data in the file and I ran the program again, the existing data would be overwritten. There is a case for using ios::trunk; but,, for my situation, it was not applicable.
2. output_file.close(); // Close file I think this is why getline was not working. output_file and input_file were opened at the same time. When out_put file was closed, getline worked.
3. input_file.open("example.txt", ios::beg);
a. Similar to 1.a above, I did not use ios::beg for the same reason.
4. while(input_file.good() && !input_file.eof())
a. I do not see the value in adding input_file.good(). !input_file.eof() controls the Boolean expression logic.