Visual Studio读取垃圾字符

时间:2016-08-16 11:09:18

标签: c visual-studio

我试图从test.vert文件中读取,但VS一直在我的字符串缓冲区的开头给我一些垃圾字符,如下所示:

junk_chars

我不得不移动指针3个位置以获得正确的字符串,以便一切正常工作。这里发生了什么?

这是一个文件读取功能。我首先尝试了我的,然后在网上复制了其他一些但结果是一样的:

char* filetobuf(char *file)
{
    FILE *fptr;
    long length;
    char *buf;

    fptr = fopen(file, "r"); /* Open file for reading */
    if (!fptr) /* Return NULL on failure */
        return NULL;
    fseek(fptr, 0, SEEK_END); /* Seek to the end of the file */
    length = ftell(fptr); /* Find out how many bytes into the file we are */
    buf = (char*)calloc(length + 1,1); /* Allocate a buffer for the entire length of the file and a null terminator */
    fseek(fptr, 0, SEEK_SET); /* Go back to the beginning of the file */
    fread(buf, length, 1, fptr); /* Read the contents of the file in to the buffer */
    fclose(fptr); /* Close the file */
    buf[length] = 0; /* Null terminator */

    return buf; /* Return the buffer */
}

0 个答案:

没有答案