How can I read from and write to a binary file simultaneously? - file

I want to change the value of a couple of bytes in a large binary file using MATLAB's fwrite command. What I am trying to do is open the file using:
fopen(filename,'r+',precision);
Then read down the file using:
fread(fid,NUM,'int32');
This all works. Once I get to the file position where I want to write (overwrite) the values of the next bytes, I use the command:
fwrite(fid,variable_name,'int32');
Then I close the file:
fclose(fid);
So then I go back and re-read the file and these bytes haven't changed!
So is this not possible? Or is 'r+' the wrong thing to use?

From the documentation for fopen:
To read and write to the same file:
Open the file with a value for permission that includes a plus sign, '+'.
Call fseek or frewind between read and write operations. For example, do not call fread followed by fwrite, or fwrite followed by fread, unless you call fseek or frewind between them.
In short, you need to call fseek before you call fwrite:
fid = fopen(filename, 'r+', precision);
data = fread(fid, NUM, 'int32');
fseek(fid, 0, 'cof');
fwrite(fid, variable_name, 'int32');
fclose(fid);
In fact, if you don't actually need to read anything from the file, and just need to move to a given position in the file, I would just use fseek in place of your call to fread. For example:
fid = fopen(filename, 'r+', precision);
fseek(fid, NUM*4, 'bof');
fwrite(fid, variable_name, 'int32');
fclose(fid);

When you read to know which byte to change keep a count of how many bytes you have to skip (4 bytes each int or float for instance).
bytesToSkip = 0;
not_the_value_you_want = true;
bytesPerValue = 4; %for a float or int
while not_the_value_you_want
...some code here...
if 'this is it'
not_the_value_you_want = false; % adapt this to your taste
else
bytesToSkip += bytesPerValue;
end;
...maybe more code here...
end;
Try this after:
fileID = fopen('YourFile.bin','w+');
fseek(fileID,bytesToSkip,'bof'); %'bof' stands for beginning of file
fwrite(fileID,newValue);
fclose(fileID);

Related

Reading a text file full with null characters and texts using fread

I am trying to design a small file system.
I have created a text file to store the files data in.
int kufs_create_disk(char* disk_name, int disk_size){
FILE* file_ptr = fopen(disk_name, "w");
if (file_ptr == NULL)
return -1;
fseek (file_ptr, disk_size * 1024-1, SEEK_SET);
fwrite("", 1, sizeof(char), file_ptr); // to make a size for the file
fclose(file_ptr);
DiskName=disk_name;
return 0;
}
After writing to the file I get a file with the size I determine when I call the function.
kufs_create_disk("test.txt", 5);
which creates a file with size of 5kbs with '\0' to fill this file to the size.
I have created another function to write to this file in different places of the file which works just fine and I won't paste the code for simplicity.
When I try to read from the file using fread(), I'm not getting all the data I have written into the memory; rather I get just some of the data.
My read implementation would be:
int kufs_read(int fd, void* buf, int n){
FILE *file_ptr= fopen("test.txt","a+");
fseek (file_ptr, FAT[fd].position, SEEK_SET); //where FAT[fd].position is where I want to start my read and fd is for indexing purposes
fread(buf, 1, n, file_ptr); //n is the number of bytes to be read
FAT[fd].position = FAT[fd].position + n;
}
The thing is the file reads some of the characters written and doesn't read the rest. I did a little test by looping all over the file and checking whether every thing is being read and fread reads every thing but in the buf I only get some of the characters I've written.
The text file looks something like this:
0\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00written string1written string2 0\00\00\00\00\00\00\00\00\00\00\00\000\00\00\00\00\00\00\00\00\00\00\00\00writtenstring 3 \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00
I get writtenstring1 and writtenstring2 in the buffer but I don't get writtenstring 3 for example.
Can you explain why?

How to duplicate an image file? [duplicate]

I am designing an image decoder and as a first step I tried to just copy the using c. i.e open the file, and write its contents to a new file. Below is the code that I used.
while((c=getc(fp))!=EOF)
fprintf(fp1,"%c",c);
where fp is the source file and fp1 is the destination file.
The program executes without any error, but the image file(".bmp") is not properly copied. I have observed that the size of the copied file is less and only 20% of the image is visible, all else is black. When I tried with simple text files, the copy was complete.
Do you know what the problem is?
Make sure that the type of the variable c is int, not char. In other words, post more code.
This is because the value of the EOF constant is typically -1, and if you read characters as char-sized values, every byte that is 0xff will look as the EOF constant. With the extra bits of an int; there is room to separate the two.
Did you open the files in binary mode? What are you passing to fopen?
It's one of the most "popular" C gotchas.
You should use freadand fwrite using a block at a time
FILE *fd1 = fopen("source.bmp", "r");
FILE *fd2 = fopen("destination.bmp", "w");
if(!fd1 || !fd2)
// handle open error
size_t l1;
unsigned char buffer[8192];
//Data to be read
while((l1 = fread(buffer, 1, sizeof buffer, fd1)) > 0) {
size_t l2 = fwrite(buffer, 1, l1, fd2);
if(l2 < l1) {
if(ferror(fd2))
// handle error
else
// Handle media full
}
}
fclose(fd1);
fclose(fd2);
It's substantially faster to read in bigger blocks, and fread/fwrite handle only binary data, so no problem with \n which might get transformed to \r\n in the output (on Windows and DOS) or \r (on (old) MACs)

Writing to File with Offset

