gpt4 book ai didi

使用 fseek 计算文件字符数

转载 作者:行者123 更新时间:2023-11-30 18:41:17 24 4
gpt4 key购买 nike

我想像这样计算文件字符:

#include <stdio.h>
#include <stdlib.h>

int main(){
int d=0;
char filename[25];
FILE *file;
printf("Enter file name to be read (Text File!) : ");
gets(filename);
file = fopen(filename, "rt");
if(file == NULL){
printf("Failed to open file...");
exit(1);
}
fseek(file, 0L, SEEK_SET);
while( !feof(file) ){
fseek(file, 1L, SEEK_CUR);
d++;
}
printf("%d", d);
}

之后我打印 d 并且它的值为 0..并且文件剂量有字符。大约150个字符..

最佳答案

fseek() 允许您在 EOF 标记之外进行查找。

要获取文件大小,您可以fseek(file, 0, SEEK_END),然后调用ftell(file)

要逐个字符读取,可以使用 for (i = 0; fgetc(file) != EOF; i++);

关于使用 fseek 计算文件字符数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22425972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com