用文件操作函数获取文件的大小,行数

来源:互联网 发布:手机短信修改软件 编辑:程序博客网 时间:2024/05/17 22:45
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
struct fileinfo//文件大小,行数结构体
{
int lines;
int size;
};
struct fileinfo countline(const char* filename)
{
FILE* fp;
struct fileinfo info;
fp = fopen(filename, "r");
if ((int)fp == 0)
{
perror("openfile error:");
exit(0);
}
char buf[1024];
int lines = 0;
while (fgets(buf, 1024, fp))//若fgets返回NULL,则退出循环
{
lines++;
}
info.lines = lines;
info.size = ftell(fp);//此时fp指针指向文件末尾,ftell的返回值即为文件大小
fclose(fp);
return info;
}
0 0
原创粉丝点击