I'm having trouble opening files. Below is a simple listing that I can replicate the problem with on my workstation. The program compiles fine but when I run it, the file does not open and I get the exception list from the line if(!myfile). In all my searching on the web I'm seeing that this is the correct way to open a file for read/write in binary mode with append but it fails every time. However, if I compile this code on MSVC++ version 6, it runs correctly. Any ideas would be great.
Thanks,
giljo11778
include <iostream> // standard IO
include <conio.h> // getch
include <fstream> // File system IO access
using namespace std;
int main()
{
fstream myfile;
myfile.open ("count.dat", ios::in | ios::out | ios::binary | ios::app);
if (!myfile)
{
cout << "problem opening file\n";
cout << "end of file\t\t" << myfile.eof() << endl;
cout << "failed to open\t\t" << myfile.fail() << endl;
cout << "bad bit found\t\t" << myfile.bad() << endl;
cout << "everything is fine\t" << myfile.good() << endl;
cout << "any key to cont.\n";
getch();
exit(1);
}
return 0;
}
output from the program:
problem opening file
end of file 0
failed to open 1
bad bit found 0
everything is fine 0
any key to cont.
Ok, problem resolved. I looked at the seekp page and could set it, however, since the file was not set to ios::in I could not read from the file. A little more searching and I found that a file opened with ios::ate also opens with the output pointer at the end of the file but I can use both ios::in and ios::out on that file so I can set the input pointer with seekg() to the top of the file and read back the whole thing.
Thanks for pushing me in the right direction.
giljo11778
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm using DEV-C++ 4.9.9.2
I'm having trouble opening files. Below is a simple listing that I can replicate the problem with on my workstation. The program compiles fine but when I run it, the file does not open and I get the exception list from the line if(!myfile). In all my searching on the web I'm seeing that this is the correct way to open a file for read/write in binary mode with append but it fails every time. However, if I compile this code on MSVC++ version 6, it runs correctly. Any ideas would be great.
Thanks,
giljo11778
include <iostream> // standard IO
include <conio.h> // getch
include <fstream> // File system IO access
using namespace std;
int main()
{
fstream myfile;
myfile.open ("count.dat", ios::in | ios::out | ios::binary | ios::app);
if (!myfile)
{
cout << "problem opening file\n";
cout << "end of file\t\t" << myfile.eof() << endl;
cout << "failed to open\t\t" << myfile.fail() << endl;
cout << "bad bit found\t\t" << myfile.bad() << endl;
cout << "everything is fine\t" << myfile.good() << endl;
cout << "any key to cont.\n";
getch();
exit(1);
}
return 0;
}
output from the program:
problem opening file
end of file 0
failed to open 1
bad bit found 0
everything is fine 0
any key to cont.
Compile log:Compiler: Default compiler
Building Makefile: "C:\Cprojects\BOOP\chapter_12\exercise_1\Makefile.win"
Executing make...
make.exe -f "C:\Cprojects\BOOP\chapter_12\exercise_1\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
g++.exe main.o -o "filing_distnaces.exe" -L"C:/Dev-Cpp/lib"
Execution terminated
Compilation successful
It seems, you can't open a file in append mode and read mode combined.
With ios::out | ios::binary | ios::app, it works fine.
If you need it in read/write mode, you can control the output pointer position with seekp:
http://www.cplusplus.com/reference/iostream/ostream/seekp.html
leo_sc
Ok, problem resolved. I looked at the seekp page and could set it, however, since the file was not set to ios::in I could not read from the file. A little more searching and I found that a file opened with ios::ate also opens with the output pointer at the end of the file but I can use both ios::in and ios::out on that file so I can set the input pointer with seekg() to the top of the file and read back the whole thing.
Thanks for pushing me in the right direction.
giljo11778