简体   繁体   English

fread跳过前8个字符

[英]fread skips first 8 characters

I am trying to read in a file, hello.ms containing the following: 我正在尝试读取一个包含以下内容的文件hello.ms:

Hello World!

Using this C code: 使用此C代码:

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

int store_file(char* file_dir, char** buffer)
{
    FILE* file;
    long lSize;
    size_t result;
    char* tempBuffer;

    file = fopen(file_dir, "r");
    if (file==NULL)
    {
        fputs("File error.", stderr);
        exit(1);
    }

    fseek(file, 0, SEEK_END);
    lSize = ftell(file);
    rewind(file);

    tempBuffer = (char*)malloc(sizeof(char)*lSize);
    if (tempBuffer == NULL)
    {
        fputs("Memory error.", stderr);
        exit(2);
    }

    result = fread(tempBuffer, 1, lSize, file);
    if (result != lSize)
    {
        fputs("Reading error.", stderr);
        exit(3);
    }

    *buffer = tempBuffer;
    free(tempBuffer);
    fclose(file);

    return lSize;
}

void fsa_cycle(char* file_dir)
{
    char* buffer;
    int bufferSize = store_file(file_dir, &buffer);

    fwrite(buffer, sizeof(char), bufferSize, stdout);
}

int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        printf("\nSyntax: %s <file-name>\n\n", argv[0]);
        exit(1);
    }

    fsa_cycle(argv[1]);

    return 0;
}

It compiles perfectly fine. 它编译得很好。 No warnings or errors. 没有警告或错误。 But it only outputs rld! 但是它只输出rld!

When I add 8 spaces to the hello.ms file, it then reads Hello World! 当我将8个空格添加到hello.ms文件时,它将读取Hello World!

Can anyone tell me why this is happening? 谁能告诉我为什么会这样吗? I tried writing fseek(file, 0, SEEK_CUR) instead of rewind but that didn't work either. 我尝试编写fseek(file,0,SEEK_CUR)而不是快退,但这也不起作用。 I couldn't find any help on Google that didn't suggest that the programmer forgot to use fseek to get to the beginning. 我在Google上找不到任何帮助,没有暗示程序员忘了使用fseek来起步。 Any assistance would be awesome! 任何帮助都将非常棒!

You are using free to release tempBuffer memory, but that memory block is in fact assigned to buffer . 您可以free释放tempBuffer内存,但实际上该内存块已分配给buffer

free might work different with different compilers, but mine works as yours, just remove free(tempBuffer); free对于不同的编译器可能会有所不同,但是我的工作与您一样,只需删除free(tempBuffer);

You are freeing the buffer too early. 您太早释放缓冲区。
You should free the memory after you use the buffer. 使用缓冲区后,应释放内存。 The line: 该行:

fwrite(buffer, sizeof(char), bufferSize, stdout);

I ran it in gdb and things go wrong after the free(tempBuffer); 我在gdb中运行它,在free(tempBuffer)之后出现了问题。

(gdb) 31 if (result != lSize) (gdb)31 if(结果!= lSize)

(gdb) print tmpBuffer (gdb)打印tmpBuffer

No symbol "tmpBuffer" in current context. 当前上下文中没有符号“ tmpBuffer”。

(gdb) print tempBuffer (gdb)打印tempBuffer

$3 = 0x602250 "Hello World!\\n" $ 3 = 0x602250“ Hello World!\\ n”

(gdb) n (gdb)n

37 *buffer = tempBuffer; 37 * buffer = tempBuffer;

(gdb) (GDB)

38 free(tempBuffer); 38免费(tempBuffer);

(gdb) print buffer (gdb)打印缓冲区

$4 = (char **) 0x7fffffffe180 $ 4 =(字符**)0x7fffffffe180

(gdb) print *buffer (gdb)打印*缓冲区

$5 = 0x602250 "Hello World!\\n" $ 5 = 0x602250“ Hello World!\\ n”

(gdb) n (gdb)n

39 fclose(file); 39 fclose(文件);

(gdb) (GDB)

41 return lSize; 41返回lSize;

(gdb) print *buffer (gdb)打印*缓冲区

$6 = 0x602250 "" $ 6 = 0x602250“”

(gdb) print tempBuffer (gdb)打印tempBuffer

$7 = 0x602250 "" $ 7 = 0x602250“”

try to use in this way 尝试以这种方式使用

    char *buff;
buff = calloc(sizeOfFileInBytes + 1, sizeof(char));//replace sizeOfFileInBytes with file size (use stat to get file size)
if (!buff) {
    printf("error : %s", strerror(errno));
    fclose(f);
}
fread(buff, sizeOfFileInBytes, 1, f);

fclose(f);
printf("%s\n", (char *) buff);//lets echo what we get from fread;
free(buff);//lets return memory to heap

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

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