来自 txt 文件的输入在第二次读取/传递时不匹配 (C++)

Input from txt file doesn't match when it is read/passed second time (C++)

本文关键字:不匹配 C++ 读取 文件 txt 输入 第二次 来自      更新时间:2023-10-16

所以我是编码和C 的新手。所以...对不起,如果这对你们中的某些人来说很明显:

我很难获得此代码以第二次读取存储在TXT文件中的输入。它首次工作。

void readcode(ifstream& infile, int list[], int& length, bool& lenCodeOk)
{
    int count;
    lenCodeOk = true;
    infile >> length; //get the length of the secret code
    cout << "Length1 is "<<length<<endl;

    if (length > MAX_CODE_SIZE)
    {
            lenCodeOk = false;
            return;
    }
            //Get the secret code.
    for (count = 0; count < length; count++)
    {
            infile >> list[count];
    }
    cout<<" Code recorded is: ";
    for (count = 0; count < length; count++)
    {
            cout<<list[count]<<" "<<endl;
    }
}

因此,我的txt文件中的第一个整数是整数序列的长度。此功能可以完成应有的一切。正确读取长度也将所有内容存储在数组中。

但是,当我调用readCode函数后调用下面的比较函数时,它带有完全随机/不同的 length2 (每次调用时都会更改/up up)和数组元素。

void compareCode(ifstream& infile, ofstream& outfile, const int list[], int length)
{
    int length2;
    int digit;
    bool codeOk;
    int count;
    codeOk = true;
    infile >> length2;
    cout<<"Length2 is "<<length<<endl;

    if(length != length)
    {
            cout<< "The original code and its copy are not of the same length"<<endl;
            return;
    }

    outfile << "Code Digit    Code Digit Copy"<<endl;
    for (count= 0; count<length; count++)
    {
            infile >> digit;
            outfile<<setw(5)<<list[count]<<setw(17)<<digit;
            if (digit != list[count])
            {
                    outfile << " Code digits are not the same"<<endl;
                    codeOk = false;
            }
            else
            {
                    outfile<<endl;
    }
            if (codeOk)
            {
                    outfile<<"Message transmitted OK."<<endl;
            }
            else
            {
                    outfile<<"Error in transmission. "<<"Retransmit!!"<<endl;
              }
    }
}

似乎我缺少有关传递Fstream变量的重要信息,如果有人将我指向正确的方向,我将非常感谢。

int main()
{
    int codeArray[MAX_CODE_SIZE];//Array to store the secret code
    int codeLength;// Variable to store the length of the code
    int codeLength2;
    bool lengthCodeOk;//Variable to indicate if the legth of the secret code is less than or equal to 250
    ifstream incode;// Declare ifstream variable
    ofstream outcode;//Declare ofstream variable
    char inputFile[51];//Variable to store the name of the input file
    char outputFile[51]; //Variable to store the name of the output file
    cout<<"Enter the input file name: "<<endl;
    cin>> inputFile;//makesure it is one word because ci.>>skips white space
    cout<<endl;
    incode.open(inputFile);
    if(!incode)
    {
            cout<< "Cannot open the input file."<<endl;
            return 1;
    }
    cout<< "Enter the output file name: " ;
    cin>> outputFile;//Same deal, one word so cin>> can read
    cout<<endl;

    outcode.open(outputFile);
    readcode(incode,codeArray,codeLength,lengthCodeOk);
    if  (lengthCodeOk)
    {
            compareCode(incode,outcode,codeArray,codeLength);
    }
    else
    {
            cout<<"Length of the secret code must be <= "<<MAX_CODE_SIZE<<endl;
    }
    incode.close();
    outcode.close();
    return 0;
 }

这应该像这样:

istream& readcode(istream& infile, std::vector<int>& list)
{
    int length;
    if (infile >> length) {
        list.reserve(length);
        for (int count = 0; count < length; count++)
        {
            int x;
            if (infile >> x)
            {
                list.push_back(x);
            } else {
                break;
            }
        }
    }
    return infile;
}
ifstream file("somefile.txt");
if (file.good())
{
    std::vector<int> a, b;
    if (readcode(file, a).good())
    {
        file.seekg(0);
        if (readcode(file, b).good())
        {
            cout << (a!=b? "code reading are different" : "code reading are same") << endl;
        }
        else
        {
            cerr << "failed read again code" << endl;
        }
    }
    else
    {
        cerr << "failed read code" << endl;
    }
}