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

时间:2016-02-04 17:11:48

标签: c++

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

#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;   
}

1 个答案:

答案 0 :(得分: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 << "\n\n\n";

    //*************************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 << "\n\n\n";

     ////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////

    //*************************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 << "\n\n\n";
    cout << "\n\n\n";
    //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 :) 
相关问题