Tu Do
Tu Do

Reputation: 339

Visual Studio reads junk characters

I tried to read from a test.vert file, but VS keeps giving me a few junk characters at the beginning of my string buffer like this:

junk_chars

I had to move the pointer 3 positions away to get the correct string for everything to work correctly. What is happening here?

Here is one file reading function. I tried mine first, then a few others copied on the internet but results are the same:

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

Upvotes: 0

Views: 254

Answers (1)

Anton Malyshev
Anton Malyshev

Reputation: 8871

Maybe it's just unicode byte order mark (your editor can hide it from you)? https://en.wikipedia.org/wiki/Byte_order_mark

Upvotes: 3

Related Questions