Consider the following program,
void main ()
{
int productno;
char productname[12];
ofstream fout(product.txt);
for(int i=0; i%26lt;5; i++)
{
cout%26lt;%26lt;"Enter product no %26amp; name \n";
cin%26gt;%26gt;productno%26gt;%26gt;produtname;
fout%26lt;%26lt;productno; cout%26lt;%26lt;"\t"
fout%26lt;%26lt;productname; cout%26lt;%26lt;endl;
}
fout.close ();
}
using this program, we can write product no %26amp; name for 5 products in text file. After that, if i open that file in Windows, the condents of that file will be in unreadable format with some symbols. How can solve this problem. I am using Microsoft Visual C++. Please help me... Thanks in advance....
How can I solve a problem which i am facing in C++ File handling? Please read further...?
I'd be wary of the logic in the program above. It doesn't look helpful to me.
It sounds like you are either writing to a binary file, which I doubt, or you have string problems. Are you typing in product names that are longer than 11 characters? If so, that could easily explain your trouble. Make productname 100 characters long, and see if that solves the problem.
Reply:Sorry I don't have a C++ compiler installed at work (as I work in Java) but give this a try...sorry if there is an error...as I do not have a compiler...but it should be much better then what you were doing there were multiple problems that I saw...I had to break the cin up because yahoo! replaced some of it with "..."
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
#include %26lt;cstdlib%26gt;
using std::ofstream;
using std::namespace;
using std::ios;
void main ()
{
int productno;
char productname[12];
ofstream fout("product.txt", ios::out);
for(int i=0; i%26lt;5; i++)
{
cout%26lt;%26lt;"Enter product no %26amp; name \n";
cin%26gt;%26gt;productno
%26gt;%26gt;productname
fout %26lt;%26lt; productno %26lt;%26lt; '\t' %26lt;%26lt; productname %26lt;%26lt; '\n';
}
return 0;
// Unnecessary because the ofstream destructor closes file fout.close ();
}
Also...as the commenter listed a learyness of logic...I did not change your logic within my code. I just added some basics...I made no logic changes.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment