I am trying to read a file and then print it but the loop doesn't ends. Why??
My file contains one line such as
66,67,256,258,69,73,
This is my input :
char d;
char code2 [12]={0};
string file1;
cout<<"Input file name"<<endl;
cin>>file1;
string file2;
cout<<"Input file name"<<endl;
cin>>file2;
ifstream input;
input.open(file1.c_str());
ofstream output;
output.open(file2.c_str());
while(! input.eof())
{
int i=0;
while(d != ',' && i < sizeof(code2))
{
input>>d;
code2[i]=d;
i++;
}
file2<<code2;
}
While debugging, I am getting a garbage value of code2. Thus, the loop doesn't ends at end of while.
Your use of eof()
is wrong, and you are using d
before initializing it. Try something more like this instead:
char d;
char code2 [13]={0};
string file1;
cout<<"Input file name"<<endl;
cin>>file1;
string file2;
cout<<"Input file name"<<endl;
cin>>file2;
ifstream input;
input.open(file1.c_str());
ofstream output;
output.open(file2.c_str());
int i = 0;
while(input >> d)
{
if ((d == ',') || (i == 12))
{
code2[i] = 0;
file2<<code2;
i = 0;
}
code2[i] = d;
i++;
}
if (i > 0)
{
code2[i] = 0;
file2<<code2;
}