C++ 读取和打印文件中的值
我试图读取一个文件,然后打印它,但循环没有结束。为什么 我的文件包含一行,例如C++ 读取和打印文件中的值,c++,ifstream,eof,C++,Ifstream,Eof,我试图读取一个文件,然后打印它,但循环没有结束。为什么 我的文件包含一行,例如 66,67,256,258,69,73, 这是我的意见: char d; char code2 [12]={0}; string file1; cout<<"Input file name"<<endl; cin>>file1; string file2; cout<<"Input file name"<<endl; cin>>file
66,67,256,258,69,73,
这是我的意见:
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;
}
chard;
字符代码2[12]={0};
字符串文件1;
cout您使用的eof()
是错误的,您在初始化它之前使用的是d
。请尝试类似以下内容:
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;
}
chard;
字符代码2[13]={0};
字符串文件1;
不能使用eof
。关于这个话题,有无数的问题和答案。搜索:考虑“<代码> d==”、“<代码> >从嵌套循环的顶部开始时发生的情况(提示:<代码>输入< /代码>应该如何达到EOF?)。