C 文件读取留下垃圾字符

发布于 2024-08-30 01:26:59 字数 1417 浏览 8 评论 0原文

我正在尝试将文件的内容读入我的程序,但我偶尔会在缓冲区末尾收到垃圾字符。我没有经常使用 C(相反我一直在使用 C++),但我认为它与流有关。我真的不知道该怎么办。我正在使用 MinGW。

这是代码(这在第二次读取结​​束时给我带来了垃圾):

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

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*)malloc(length+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 */
}

int main()
{
 char* vs;
 char* fs;

 vs = filetobuf("testshader.vs");
 fs = filetobuf("testshader.fs");

 printf("%s\n\n\n%s", vs, fs);

 free(vs);
 free(fs);

 return 0;
}

filetobuf 函数来自此示例 http://www.opengl.org/wiki/Tutorial2:_VAOs、_VBOs、_Vertex_and_Fragment_Shaders_%28C_/_SDL%29。不过,这对我来说似乎是正确的。

无论如何,这是怎么回事?

I'm trying to read the contents of a file into my program but I keep occasionally getting garbage characters at the end of the buffers. I haven't been using C a lot (rather I've been using C++) but I assume it has something to do with streams. I don't really know what to do though. I'm using MinGW.

Here is the code (this gives me garbage at the end of the second read):

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

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*)malloc(length+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 */
}

int main()
{
 char* vs;
 char* fs;

 vs = filetobuf("testshader.vs");
 fs = filetobuf("testshader.fs");

 printf("%s\n\n\n%s", vs, fs);

 free(vs);
 free(fs);

 return 0;
}

The filetobuf function is from this example http://www.opengl.org/wiki/Tutorial2:_VAOs,_VBOs,_Vertex_and_Fragment_Shaders_%28C_/_SDL%29. It seems right to me though.

So anyway, what's up with that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2024-09-06 01:26:59

您需要清除缓冲区 - malloc 不会这样做。尝试使用 calloc 代替或 memset'ing 你的缓冲区,以便它开始清晰。

You need to clear out your buffer - malloc doesn't do so. Try using calloc instead or memset'ing your buffer so that it starts out clear.

娇妻 2024-09-06 01:26:59

使用 fopen(..., "rb") 而不是 (..., "r");
在 Windows 下以“二进制”模式打开文件。

Use fopen(.... , "rb") instead of (..., "r");
Opens the file in "binary" mode under Windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文