从两个不同的文件获取输入时,输入会发生变化
The input changes when taking input from two different files
我从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 :)
相关文章:
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何从用户那里获取输入并将其存储在向量中?
- 获取通过重定向传入的输入文件的名称?
- Cryptopp:获取密码输入的填充字符串
- 从格式化输入操作中获取读取字符数
- 如何获取用户输入的算术表达式,如 5-8+7*4-8+9 或 1+5?
- 我无法从 C# 获取 PInvoke 的输入参数以C++ DLL 以用作 IntPtr 的输出
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 用于获取枚举值的用户输入
- C++如何使用表达式获取用户输入?
- 从 cin 获取 c++ 中结构中多个枚举的输入
- 代码在执行之前终止,并且没有从用户那里获取输入
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 推荐一种在未输入获取行分隔符时引发异常的方法?
- 输入获取线的结尾
- 线程不刷新数据,无法从标准输入获取所有数据
- 如何从同一输入获取两个输入 (C++)
- 在C++中静态地将用户输入获取到类中
- 在使用C++字符串时对特定输入获取bad_alloc
- 使用MPI程序,所有进程都从用户输入获取数据