开发者

Reading the last 50 characters of a file with fseek()

开发者 https://www.devze.com 2022-12-20 00:19 出处:网络
I\'m trying to read the last 50 开发者_如何学JAVAcharacters in a file by doing this: FILE* fptIn;

I'm trying to read the last 50 开发者_如何学JAVAcharacters in a file by doing this:

FILE* fptIn;
char sLine[51];
if ((fptIn = fopen("input.txt", "rb")) == NULL) {
    printf("Coudln't access input.txt.\n");
    exit(0);
}
if (fseek(fptIn, 50, SEEK_END) != 0) {
    perror("Failed");
    fclose(fptIn);
    exit(0);
}
fgets(sLine, 50, fptIn);
printf("%s", sLine);

This doesn't return anything that makes sense remotely. Why?


Change 50 to -50. Also note that this will only work with fixed-length character encodings like ASCII. Finding the 50th character from the end is far from trivial with things like UTF-8.


Try setting the offset to -50.


Besides the sign of the offset the following things could make trouble:

A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.

Use either ferror or feof to check whether an error happened or the End-of-File was reached.

See also


fseek(fptIn, 50, SEEK_END)

Sets the stream pointer at the end of the file, and then tries to position the cursor 50 bytes ahead thereof. Remember, for binary streams:

3 For a binary stream, the new position, measured in characters from the beginning of the file, is obtained by adding offset to the position specified by whence..The specified position is the beginning of the file if whence is SEEK_SET, the current value of the file position indicator if SEEK_CUR, or end-of-file if SEEK_END. A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END.

This call should fail. The next call to fgets invokes UB. Try -50 as an offset and also iff the call succeeds try to read it into your buffer

Note: emphasis mine

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号