problem with ofstream


In the following code. i couldnt write into the file "ride.out".
Please help. I donno wat but


#include<iostream>
#include<fstream>
using namespace std;
#include<string.h>
#include<conio.h>
int main()
{
ofstream fout("ride.out");
ifstream fin("ride.in");
string a,b;
while(!fin.eof())
{
getline(fin,a);
getline(fin,b);
}
int c=0,c1=0;
for(int i=0;i<a.size();i++)
c=(c+(int)a[i]-64)%47;
for(int i=0;i<a.size();i++)
c1=(c1+(int)a[i]-64)%47;
cout<<c<<c1;

if(c==c1)
{
fout << "GO";
}
else
// fprintf(fout,"STAY\n");
fout<<"STAY\n";
cin>>a;
getch();
exit(0);
}
Last edited on
Let me ask this: What would happen if u tried to open something u know to be a video with a text file program like wordpad ? It's a similar issue here.

.in and .out are not standard extensions for string output. Make it easy and use .txt
Last edited on
It's not based on the file extensions. Maybe you didn't fout.open() and fin.open()?
Also don't loop on fin.eof(), loop on fin.good() or on getline.
Last edited on
don't loop on fin.eof(), loop on fin.good() or on getline.

This is probably your problem. I don't know what the string value will be if getline fails. Is it implementation defined? After your while loop at least one of the calls to getline will have failed so it could be that your strings contain a value that you don't expect.

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5

Last edited on
Topic archived. No new replies allowed.