txt文件读取问题

来源:百度知道 编辑:UC知道 时间:2024/05/27 05:58:03
比如我有一个TXT文档,有2行,第一行有10个整数,第二行有一个整数;
我要分别读取每一行中的数,第一行写到到数组A[10]中,第二行写到INT型 B中,请大侠指教
一定要C++的代码,C知道的

#include <iostream>
#include <fstream>
using namespace std;
void main()
{
const int N=10;
char filename[10];
cout<<"请输入文件名:";
cin>>filename;
fstream f;
f.open(filename,ios::in);
if(!f)
{
cout<<"无法打开"<<filename<<endl;
abort();
}
int a[N];int b;
for(int i=0;i<N;i++)
f>>a[i];
f>>b;
f.close();
cout<<"数组A:"<<endl;
for(i=0;i<N;i++)
cout<<a[i]<<' ';
cout<<endl;
cout<<"B="<<b<<endl;
}
在VC中调试过了,运行通过
输入文件名时,要连同扩展名一同输入

#include <stdio.h>

FILE *fp=NULL;

char filepath[50],c;
int a[10];
int b,i;

printf("Please enter the filepath");
filepath=gets();

fp=fopen("filepath","r");
if(