Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读取和打印文件中的值_C++_Ifstream_Eof - Fatal编程技术网

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?)。