|
-
September 26th, 2018, 05:28 AM
#2
Re: Why am I printing more characters that i expect?
Ah - an oldy but a goodie!
The problem here is binary versus text files. When you use .seekg() to get the size of the file, this is the actual size of the file (as shown in file listings etc). But you are opening the file as text, not binary. Hence lf/cr combinations are converted to just lf etc. So the actual size of the file read is usually smaller than the size given by .seekg(). The actual number of chars read by .read() is got by using gcount(). See http://www.cplusplus.com/reference/i...stream/gcount/ Also, you don't seem to store the size of the file data buffer anywhere - so how do you know how many bytes it contains when you come to use it??
Code:
//Release the memory for the data since I coppied them into the ShaderSource structure.
free(data);
No you haven't. You're just set a pointer to point to the memory into which the file is read - you haven't copied the memory. So you're freeing memory that is still going to used.
PS why are using malloc()/free() in c++?? At a minimum you should be using new/delete but much better to use managed memory and use unique_ptr. See http://www.cplusplus.com/reference/memory/unique_ptr/
Last edited by 2kaud; September 26th, 2018 at 05:31 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|