在C++语言中,怎样才能读取另一个文件的数值传递给本文件的一个变量?举个例子

如题所述

第1个回答  2013-07-30
一个变量只能定义一次,但可以多次声明。
在你要用到此变量的地方用extern声明即可
例如:
//A.cpp
int a;
//B.cpp
exterin int a;
此后就可以使用该变量了
第2个回答  2013-07-30
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void main()
{
//创建文件,输入点内容来测试
string str="Hello World";
ofstream fp;
fp.open("temp.txt");
fp<<str;
fp.close();

//读取文件内容
string temp;
ifstream ftemp;
ftemp.open("temp.txt",ios::in);
ftemp>>temp;
cout<<temp<<' '<<temp<<endl;
ftemp.close();
}本回答被网友采纳
相似回答