#include
int main(void)
{
FILE *fp=fopen("a.txt","r");
int a[10][3];
int i,j=0,x=2,y=3,rows;
fscanf(fp,"%*[^\n]%*c");//忽略标题行
for (i=1;i
rows=y-x+1;//计算要读取的行数
for (i=0;i
fscanf(fp,"%d",&x);//跳过每行第一个数字(即学生编号)
for (x=0;x<3;x++)//分别读取其余三个成绩
fscanf(fp,"%d",&a[j][x]);
j++;
}
fclose(fp);//关闭文件
for (i=0;i
for (j=0;j<3;j++)
printf("%d " ,a[i][j]);
putchar('\n');
}
return 0;
}
C语言读取文件中的TXT数据,参考代码如下:
读文件中的TXT数据:
#include
#include
void main()
{
FILE* EMFile = fopen("D://EMFile.TXT","r");
if (!EMFile) {
cout<<"FILE NOT FOUND!";
return;
}
int XN=2;//假设文件有两行,三列
int YN=3;
float** Ef;
Ef = new float*[XN];
for(int idx=0;idx{
Ef[idx] = new float [YN];
}
for(int i=0;i{
for(int j=0;jEf[i][j] = 0.0 ;
}
for(int xn=0;xn{
for(int yn=0;yn{
fscanf(EMFile,"%f",&Ef[xn][yn]);//假如是科学计数法写成%e或者%le
}
}
}
#include
#include
#define FILE_PATH "stu.txt"
int func(int **a)
{
FILE *fdr;
char buf[256], temp[3];
int i, j;
if((fdr = fopen(FILE_PATH, "r")) == NULL)return -1;
fgets(buf, 256, fdr); //跳过第一行
i = 0;
while(!feof(fdr))
{
fscanf(fdr, "%s", temp); //跳过编号
for(j = 0; j < 3; j++) fscanf(fdr, "%d", &(a[i][j]));
i++;
}
fclose(fdr);
return 1;
}
如果能确定只有第一行要去掉,可以
FILE *fp = fopen("stu.txt", "r");
char buffer[128];
fgets(buffer, sizeof(buffer), fp);
int score[128][4];
int stu_count = 0;
while (4 == fscanf(fp, "%d%d%d%d", &score[stu_count][0], &score[stu_count][1], &score[stu_count][2], &score[stu_count][3]) )
{
stu_count++;
}
fclose(fp);
第一行要去掉
学号这一列的数据也不要
FILE *fp = fopen("stu.txt", "r");
char buffer[128];
fgets(buffer, sizeof(buffer), fp);
int score[128][3];
int stu_count = 0;
while (4 == fscanf(fp, "%*d%d%d%d", &score[stu_count][0], &score[stu_count][1], &score[stu_count][2],)
{
stu_count++;
}
fclose(fp);