Reading and writing binary file in C language
NickName:Goldenbough Ask DateTime:2019-12-06T15:01:00

Reading and writing binary file in C language

I tried to read and write fild in C but it failed. It partly worked, but the original file and the output file is not same. I tried to read and write bmp file.

FILE* openFile = fopen(argv[1], "rb");                      
FILE* writeFile = fopen(strcat(argv[1], ".cpd"), "wb");     
fseek(openFile, 0, SEEK_END);                               
long size = ftell(openFile);                                
char* bin = (char*)malloc(sizeof(char) * (size + 1));       
rewind(openFile);                                           
fwrite(bin, size, 1, writeFile);

//closefile, free, ...

Copyright Notice:Content Author:「Goldenbough」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59208357/reading-and-writing-binary-file-in-c-language

More about “Reading and writing binary file in C language” related questions

Reading and writing binary file in C

Firstly, I read here: reading-binary-files-in-c binary-file-reading-writing-in-c I am trying to read binary file in C. And wrote this code: struct emp { char name[20]; char surname[20]; ...

Show Detail

Binary Reading from and Writing to a File in C

I need some help writing a program in C to reading and writing binary values from and to a file. For this program I do not need the entire contents of the file (2048 bytes) read in, only the first 30

Show Detail

Reading and Writing from a binary file in C

This question has been asked many times before, but I've been trying this for hours with a variety of methods and I have no clue why this is wrong. I've been writing C structs to a binary file us...

Show Detail

writing and reading into a file in C language

Recentl I have started learning file I/O in C like fopen fwrite and stuff like that. I have a question regard writing/reading a struct that has pointers to a file. Let's say the struct looks like t...

Show Detail

qt binary file writing and reading

void write(QString filename) { QChar ch('b'); QFile mfile(filename); if (!mfile.open(QFile::WriteOnly) { qDebug() << "Could not open file for writing"; return; } QDataStream..

Show Detail

Writing data to a binary file, then reading it back

I am trying to write data to a binary file and then read back the data from the file. The data consists of a single number (an integer) nrows. Below is my code for writing the data to a binary file.

Show Detail

Writing/reading strings as binary into random accessed file

Simple question: I want to write strings of fixed length into a binary file (that is, as binary), as illustrated in the following snippet. The writing "looks" fine, but reading from the f...

Show Detail

How to resolve segmentation error in reading/writing to binary file in C

This is the struct definition that I am trying to write copies of to and read from binary file typedef struct carType Car; struct carType { int vehicleID; char make[20]; char model[20]...

Show Detail

Binary file- menu writing and reading, in c issue

I have to write a program for writing and reading a binary file. There should be a menu for the choice. The file should have indentification number, an article with name, parameters, etc and a pric...

Show Detail

Reading and writing integers to a binary file using C?

I am trying to write 100 integers to binary file. I have tried writing to this file, and reading from it. When reading from it I get completely random digits. Here is the block concerning the writ...

Show Detail