簡體   English   中英

讀取和打印文件中的值

[英]Reading and printing values in a 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;
    }

在調試時,我得到了code2的垃圾值。 因此,循環不會在while結束時結束。

您對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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM