I'm using fwrite in an MFC application to print content of lBuffer as shown in following C++ code:
PvBuffer *lBuffer = NULL;
// Retrieve next buffer
PvResult lResult = lStream.RetrieveBuffer(&lBuffer, &lOperationResult, 1000);
if (lResult.IsOK())
{
FILE *fp = fopen("C:\\Users\\acm45\\Desktop\\abuffer.bin", "wb");
fwrite(lBuffer, 1, 10075968, fp);
fclose(fp);
}
Any idea why the content of the file abuffer.bin is always empty even though IResult returns success?
Note the file is always created when I run the program, but it is empty and size is 0KB.
Update:
To debug I did this:
FILE *fp = fopen("C:\\Users\\acm45\\Desktop\\abuffer.bin", "wb");
if (fp) {
fwrite(lBuffer, 1,10075968, fp);
fclose(fp);
} else {
printf("error opening file");
}
and the output did not print "error opening file", but still the file is empty. What do I do now?
I guess you are having some undefined behaviour there.
The man page of fwrite says:
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.
But your call
lStream.RetrieveBuffer( &lBuffer, &lOperationResult, 1000 );
lets me guess you don't have 10075968 elements with each one byte long.
Also: Is your buffer a collection of POD elements? If not, that's another reason for undefined behaviour. fwrite is only for POD types.
Generally, it is better to use C++ streams.
addendum to explain fwrite
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
Here, size means the size of a single element.
nmemb is the number of such elements. For example:
...
struct Foobar {
int frob;
};
int main () {
...
Foobar thingies[5];
fwrite (thingies, sizeof(Foobar), 5, some_file);
}
A more generic approach is this:
Foobar thingies[5];
const size_t nm = sizeof(thingies) / sizeof(Foobar);
fwrite (thingies, sizeof(Foobar), nm, some_file);
This read: "size of the array, divided by size of a single element, which equals the number of elements in that array".
But note that this only works for arrays, not pointers!
Foobar *thingies = new Foobar[5];
const size_t nm = sizeof(thingies) / sizeof(Foobar);
fwrite (thingies, sizeof(Foobar), nm, some_file);
// THIS IS WRONG. sizeof(thingies) now equals to the
// size of a pointer!
Try doing this:
FILE *fp = fopen("C:\\Users\\acm45\\Desktop\\abuffer.bin", "wb");
if (fp) {
fwrite(lBuffer, 1, 10075968, fp);
fclose(fp);
} else {
perror("fopen");
}
Check whether you are retrieving the buffer correctly. Use the function if available in that class to get the size of the buffer and pass it instead of that constant.
Related
To read data from a file, I create heap memory then pass the variable pointer to a function so fread() will put the file data into the pointer. But when the function returns, there is no data in the new created memory.
int main(...) {
MyFile File;
File.Open(...);
int filesize = File.Tell();
char* buffer = new buffer[filesize]; // Create some memory for the data
File.Read((char**)&buffer);
// Now do something with the buffer. BUT there is trash in it.
File.Close();
delete [] buffer;
}
size_t File::Read(void* buf) {
...
::fseek(fStream, 0, SEEK_END);
int fileSize = ::ftell(fStream); // Get file size.
::fseek(fStream, 0, SEEK_SET);
::fread(buf, 1, fileSize, fStream);
return (fileSize);
}
Yes, I can put char * myBuffer = new char[fileSize]; inside of File::Read(...) before ::fread(myBuffer, 1, fileSize, fStream);,
but I should not have to do this because I already have heap memory
(buffer) in main().
You're reading your file contents into the pointer buffer, not the array it points to.
You're overcomplicating things anyway. You don't need a pointer to a pointer, or a void*. You can simply pass a char* to Read. You should really also pass the size of the buffer pointed to into Read as well. Otherwise you risk overflowing your buffer.
int main() {
MyFile File;
File.Open(/*.....*/);
int filesize = File.Tell()
char* buffer = new buffer[filesize]; // Create some memory for the data
File.Read(buffer, filesize);
// Now do something with the buffer. BUT there is trash in it.
File.Close();
delete [] buffer;
}
size_t File::Read(char* buf, size_t count) {
// ......
// No need to find the size of the file a second time
// Return the actual number of bytes read
return ::fread(buf, 1, count, fStream);
}
I changed my function to:
size_t nvFile::Read( char * pszBuffer, const size_t uiCount ) ...
Thank you Miles Budnek! I did not think enought of my problem. I am opening a binary file and it is a byte (char), so it not have to be void *. (I put on my 'cone-of-shame' for not thinking.)
Thank you for help and makeing me think more. :)
I did a sample project to read a file into a buffer.
When I use the tellg() function it gives me a larger value than the
read function is actually read from the file. I think that there is a bug.
here is my code:
EDIT:
void read_file (const char* name, int *size , char*& buffer)
{
ifstream file;
file.open(name,ios::in|ios::binary);
*size = 0;
if (file.is_open())
{
// get length of file
file.seekg(0,std::ios_base::end);
int length = *size = file.tellg();
file.seekg(0,std::ios_base::beg);
// allocate buffer in size of file
buffer = new char[length];
// read
file.read(buffer,length);
cout << file.gcount() << endl;
}
file.close();
}
main:
void main()
{
int size = 0;
char* buffer = NULL;
read_file("File.txt",&size,buffer);
for (int i = 0; i < size; i++)
cout << buffer[i];
cout << endl;
}
tellg does not report the size of the file, nor the offset
from the beginning in bytes. It reports a token value which can
later be used to seek to the same place, and nothing more.
(It's not even guaranteed that you can convert the type to an
integral type.)
At least according to the language specification: in practice,
on Unix systems, the value returned will be the offset in bytes
from the beginning of the file, and under Windows, it will be
the offset from the beginning of the file for files opened in
binary mode. For Windows (and most non-Unix systems), in text
mode, there is no direct and immediate mapping between what
tellg returns and the number of bytes you must read to get to
that position. Under Windows, all you can really count on is
that the value will be no less than the number of bytes you have
to read (and in most real cases, won't be too much greater,
although it can be up to two times more).
If it is important to know exactly how many bytes you can read,
the only way of reliably doing so is by reading. You should be
able to do this with something like:
#include <limits>
file.ignore( std::numeric_limits<std::streamsize>::max() );
std::streamsize length = file.gcount();
file.clear(); // Since ignore will have set eof.
file.seekg( 0, std::ios_base::beg );
Finally, two other remarks concerning your code:
First, the line:
*buffer = new char[length];
shouldn't compile: you have declared buffer to be a char*,
so *buffer has type char, and is not a pointer. Given what
you seem to be doing, you probably want to declare buffer as
a char**. But a much better solution would be to declare it
as a std::vector<char>& or a std::string&. (That way, you
don't have to return the size as well, and you won't leak memory
if there is an exception.)
Second, the loop condition at the end is wrong. If you really
want to read one character at a time,
while ( file.get( buffer[i] ) ) {
++ i;
}
should do the trick. A better solution would probably be to
read blocks of data:
while ( file.read( buffer + i, N ) || file.gcount() != 0 ) {
i += file.gcount();
}
or even:
file.read( buffer, size );
size = file.gcount();
EDIT: I just noticed a third error: if you fail to open the
file, you don't tell the caller. At the very least, you should
set the size to 0 (but some sort of more precise error
handling is probably better).
In C++17 there are std::filesystem file_size methods and functions, so that can streamline the whole task.
std::filesystem::file_size - cppreference.com
std::filesystem::directory_entry::file_size - cppreference.com
With those functions/methods there's a chance not to open a file, but read cached data (especially with the std::filesystem::directory_entry::file_size method)
Those functions also require only directory read permissions and not file read permission (as tellg() does)
void read_file (int *size, char* name,char* buffer)
*buffer = new char[length];
These lines do look like a bug: you create an char array and save to buffer[0] char. Then you read a file to buffer, which is still uninitialized.
You need to pass buffer by pointer:
void read_file (int *size, char* name,char** buffer)
*buffer = new char[length];
Or by reference, which is the c++ way and is less error prone:
void read_file (int *size, char* name,char*& buffer)
buffer = new char[length];
...
fseek(fptr, 0L, SEEK_END);
filesz = ftell(fptr);
will do the file if file opened through fopen
using ifstream,
in.seekg(0,ifstream::end);
dilesz = in.tellg();
would do similar
So i have this function that reads file, allocates memory, and puts file's content into buffer. I constantly get garbage data in the end though, so i need a way to null terminate the buffer.
#include "GetText.h"
void GetText(const char* filename, char** buffer)
{
FILE* file = fopen(filename,"rb");
long file_lenght;
if(file)
{
fseek(file, 0, SEEK_END);
file_lenght = ftell(file);
rewind(file);
*buffer = (char*) malloc(file_lenght + 1);
fread(*buffer, 1, file_lenght, file);
*buffer[file_lenght] = '\0'; //This line crashed program
fclose(file);
}
}
Since the bracket operator has higher precedence than pointer dereference you need to dereference buffer before indexing, like this:
(*buffer)[file_lenght] = '\0';
In your program you also need to make sure malloc was successful.
I used fwrite to store some data and now I'm trying to use fread to read the data from the txt file for processing. I want to read the values individually but I can't figure out how you'd do that. This is what I have tried:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE * pFile;
long lSize;
unsigned short * buffer;
size_t result;
pFile = fopen ( "myfile.txt" , "rb" );
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (unsigned short *) malloc (sizeof(unsigned short)*lSize);
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
printf("%uz\n", result);
// terminate
fclose (pFile);
free (buffer);
return 0;
}
The above program compiles fine but when I run it with ./a.out I get a Segmentation fault. When I run it with sudo ./a.out I don't get seg fault but nothing prints out.
Any idea what I can do to fix it?
Problems I see:
Allocating more memory than needed
After
lSize = ftell (pFile);
lSize is set to the number of characters in the file, not the number of unsigned shorts. Hence, you need
buffer = malloc(lSize);
See Do I cast the result of malloc?. If you are using a C++ compiler (as your C++ tag seems to imply), you need to cast the return value of malloc.
Wrong format specifier
printf("%s\n", result);
uses the wrong format specifier to print result. You need to use
printf("%zu\n", result);
That line is the most likely culprit for the segmentation fault you are seeing.
Reading objects one by one
You can certainly use:
size_t count = lSize/sizeof(short);
for ( size_t i = 0; i < count; ++i )
{
unsigned short number;
result = fread (&number, sizeof(unsigned short), 1, pFile);
}
You can also use:
size_t count = lSize/sizeof(short);
for ( size_t i = 0; i < count; ++i )
{
result = fread (buffer+i, sizeof(unsigned short), 1, pFile);
}
I'm trying to read a jpg file from disk, and copy to a char buffer. The problem is that on the bytes there are some NULL character, and I'm having problems when I read the char buffer.
This is the current code:
char* readImg(char* filename)
{
FILE * pFile;
char jpgBuffer[20048];
long lSize;
errno_t result = fopen_s (&pFile,filename,"rb");
if (result != 0) {
printf("Error \n");
}
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
result = fread (jpgBuffer,1,lSize,pFile);
fclose (pFile);
jpgBuffer[lSize] = '\0';
return jpgBuffer;
}
and the call to the function is:
char* img = readImg("img.jpg");
then, I need to encode into base64, but if I want to know the size of the image buffer with strlen(), I'm getting a size of 4, because the 5 character is a "0".
How can I avoid the NULL characters into image buffer?
You may change your function prototype.
long readImage(const char* filename, char* buf, long bufSize)
{
FILE * pFile;
long lSize;
errno_t result = fopen_s (&pFile,filename,"rb");
if (result != 0) {
printf("Error \n");
}
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
if(bufSize < lSize)
{
printf("buf too small, needs %lu\n", lSize);
fclose(pFile);
return -1;
}
result = fread (buf,1,lSize,pFile);
fclose (pFile);
return lSize;
}
Then you get img data & actual size of it.
If you don't mind using malloc, you can alloc memory for buffer in your function.
long readImage(const char* filename, char** pbuf)
{
FILE * pFile;
long lSize;
errno_t result = fopen_s (&pFile,filename,"rb");
if (result != 0) {
printf("Error \n");
}
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
*pbuf = (char*)malloc(lSize * sizeof(char));
result = fread (*buf,1,lSize,pFile);
fclose (pFile);
return lSize;
}
call this function like following, and you need to remember free buffer.
char* buf = NULL;
long len = 0;
len = readImage(filename, &buf);
...
free(buf);
Use the lSize you determine in readImg(...) as the file size. strlen is for null terminated strings. dont use strlen to determine image size in bytes.
Note also, that you should assign jpgBuffer as a heap variable via new[] using lSize as the byte size. That way you can return a pointer to heap memory. Your current stack variable jpgBuffer will be invalid after the function returns, and hence the pointer to it. This way you also dont have to worry about the situation where you need more bytes than your hardcoded value (which you currently dont check!).
You will also want to return the lSize via an input parameter pointer/ref variable so you have a bounds to iterate over later on.
You can't use strlen to something that is not a string. You should return the size from the readIamge function. e.g.
char* readImg(char* filename, int *size);
If you program in C++ your should instead return an instance of a kind of an image class. This would avoid the splattered location of results.
Further you should never return the address of a local variable as your jpgBuffer. This variable will be overwritten aftern return from function.
You can return your data as a char* without any size information.
You can't return your jpgBuffer, locally allocated.
Change your function into:
int readImg(char* filename, unsigned char* buffer)
{
//...
result = fread (buffer,1,lSize,pFile);
return lSize;
}
If you can, allocate your buffer in the calling function, it will simplify your code.
However, it you want to avoid buffer overflow, you should pass the size of the allocated buffer to your read function, and return an error/throw an exception if the file size is greater than the size you allocated.
int readImg(char* filename, unsigned char* buffer, size_t aSize)
{
//...
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
if (lSize > aSize)
{
// Manage error case
}
//...
result = fread (buffer,1,lSize,pFile);
return lSize;
}
Keep in mind that this way of coding is more C-style than C++-style. If you wish and can use C++, take advantage of C++ constructs, standard library to improve your code.
jpgBuffer is a "binary" buffer, not a "string". Put a null char at the end is non-sense. You should use the result variable to read your buffer.