2016-08-16 41 views
0

我试图从test.vert文件阅读,但VS不断给我一些垃圾字符在我的字符串缓冲区的开头是这样的:Visual Studio中读取垃圾字符

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 */ 
} 

回答

3

也许这只是Unicode字节顺序标记(编辑器可以从你隐藏它)? https://en.wikipedia.org/wiki/Byte_order_mark

+0

可能是这种情况。我创建的文本文件是使用VS和NShader安装的:https://github.com/samizzo/nshader/。我删除它,并使用记事本创建一个新的文件,并得到正确的文本字符串。可能我应该只是使用真正的文本编辑器来处理数据文件。 –