C 文件读取留下垃圾字符
我正在尝试将文件的内容读入我的程序,但我偶尔会在缓冲区末尾收到垃圾字符。我没有经常使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要清除缓冲区 - 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.
使用 fopen(..., "rb") 而不是 (..., "r");
在 Windows 下以“二进制”模式打开文件。
Use fopen(.... , "rb") instead of (..., "r");
Opens the file in "binary" mode under Windows.