Fstream not creating file
May 16, 2013 at 1:07am UTC
Hi,
I am writing code to read a specific line in a file. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
fstream myfile("file.txt" );
myfile << "1 \n 2 \n 3\n 4\n 5\n 6\n 7\n 8\n" ;
myfile.open("file.txt" );
string line;
for (int lineno = 0; getline (myfile,line) && lineno < 7; lineno++)
if (lineno == 6)
cout << line << endl;
myfile.close();
return 0;
}
The problem is that the program is not creating file.txt. Please help!
May 16, 2013 at 1:35am UTC
Is the program actually building?
It looks like you're missing a header file to me: <string>
This is where getline() lives!
Andy
May 16, 2013 at 1:48am UTC
std::string is from <string>
std::getline is the least of your worries
Last edited on May 16, 2013 at 1:49am UTC
May 16, 2013 at 2:38am UTC
YUp, just changed that, but it still does not make the file
May 16, 2013 at 6:27am UTC
By default, the file is opened in input/output mode. If the file doesn't already exist, that will fail.
Instead, open it just in output mode the first time.
Remember to close the file before re-opening in input mode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream myfile("file.txt" , ios::out);
myfile << "1 \n 2 \n 3\n 4\n 5\n 6\n 7\n 8\n" ;
myfile.close();
myfile.open("file.txt" , ios::in);
string line;
for (int lineno = 0; getline (myfile,line) && lineno < 7; lineno++)
if (lineno == 6)
cout << line << endl;
myfile.close();
return 0;
}
Last edited on May 16, 2013 at 6:28am UTC
May 16, 2013 at 11:23am UTC
You can open a fstream in in/out mode it you you are happy to truncate the existing file...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream myfile("file.txt" , ios::in | ios::out | ios::trunc);
myfile << "1 \n 2 \n 3\n 4\n 5\n 6\n 7\n 8\n" ;
myfile.close(); // remember to close
myfile.open("file.txt" ); // ios::in | ios::out by default
string line;
for (int lineno = 0; getline (myfile,line) && lineno < 7; lineno++)
if (lineno == 6)
cout << line << endl;
myfile.close();
return 0;
}
Note that if you don't close before you re-open, the file pointer is still at the end of the file. The second open actually does nothing (the file is already open) and the read starts from the end of the file (and sees nothing).
Rather than close and open you can just move the the file pointer back to the start of the file, with fstream::seek()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream myfile("file.txt" , ios::in | ios::out | ios::trunc);
myfile << "1 \n 2 \n 3\n 4\n 5\n 6\n 7\n 8\n" ;
myfile.seekg(ios::beg); // move file pointer back to start
string line;
for (int lineno = 0; getline (myfile,line) && lineno < 7; lineno++)
if (lineno == 6)
cout << line << endl;
myfile.close();
return 0;
}
But in this case I'd prob take a similar approach to Chervil, but with ofstream and ifstream rather than the open flags.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// the anonymous scopes force destruction at the required points, to close file
int main()
{
const char filename[] = "file.txt" ;
{
ofstream ofs(filename);
ofs << "1 \n 2 \n 3\n 4\n 5\n 6\n 7\n 8\n" ;
}
{
ifstream ifs(filename);
string line;
for (int lineno = 0; getline(ifs, line) && lineno < 7; ++lineno)
if (lineno == 6)
cout << line << endl;
}
return 0;
}
Andy
Last edited on May 16, 2013 at 11:26am UTC
May 17, 2013 at 2:06am UTC
Thank you Chervil! Helped me again :D!
Topic archived. No new replies allowed.