简体   繁体   English

从C中的文件计算未知数量的字符

[英]Counting unknown numbers of chars from a file in C

I'm looking for a easy way to count the unknown number of characters in a file using C language. 我正在寻找一种使用C语言计算文件中未知字符数的简便方法。 thanks for your help 谢谢你的帮助

The POSIX way (probably what you want): POSIX方式(可能你想要的):

off_t get_file_length( FILE *file ) {
    fpos_t position; // fpos_t may be a struct and store multibyte info
    off_t length; // off_t is integral type, perhaps long long

    fgetpos( file, &position ); // save previous position in file

    fseeko( file, 0, SEEK_END ); // seek to end
    length = ftello( file ); // determine offset of end

    fsetpos( file, &position ); // restore position

    return length;
}

The standard C way (to be pedantic): 标准的C方式(迂腐):

long get_file_length( FILE *file ) {
    fpos_t position; // fpos_t may be a struct and store multibyte info
    long length; // break support for large files on 32-bit systems

    fgetpos( file, &position ); // save previous position in file

    if ( fseek( file, 0, SEEK_END ) // seek to end
        || ( length = ftell( file ) ) == -1 ) { // determine offset of end
        perror( "Finding file length" ); // handle overflow
    }

    fsetpos( file, &position ); // restore position

    return length;
}

If you want to know the number of multibyte characters, you need to read the entire file with eg fgetwc . 如果您想知道多字节字符的数量,您需要使用例如fgetwc读取整个文件。

FILE *source = fopen("File.txt", "r");
fseek(source, 0, SEEK_END);
int byteCount = ftell(source);
fclose(source);
/* wc is used to store the result */
long wc;

/* Open your file */
FILE * fd = fopen("myfile", "r");

/* Jump to its end */
fseek(fd, 0, SEEK_END);

/* Retrieve current position in the file, expressed in bytes from the start */
wc = ftell(fd);

/* close your file */
fclose(fd);

EDIT: You probably want to read the answers below this. 编辑:您可能想要阅读下面的答案。

You can keep reading characters until the end of a file by checking the result of a reading operation against EOF (end of file). 通过检查针对EOF (文件结束)的读取操作的结果,您可以继续读取字符直到文件末尾。 Doing them one at a time also lets you gather other statistics about them. 一次执行一个也可以让您收集有关它们的其他统计信息。

char nextChar = getc(yourFilePointer);
int numCharacters = 0;

while (nextChar != EOF) {
    //Do something else, like collect statistics
    numCharacters++;
    nextChar = getc(yourFilePointer);
}

This should get you started if you need to count only some characters (for example, only printable characters) 如果您只需要计算一些字符(例如,只有可打印的字符),这应该可以帮助您入门

while (fgetc(file_handler)!=EOF)
{
 //test condition here if neccesary.
  count++;
}

If you are looking for the size of the file, the fseek / ftell solution seems less syscall expensive. 如果你正在寻找文件的大小,fseek / ftell解决方案似乎不那么昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM