今天给各位分享c语言读取文件数组的知识,其中也会对c语言从文件中读取数据存入数组进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、c语言将文件内容读入数组中2、C语言如何从文件读入数组3、c语言如何读取txt内容到数组?4、c语言 如何读取txt内容到数组5、C语言从文件读写数组6、c语言读取文件然后保存到数组中
c语言将文件内容读入数组中
#include stdio.h
int main(int ac, char *pav[])
{
if (ac!=2){
printf(“程序名 要读取的文件名\n”);
printf(“test.exe 1.txt\n”);
return 0;
}
FILE *fp = fopen(pav[1],”r+”);
if(fp == 0)
{
printf(“fail to open the file1 !\n”);
return 0;
}
char buf[256] = {0};
while(!feof(fp))//判断文件是否已结束
{
fgets(buf,256,fp);
}
printf(buf);
return 0;
}
C语言如何从文件读入数组
主要语句如下:
FILE *fin;
int arr1[3],arr2[3];
int i=3;
if ( (fin = fopen(“a.in”,”r”) ) == NULL ) {
printf(“\007Cann’t open input file a.in “);
exit(0);
};
for (i=0;i3;i++)
fscanf(fin,”%d %d”, arr1[i], arr2[i]);
fclose(fin);
c语言如何读取txt内容到数组?
一、编程思路。1 以文本方式打开文件。2 循环用fscanf格式化输入数据到数组。3 判断fscanf的返回值,如果显示到达文件结尾,退出输入。4 关闭文件。5 使用数据。二、代码实现。设定文件名为in.txt, 存有一系列整型数据,以空格或换行分隔。代码可以写作:
#include stdio.hint main(){ int v[100];//开一个足够大的数组。 int i = 0, j; FILE *fp;//文件指针 fp = fopen(“in.txt”, “r”);//以文本方式打开文件。 if(fp == NULL) //打开文件出错。 return -1; while(fscanf(fp, “%d”, v[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF) i++; fclose(fp);//关闭文件 for(j = 0; j i; j ++)//循环输出数组元素。 { printf(“%d “, v[j]); } return 0;}当文件内容为:1 35 6 8 9 9 10 123 3476 54 98程序输出:1 35 6 8 9 9 10 123 34 76 54 98
c语言 如何读取txt内容到数组
#includestdio.h
#includestdlib.h
#define N 10000
int main()
{
FILE *fp;
if((fp=fopen(“D:\\123.txt”,”r”))==NULL)//判断文件是否打开成功
{//读取D盘下,名为123的文本文件
printf(“文件打开失败\n”);
exit(0);
}
else
printf(“文件打开成功\n”);
double a[N];
for(int i=0;i5;i++)//读取五个浮点型数据
fscanf(fp,”%lf,”,a[i]);//fscanf函数固定格式读取文本中的数据;
for(int i=0;i5;i++)
printf(“%.6lf,\n”,a[i]);
fclose(fp);
}
C语言从文件读写数组
首先,文件里的分隔符号只能是空格,逗号不会被fscanf识别,除非你用单字节逐一读取自己判断。
第二,文本文件中的字符串不用加“ ”,否则读取出来的就多了一对双引号;
第三,N[ ]数组元素我用的是指针,这样不用去记录有效元素个数,又节省存储空间。
第四,这种结构体数据文件读取其实最好是整个结构体单元一起读,即fread()方式,然后链表操作会简单些
文本文件示例格式:
1001 aaa 85 89 98 100 93 80 47 1
1002 vvv 85 86 88 100 92 90 45 1
1003 ccc 78 75 90 80 95 88 37 2
程序:
#define FILENAME “tempt.txt”
#define FILEOPTYPE “r”
#define KeyType int
typedef struct // 数据元素类型
{
long number;// 准考证号
char name[9];// 姓名(4个汉字加1个串结束标志)
int politics;// 政治
int Chinese;// 语文
int English;// 英语
int math;// 数学
int physics;// 物理
int chemistry;// 化学
int biology;// 生物
int weight;//权值
KeyType key;// 关键字类型应为KeyType,域名应为key
} ElemType,*pElemType;//
int main(int argc, char *argv[])
{
int count;
pElemType N[100] = {NULL};
FILE *pf;
if(NULL == (pf = fopen(FILENAME,FILEOPTYPE)))
printf(“file open failed! \n”);
printf(“file open ok! \n”);
count = 0;
while(!feof(pf))
{
if(NULL == (N[count] = (pElemType)malloc(sizeof(*((pElemType)NULL)))))
{
printf(“malloc error!\n”);
break;
}
fscanf(pf,”%ld%s%d%d%d%d%d%d%d%d”,N[count]-number\
,N[count]-name\
,N[count]-politics\
,N[count]-Chinese\
,N[count]-English\
,N[count]-math\
,N[count]-physics\
,N[count]-chemistry\
,N[count]-biology\
,N[count]-weight);
N[count]-key = 0; //补全key
count++;
}
free(N[–count]); //因为feof()判断的问题 ,最后一个读取的无效
N[count] = NULL;
fclose(pf);
count = 0;
while(NULL != N[count])
{
printf(“%ld,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d\n”,N[count]-number\
,N[count]-name\
,N[count]-politics\
,N[count]-Chinese\
,N[count]-English\
,N[count]-math\
,N[count]-physics\
,N[count]-chemistry\
,N[count]-biology\
,N[count]-weight\
,N[count]-key);
count++;
}
system(“PAUSE”);
return 0;
}
c语言读取文件然后保存到数组中
1,文件里的数据是不同类型的,既有字符类型的,也有整型的。
2,遇到这种情况,可以按照字符串类型定义数组。例如,char s[100][4];
3,编写程序时,一个一个字符读取,遇到空格就完成了一个数据读取。遇到换行,就换到下一行。
这道题目的关键是把数据类型当作字符串类型处理。
你看下面的程序中,把文本的内容都按照整型数来读取,那么当然有问题!数组s的声明是错误的!
c语言读取文件数组的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言从文件中读取数据存入数组、c语言读取文件数组的信息别忘了在本站进行查找喔。