Reputation:
Trying to read into a file the word, "beef"
Later going and editing the contents of the file to whatever to user wants which will be stored in the string, "line"
The file never displays and when I go to check it manually, the text file is blank.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line = { " " };
ofstream file1;
file1.open("beef.txt");
file1 << "beef" << endl;
file1.close();
file1.open("beef.txt");
if (file1.is_open())
{
cout << "Enter what you would like to be contained in the file" << endl;
cin >> line;
ofstream file1;
file1 << line << endl;
}
return 0;
}
Upvotes: 0
Views: 14401
Reputation: 595377
A std::ofstream
is for output only, you can't read input with it.
A std::ifstream
is for input only, you can't write output with it.
So, you need to either
use separate std::ofstream
and std::ifstream
variables:
int main()
{
ofstream out_file;
ifstream in_file;
string line;
out_file.open("beef.txt", ios_base::trunc);
if (out_file.is_open())
{
out_file << "beef" << endl;
out_file.close();
}
in_file.open("beef.txt");
if (in_file.is_open())
{
getline(in_file, line);
in_file.close();
cout << "File contains:" << endl;
cout << line << endl;
}
cout << "Enter what you would like to be contained in the file" << endl;
getline(cin, line);
out_file.open("beef.txt", ios_base::trunc);
if (out_file.is_open())
{
out_file << line << endl;
out_file.close();
}
in_file.open("beef.txt");
if (in_file.is_open())
{
getline(in_file, line);
in_file.close();
cout << "File now contains:" << endl;
cout << line << endl;
}
return 0;
}
use a single std::fstream
variable, which can be used for both output and input, depending on whether you specify in
and/or out
flags when calling open()
:
int main()
{
fstream file;
string line;
file.open("beef.txt", ios_base::out | ios_base::trunc);
if (file.is_open())
{
file << "beef" << endl;
file.close();
}
file.open("beef.txt", ios_base::in);
if (file.is_open())
{
getline(file, line);
file.close();
cout << "File contains:" << endl;
cout << line << endl;
}
cout << "Enter what you would like to be contained in the file" << endl;
getline(cin, line);
file.open("beef.txt", ios_base::out | ios_base::trunc);
if (file.is_open())
{
file << line << endl;
file.close();
}
file.open("beef.txt", ios_base::in);
if (in_file.is_open())
{
getline(in_file, line);
in_file.close();
cout << "File now contains:" << endl;
cout << line << endl;
}
return 0;
}
Upvotes: 2
Reputation: 36463
In this snippet:
if (file1.is_open())
{
cout << "Enter what you would like to be contained in the file" << endl;
cin >> line;
ofstream file1;
file1 << line << endl;
}
You create another ofstream
object:
ofstream file1;
This shadows the one you had already created before. Moreover, the one you are shadowing now is the object that contains a valid file pointer to "beef.txt"
. Using the new inner-scope file1
doesn't point to any file yet, thus writing to it won't give you any results in "beef.txt"
.
Remove it to use the correct object:
if (file1.is_open())
{
cout << "Enter what you would like to be contained in the file" << endl;
cin >> line;
file1 << line << endl;
}
Upvotes: 2