第1个回答 2020-04-11
//你的这个程序是错的,做如下修改,
///fscanf()读取出来的都是字符串,因此不能直接给int类型,不然数据就出现不是你写入txt//中的数据,要把字符串变成数字才行,一下就是数字字符串变成数字atoi()
//用法如下:a="4560"
//int b=atoi(a)
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
int read(int arr[])
{
int i=0,n;
char name[100];
FILE *fp;//定义文件指针
if((fp=fopen("data.txt","r"))==NULL)//如果文件名不存在
{printf("cantfind the file!");}//则输出没有找到文件
while(!feof(fp))//判断文件是否结束
{
fscanf(fp,"%s",name);//逐个将文件中的数据放入字符串中
arr[i++]=atoi(name);//把字符串转变成数字(int)类型
printf("%d\n",arr[i-1]);
}
n=i;//n为数组中数据个数
fclose(fp);//关闭文件
return n;//返回n即数据个数的值
}
void main()
{
int arr[100];
read(arr);
}