Troublesome Handling of Files in C++
-
#include <iostream> #include <fstream> using namespace std; int main() { char data[5];//={'a','b','c','f','\0'}; //fstream file("c:\\test.txt",ios::out | ios::binary); fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); cout<<data; } file.close(); } else { cout<<"Error"; } return 0; }
I've written "abcf" to text.txt file in binary mode. i'm reading back the data into the variable "data". why the heck the output is "abcfabcf". I should be "abcf" but what happened to this one. and another question do anybody knows how to write and read string data-type in binary mode. please help. -
#include <iostream> #include <fstream> using namespace std; int main() { char data[5];//={'a','b','c','f','\0'}; //fstream file("c:\\test.txt",ios::out | ios::binary); fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); cout<<data; } file.close(); } else { cout<<"Error"; } return 0; }
I've written "abcf" to text.txt file in binary mode. i'm reading back the data into the variable "data". why the heck the output is "abcfabcf". I should be "abcf" but what happened to this one. and another question do anybody knows how to write and read string data-type in binary mode. please help.This works for me (error handling left to the reader)
#include <iostream>
#include <fstream>
using namespace std;int main() {
// write some characters in binary mode
fstream file("C:\\test.txt", ios::out | ios::binary);
char out[]="hello";
file.write(out, sizeof(out));
file.close();// read the written characters
file.open("C:\\test.txt", ios::in | ios::binary);
char in[sizeof(out)];
file.read(in, sizeof(in));
cout << out << ", " << in << endl;
file.close();return 0;
}If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
#include <iostream> #include <fstream> using namespace std; int main() { char data[5];//={'a','b','c','f','\0'}; //fstream file("c:\\test.txt",ios::out | ios::binary); fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); cout<<data; } file.close(); } else { cout<<"Error"; } return 0; }
I've written "abcf" to text.txt file in binary mode. i'm reading back the data into the variable "data". why the heck the output is "abcfabcf". I should be "abcf" but what happened to this one. and another question do anybody knows how to write and read string data-type in binary mode. please help.What's wrong with just using std::string and using the library provided operators, like here?
fstream file("c:\\test.txt",ios::in | ios::binary);
if(file.is_open())
{
std::string s;
file >> s;
std::cout << s << std::endl;
file.close();
}
else
{
cout<<"Error";
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!
modified on Wednesday, February 3, 2010 8:27 AM
-
What's wrong with just using std::string and using the library provided operators, like here?
fstream file("c:\\test.txt",ios::in | ios::binary);
if(file.is_open())
{
std::string s;
file >> s;
std::cout << s << std::endl;
file.close();
}
else
{
cout<<"Error";
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!
modified on Wednesday, February 3, 2010 8:27 AM
The format... :-D On the serious side, while there is, of course, nothing wrong in your approach, there is, as well, nothing wrong in his one. Disclaimer: this is going on my... :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
The format... :-D On the serious side, while there is, of course, nothing wrong in your approach, there is, as well, nothing wrong in his one. Disclaimer: this is going on my... :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]I always like using idiomatic C++ rather than not :-)
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!
-
#include <iostream> #include <fstream> using namespace std; int main() { char data[5];//={'a','b','c','f','\0'}; //fstream file("c:\\test.txt",ios::out | ios::binary); fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); cout<<data; } file.close(); } else { cout<<"Error"; } return 0; }
I've written "abcf" to text.txt file in binary mode. i'm reading back the data into the variable "data". why the heck the output is "abcfabcf". I should be "abcf" but what happened to this one. and another question do anybody knows how to write and read string data-type in binary mode. please help.fstream::eof() - returns true if the 'eofbit' has been set by a previous i/o operation. fstream::read(s, n) - sets 'eofbit' if the End-of-File is reached before n characters have been read. The file has only 5 characters and the while loop will execute twice. In the first pass you try to read 5 bytes and it will be success. In the second pass read will fail and sets the 'eofbit'. Since you are not checking how many bytes read, you cout the old buffer contents again. This is the reason why you got "abcfabcf". The code can be changed to print "abcf" once, by checking the bytes read using fstream::gcount() as ................. if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); if(file.gcount()) cout<<data; } file.close(); } ..................