gpt4 book ai didi

C 文件中的字符数

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

当文件写入“test1”时,为什么我的程序返回 9?该程序查找文件中的字符数。我想运行 'while(fgetc(file) != EOF)' 方法,但这似乎不起作用。

我希望能得到一些帮助。谢谢

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

#define FORMATLOG "FORMAT ERROR: invalid parameter: s5e4 <filename.txt>"
#define TXFILELOG "FILE ERROR: Can't open file or file does not exist"

enum { true, false };


int interface(char *filename) {

FILE *f_text = fopen(filename, "r+");
size_t size;
char c;

if(f_text == NULL) {
puts(TXFILELOG);
return NULL;
}

fseek(f_text, 0, SEEK_END);
size_t length = (size_t) ftell(f_text);

printf("%d", length);

fclose(f_text);

return true;
}


int main(int argc, char **argv) {

if(argc != 2) {
puts(FORMATLOG);
return false;
}

return interface(argv[1]);
}

最佳答案

为什么会出现并发症?

参见http://linux.die.net/man/2/stat

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
struct stat buf;
stat("filename", &buf);
printf("Size:%d\n", buf.st_size);
return 0;
}

(哎呀错过了错误检查,但我想你明白了)

关于C 文件中的字符数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26957713/

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