从两个不同的文件获取输入时,输入会发生变化

The input changes when taking input from two different files

本文关键字:输入 获取 变化 文件 两个      更新时间:2023-10-16

我从txt文件(比如a.txt)中获取输入,将其保存在变量中,例如a,我关闭连接。然后我从另一个 txt 文件(比如 b.txt)中获取输入并将其保存在不同的变量中,比如 b ,我关闭连接。现在我在不同的文本文件 (c.txt) 中输出变量。 变量b的输出值会发生变化,并且与输入文本文件 b.txt 中存在的值不同。 我什至尝试使用fflush(标准),但结果没有改变。

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int a,b;
    freopen("a.txt","r",stdin);
    cin>>a;
    fflush(stdin);
    fclose(stdin);
    fflush(stdin);
    freopen("b.txt","r",stdin);
    cin>>b;
    fflush(stdin);
    fclose(stdin);
    fflush(stdin);
    freopen("c.txt","w",stdout);
    cout<<"a= "<<a<<endl<<"b= "<<b<<endl;
    fflush(stdin);
    fclose(stdout);
    fflush(stdin);
    return 0;   
}

尝试使用 fstream.h 库。 例如;

#include<iostream>
//fstream is the library from which we use file input and output resources.
#include<fstream>
using namespace std;
/*
There are three basic operations, you already know about
1. LOAD | we load any thing on which we are supposed to work, into MAIN     MEMORY i.e. RAM
2. PROCESS | any task we are supposed to do on it.
3. STORE | write data back onto STORAGE i.e. HARD DISK
*/
int main()
{
    //*************************INPUT FORM FILE****************************//
    //to get data from file
    //file input variable declaration
    ifstream fin;
    //load the file into memory
    //file.txt must exists in same folder where you placed your cpp file.
    fin.open("file.txt");
    char x[100];
    //tp get data from file.
    fin >> x;
    cout << "Data from file.txt is :: " << x << endl;
    //now as we are done taking input from file.
    //so now we must close the open file.
    fin.close();
    //*************************INPUT FORM FILE****************************//

    cout << "nnn";
    //*************************SAVING DATA INTO FILE | FILE REPACE CODE****************************//
    //to enter a data in a file
    ofstream fout;
    //load the file into memory
    //file.txt must exists in same folder where you placed your cpp file.
    // if file isn;t available then fout will create the file with specified name
    fout.open("file.txt");
    char str[100];
    //to get data from console.
    cout << "Enter some string to write in file.txt (file replace mode)" << endl;
    cin >>str;
    cout << "Writing data into file now." << endl;
    fout << str;
    //now as we are done saving data into file.
    //so now we must close the opened file.
    fout.close();
    //*************************SAVING DATA INTO FILE | FILE REPACE CODE****************************//
     /*
     remember code o fout given above will replace data
     */

    cout << "nnn";
     ////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
    //*************************SAVING DATA INTO FILE | FILE APPEND CODE****************************//
    //to enter a data in a file
    ofstream fout2;
    //load the file into memory
    //file.txt must exists in same folder where you placed your cpp file.
    // if file isn;t available then fout2 will create the file with specified name
    fout2.open("file.txt", ios::app);
    //to get data from console.
    cout << "Enter some string to write in file.txt (Append mode)" << endl;
    cin >> str;
    cout << "Data from console. is " << str << endl;
    cout << "Writing data into file now." << endl;
    fout2 << str;

    cout << "nnn";
    cout << "nnn";
    //now as we are done saving data into file.
    //so now we must close the opened file.
    fout2.close();
    fout2.open("file.txt", ios::app);
    //to get data from console.
    cout << "Enter some string to write in file.txt *(append mode)" << endl;
    cin >> str;
    cout << "Data from console. is " << str << endl;
    cout << "Writing data into file now." << endl;
    fout2 << str;
    //now as we are done saving data into file.
    //so now we must close the opened file.
    fout2.close();
    //*************************SAVING DATA INTO FILE | FILE APPEND CODE****************************//
    return 0;
}
//This code is actually written by a friend of mine , Ammar Tahir :)