I'm trying to write a simple block of code that writes the contents of an array to a position in a file. The file is a simple, text only file. It seems to do everything write, but file contents end up in hex (with a bunch of zeroes at the beginning). I'm using the following:
FILE * fp; // file to write to
void fwritel(long offset, char * data) {
fp = fopen(filename, "w");
fseek(fp, offset, SEEK_SET);
fwrite(data, 1, strlen(data) - 1, fp);
fclose(fp);
}
I've also used a variation of pwrite that didn't work: pwrite(fp, data, strlen(data) - 1, offset); The code I'm looking for would write data at position offset only modifying the file from offset to offset + strlen(data).
This is a multiple part fix:
Joachim Pilebord is right. Opening the file using fopen(file, "w") will destroy the contents of the file. So each time it would open the file, zero the file, then write the new contents.
M Oehm was also correct, the new content would be padded with zeros - thus Sublime treated it as hex.
So, opening the file once, somewhere at the beginning of the program, using only
fseek(fp, offset, SEEK_SET);
fwrite(data, 1, strlen(data), fp);
and closing the file somewhere near the end of the program produces the correct output. Using fputc(' ') would work but you'd need to make sure I'm not replacing characters that you need (i.e. previously placed data in those parts of the file).

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. However, when I try to read back the data and print the result using printf, I obtain a nonsensical result: -2.
FILE *fout;
FILE *file_pointer;
int nrows = 5;
fout = fopen("matrixB.bin", "wb") //Writing to a binary file.//
fwrite(&nrows, sizeof(int), 1, fout); //Writing the number nrows to the binary file "matrixB.bin"//
file_pointer = fopen("matrixB.bin", "rb"); //Reading a binary file.//
fread(&nrows, sizeof(int), 1, file_pointer);
printf("%d", nrows); //Here -2 is printed, instead of 5.//
What is the problem with my code?
One of the characteristics of the C file functions (fread, fwrite, etc) is that they normally perform buffered I/O.
Your program doesn't flush and/or close the file before reopening it. It should.

Copying a file in C with fwrite

I am new to C and was trying to write a program just to copy a file so that I could learn the basics of files. My code takes a file as input, figures out its length by subtracting its start from its end using fseek and ftell. Then, it uses fwrite to write, based on what I could get from its man page, ONE element of data, (END - START) elements long, to the stream pointed to by OUT, obtaining them from the location given by FI. The problem is, although it does produce "copy output," the file is not the same as the original. What am I doing wrong? I tried reading the input file into a variable and then writing from there, but that didn't help either. What am I doing wrong?
Thanks
int main(int argc, char* argv[])
{
FILE* fi = fopen(argv[1], "r"); //create the input file for reading
if (fi == NULL)
return 1; // check file exists
int start = ftell(fi); // get file start address
fseek(fi, 0, SEEK_END); // go to end of file
int end = ftell(fi); // get file end address
rewind(fi); // go back to file beginning
FILE* out = fopen("copy output", "w"); // create the output file for writing
fwrite(fi,end-start,1,out); // write the input file to the output file
}
Should this work?
{
FILE* out = fopen("copy output", "w");
int* buf = malloc(end-start); fread(buf,end-start,1,fi);
fwrite(buf,end-start,1,out);
}
This isn't how fwrite works.
To copy a file, you'd typically allocate a buffer, then use fread to read one buffer of data, followed by fwrite to write that data back out. Repeat until you've copied the entire file. Typical code is something on this general order:
#define SIZE (1024*1024)
char buffer[SIZE];
size_t bytes;
while (0 < (bytes = fread(buffer, 1, sizeof(buffer), infile)))
fwrite(buffer, 1, bytes, outfile);
The first parameter of fwrite is a pointer to the data to be written to the file not a FILE* to read from. You have to read the data from the first file into a buffer then write that buffer to the output file. http://www.cplusplus.com/reference/cstdio/fwrite/
Perhaps a look through an open-source copy tool in C would point you in the right direction.
Here is How It can be done:
Option 1: Dynamic "Array"
Nested Level: 0
// Variable Definition
char *cpArr;
FILE *fpSourceFile = fopen(<Your_Source_Path>, "rb");
FILE *fpTargetFile = fopen(<Your_Target_Path>, "wb");
// Code Section
// Get The Size Of bits Of The Source File
fseek(fpSourceFile, 0, SEEK_END); // Go To The End Of The File
cpArr = (char *)malloc(sizeof(*cpArr) * ftell(fpSourceFile)); // Create An Array At That Size
fseek(fpSourceFile, 0, SEEK_SET); // Return The Cursor To The Start
// Read From The Source File - "Copy"
fread(&cpArr, sizeof(cpArr), 1, fpSourceFile);
// Write To The Target File - "Paste"
fwrite(&cpArr, sizeof(cpArr), 1, fpTargetFile);
// Close The Files
fclose(fpSourceFile);
fclose(fpTargetFile);
// Free The Used Memory
free(cpArr);
Option 2: Char By Char
Nested Level: 1
// Variable Definition
char cTemp;
FILE *fpSourceFile = fopen(<Your_Source_Path>, "rb");
FILE *fpTargetFile = fopen(<Your_Target_Path>, "wb");
// Code Section
// Read From The Source File - "Copy"
while(fread(&cTemp, 1, 1, fpSourceFile) == 1)
{
// Write To The Target File - "Paste"
fwrite(&cTemp, 1, 1, fpTargetFile);
}
// Close The Files
fclose(fpSourceFile);
fclose(fpTargetFile);

Resources