C++ 怎么用文件流读取文件中的整型数据,并在接下来的运算中使用这些数据参与运算?

我接下来要进行各种排序运算。

我是时光の痕
因为被选为推荐答案了,所以不能改了,只能匿名继续回答
上面那个答案用的是c语言风格输入方式
c++的输入流是这样的:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main()
{
ifstream file1("d:\\1.txt");
int a;
file1 >> a;
system("pause");
}

具体怎么用请参考百科
http://baike.baidu.com/view/1679747.htm

!!!!!楼主如果要选最佳答案的话千万要选我的主号时光の痕的啊~
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-10-24
#include <stdio.h>
#include <iostream>
#include <stdlib.h>

int main()
{
FILE *fp = fopen("D:\\1.txt","r");
int a;
fscanf(fp,"%d",&a);
fclose(fp);
system("pause");
}

D:\1.txt文件:
123412

a就成123412了
fscanf()和scanf()的格式一样本回答被提问者和网友采纳
第2个回答  2012-07-05
先读出来,然后使用atoi进行类型转换
第3个回答  2012-07-05
一种读取方法:
theString="\\bin\\Record.txt";
fstream infile2;
infile2.open(theString);
while(infile2)
{
int size = sizeof(buffer)/sizeof(char);
memset(buffer,0,size);
infile2.getline(buffer,size);
string strTemp = buffer;
CString CStrTemp="";
CStrTemp.Format("%s",strTemp.c_str());
nCount = atoi(CStrTemp);
}
相似回答