View Full Version : Write a structure to a file
quanganht
March 6th, 2010, 03:29 PM
I'm new in C and I'm looking for help.
I have an structure, and I tried to write it to a file, binary mode. Neither of them works:
write(fd, &desc, sizeof(desc))
and
fwrite(&desc, sizeof(desc), 1, fd)
Nothing get written to file.
napsy
March 6th, 2010, 03:31 PM
How do you open the file?
MadCow108
March 6th, 2010, 03:33 PM
please show more code.
those lines seem correct but are probably used wrong in a greater context
quanganht
March 6th, 2010, 03:35 PM
The whole damn thing:
#include <stdio.h>
struct SB_DESC
{
char magic[4];
unsigned int index_num;
unsigned long data_num;
unsigned long total_num;
char name[8];
}
int main()
{
int c,i;
void *code;
FILE *fd, *boot;
struct SB_DESC desc;
printf("Building floppy image with BSFS...\n");
fd=fopen("floppy.img","wb");
boot=fopen("boot","rb");
if (boot==NULL)
{
printf("boot binary does not exist!");
exit(0);
}
while ((c=fgetc(boot))!=EOF) // copy boot binary
{
fputc(c,fd);
}
fclose(boot);
//TODO: add code for inserting missing datas and kernel files.
desc.magic[1]='B';
desc.magic[2]='S';
desc.magic[3]='F';
desc.magic[4]='S';
desc.index_num=32;
desc.data_num=2847;
desc.total_num=2880;
for (i=0;0<=8;i++)
desc.name[i]=' ';
if (fwrite(&desc, sizeof(desc), 1, fd) == NULL)
printf("ERROR!\n");
fputc(0x55,fd); fputc(0xAA,fd);
fflush(fd);
fclose(fd);
printf("Done!\n\n");
}
quanganht
March 6th, 2010, 03:56 PM
I got it working now. Thanks guys for helping.
Close thread plz.
SledgeHammer_999
March 6th, 2010, 05:22 PM
Maybe post the solution for others to benefit too?
MadCow108
March 6th, 2010, 05:58 PM
theres a buffer overrun here:
desc.magic[4]='S';
and an infinite loop (+buffer overrun) here:
for (i=0;0<=8;i++)
desc.name[i]=' ';
and a few signed/unsigned comparisons
otherwise the code should work
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.