2016-08-16 97 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/。我刪除它,並使用記事本創建一個新的文件,並得到正確的文本字符串。可能我應該只是使用真正的文本編輯器來處理數據文件。 –