ios::in | ios::out error
-
#include <iostream> #include <fstream> using namespace std; int main() { fstream file("c:\\test.txt",ios::in | ios::out | ios::binary); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; } return 0; } I wonder why this code is giving me the output "Error" unexpectedly...what went wrong in this code. I'm not able to write texts in file..please help
-
#include <iostream> #include <fstream> using namespace std; int main() { fstream file("c:\\test.txt",ios::in | ios::out | ios::binary); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; } return 0; } I wonder why this code is giving me the output "Error" unexpectedly...what went wrong in this code. I'm not able to write texts in file..please help
As you may check, opening the file with
ios::in
flag set, fails if the file doesn't exist. :)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() { fstream file("c:\\test.txt",ios::in | ios::out | ios::binary); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; } return 0; } I wonder why this code is giving me the output "Error" unexpectedly...what went wrong in this code. I'm not able to write texts in file..please help
Member 6910300 wrote:
I'm not able to write texts in file..please help
Does the file exist?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Member 6910300 wrote:
I'm not able to write texts in file..please help
Does the file exist?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
no the file is expected to be created. in the following case also it outputs "Error" fstream file("c:\\test.txt",ios::out | ios::in); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; }
-
no the file is expected to be created. in the following case also it outputs "Error" fstream file("c:\\test.txt",ios::out | ios::in); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; }
Then you should use:
ofstream file("c:\\test.txt", ios::binary);
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Then you should use:
ofstream file("c:\\test.txt", ios::binary);
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
thanks for the answer but i dont want to use ofstream and ifstream class instead i want to use fstream class only. Is it possible? and i also need the file to be created as using ofstream.
-
#include <iostream> #include <fstream> using namespace std; int main() { fstream file("c:\\test.txt",ios::in | ios::out | ios::binary); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; } return 0; } I wonder why this code is giving me the output "Error" unexpectedly...what went wrong in this code. I'm not able to write texts in file..please help
maybe your file doesn't exist.check your file. I check your code, is ok. Calvin
-
thanks for the answer but i dont want to use ofstream and ifstream class instead i want to use fstream class only. Is it possible? and i also need the file to be created as using ofstream.
if the file does not exist, you have to use
fstream file("c:\\test.txt", ios::out | ios::binary);
:)
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] -
if the file does not exist, you have to use
fstream file("c:\\test.txt", ios::out | ios::binary);
:)
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]what's problem with this bit masking
ios::in | ios::out | ios::binary
since we have specified out in the mask, why the file is not being created. thanks for replying. -
what's problem with this bit masking
ios::in | ios::out | ios::binary
since we have specified out in the mask, why the file is not being created. thanks for replying.The problem is you can't read from a not exitsting file, I suppose. Your bit mask works if the file do exist. On the other hand, if the file does not exist you should use the
ios::out | ios::binary
mask. :)
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]