I have a binary data in c++ variable buffer as below:
int len = 39767;
uint16_t * buffer = (uint16_t) malloc(len);
FILE * fp = fopen("rahul.jpg", "rb"); // size of file is 39767 byte.
fread(buffer, len, 1, fp);
fclose(fp);
fp = fopen("rahul3.jpg", "wb");
fwrite(buffer, len, 1, fp); // Here it is written correct.
fclose(fp);
I want to pass this buffer to Node.js and write to a file. I used below line to convert it to Local and then wrapping to an Object:
Local<String> str = Nan::New(buffer).ToLocalChecked();
return scope.Escape(str);
But, in node.js when I am checking length of received data it prints 9 only and value seems corrupted.
console.log(data);
console.log("len = " + data.length );
fs.writeFileSync('rahul2.jpg', data, 'binary');
Here rahul2.jpg is corrupted and is of 9 bytes only. How can we get rahul2.jpg from node.js code same as rahul.jpg in c++? Which Nan::New() we should use to pass binary data unaffected? Please help. Thanks.
Try something like this:
Local<Value> returnValue = Nan::CopyBuffer(buffer, len).ToLocalChecked();
and, by the way, fread returns the number of bytes read from file, so it is better to do as follows:
int truelen = fread(buffer, len, 1, fp);
. . .
fwrite(buffer, truelen, 1, fp);
Local<String> str = Nan::NewOneByteString((uint8_t *) buffer, len).ToLocalChecked();
I used above code in C++ which solved the issue. In node.js used the below code to write to file:
fs.writeFileSync('rahul2.jpg', data, 'binary');
Thanks.
Related
I need write a byte in binary file, but only if this is diferent (something looked like EEPROM.update(a) from Arduino IDE). I think there is some function that does this, but now I can't find it/I'm not sure that exists.
So far I have do this:
FILE *fp = fopen("file.dat", "r+");
fseek(fp, (long) address, SEEK_SET);
fread(&value, sizeof(uint8_t), 1, fp);
if (value == val) {
fclose(fp);
exit();
}
fseek(fp, (long) address, SEEK_SET);
fwrite(v, sizeof(uint8_t), 1, fp);
fclose(fp);
This code is an example, not the real code.
Thanks for your time.
The fread advances the file pointer, so your fwrite doesn't overwrite the byte at offset address, but rather the following byte. You need to fseek back to offset address again before writing.
Also there is a v in your sample code which isn't defined. Probably it should be &val or something like that.
Scenario: I have a file that is 8,203,685 bytes long in binary, and I am using fread() to read in the file.
Problem: Hexdumping the data after the fread() on both Linux and Windows yields different results. Both hexdump files are the same size, but on Linux it matches the original input file that went in, whereas on Windows starting at byte 8,200,193 the rest of the hexdump contains 0's.
Code:
int main(void)
{
FILE * fp = fopen("input.exe", "rb");
unsigned char * data = NULL;
long size = 0;
if (fp)
{
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
data = (unsigned char *)malloc(size);
size_t read_bytes = fread(data, 1, size, fp);
// print out read_bytes, value is equal to size
// Hex dump using ofstream. Hexdump file is different here on Windows vs
// on Linux. Last ~3000 bytes are all 0's on Windows.
std::ofstream out("hexdump.bin", std::ios::binary | std::ios::trunc);
out.write(reinterpret_cast<char *>(data), size);
out.close();
FILE * out_file = fopen("hexdump_with_FILE.bin", "wb");
fwrite(data, 1, size, out_file);
fflush(out_file);
fclose(out_file);
}
if (fp) fclose(fp);
if (data) free(data);
return 0;
}
Has anyone seen this behavior before, or have an idea of what might be causing the behavior that I am seeing?
P.S. Everything works as expected when using ifstream and its read function
Thanks!
I have a large file containing strings. I have to read this file and store it in a buffer using C or C++. I tried to do it as follows:
FILE* file = fopen(fileName.c_str(), "r");
assert(file != NULL);
size_t BUF_SIZE = 10 * 1024 * 1024;
char* buf = new char[BUF_SIZE];
string contents;
while (!feof(file))
{
int ret = fread(buf, BUF_SIZE, 1, file);
assert(ret != -1);
contents.append(buf);
}
The data in the file would be the strings and i have to find the character with maximum frequency.
Is it possible to optimize the code more than this ? Will using BinaryReader improve optimisation ? Could you share some more ways if you know?
I have a binary file,
FILE *fp;
fp = fopen("file_name.data", "rb");
Which can be successfully read using fread() with the following C code
int S = 8;
int *table = (int*)malloc(S*sizeof(int));
fread(table, S*sizeof(int), 1, fp);
But when I read file to C++ vector, the result is wrong
vector<int> table;
table.resize(S);
fread(&table[0],table.size(), 1, fp);
Is there anything wrong with above code ?.
table.size() returns the number of elements in the std::vector, not the number of bytes. You still need to multiply that by the size of each element, just like you do in the C code.
fread(&table[0],table.size()*sizeof(int), 1, fp);
Your fread should be:-
fread (&table[0], sizeof(vector<int>::value_type), table.size(), fp);
// OR
fread (&table[0], sizeof(int), table.size(), fp);
Both appear to be wrong (although the C version looks like it will work).
See: http://www.tutorialspoint.com/c_standard_library/c_function_fread.htm
Try these:
int S = 8;
int *table = (int*)malloc(S*sizeof(int));
fread(table, sizeof(int), S, fp);
vector<int> table;
table.resize(S);
fread(&table[0], sizeof(int), table.size(), fp);
I try to read data from one PNG file, and want to write this data to the new file and save it.
I do such stuff like that:
FILE *fp = fopen("C:\\dev\\1.png", "rb");
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
rewind(fp);
char *buffer = (char*)malloc(sizeof(char)*size);
size_t result = fread(buffer, 1, size, fp);
FILE *tmpf = fopen("C:\\dev\\1_1.png", "wb");
fputs(buffer, tmpf);
fflush(tmpf);
fclose(tmpf);
I've got problem, that second file only has in its content, only that: ‰PNG SUB
In debugging , I have checked, long size = 652521, and size_t result has got the same size...
Don't understand, why I can't write all data to the second file...
Don't use fputs - use fwrite - fputs is for strings and will terminate on the first zero byte.
Change:
fputs(buffer, tmpf);
to:
fwrite(buffer, 1, size, tmpf);