//假如一txt文件中,有二个数据:50 100,要从文件读取这两个值分别放入相应的变量中,50与100间有空格
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <fstream>
void main()
{
FILE *fp = fopen("try.txt","r");
int buffer[2]={0};//如果是实数,可以修改成float buffer[2]={0};
int i=0;
for (i=0;i<2;i++)
{
fscanf(fp,"%d",&buffer[i]);//如果是实数,把"%d"修改成"%f"
cout<<buffer[i]<<endl;
}
fclose(fp);
}
如果是在MFC对话框中
void CIDcardRecogizeDlg::OnButton3()
{
// TODO: Add your control notification handler code here
FILE *fp = fopen("try.txt","r");
int buffer[2]={0};
int qi=0;
CString strstr;
for (qi=0;qi<2;qi++)
{
fscanf(fp,"%d",&buffer[qi]);
strstr.Format("%d",buffer[qi]);
MessageBox(strstr);
}
fclose(fp);
}