Writing structure to a file in c

This is my code where I read 256 bytes in "example.txt" and saved it in chunk.payload.

I want to write my structure first in "write.txt" and then write the content that I read, "write.txt". I was able to use fwriteto write the entire chunk.payload file to a file, but it is difficult for me to write the entire structure to a file.

#define MAXSIZE 256
int main(void){
    FILE *fp = fopen("C:\\Users\\alice\\Desktop\\example.txt", "r");
    FILE *wr = fopen("C:\\Users\\alice\\Desktop\\write.txt", "w+");

    struct packet{
      unsigned short block_num;
      unsigned short block_size;
      unsigned short crc;
      unsigned char *payload;
    };

    /*Create A dummy packet */
    struct packet chunk;
    chunk.block_num = 0;
    chunk.block_size = 256;
    chunk.crc = 0x101001;
    chunk.payload = malloc(MAXSIZE + 1);  // allocating memory
    chunk.payload[MAXSIZE] = '\0';

    //read first 256 lines and store into chunk.payload
    int read = fread(chunk.payload, sizeof(char), MAXSIZE, fp);  

    // Write struct to write.txt
    fwrite(&chunk, sizeof(chunk), 1, wr);

    // Write whatever has been read so far to write.txt
    fwrite(chunk.payload, sizeof(char), read, wr);

    getch();
    fclose(fp);
    fclose(wr);
    return 0;

How I wrote my call:

fwrite(&chunk, sizeof(chunk), 1, wr);

actually allowed me to compile the code, but in the end I had random characters written to a file before the 256 bytes I read were written to my file

What is the correct method for doing this?

+3
source share
4 answers

Your first file is trying to write the binary contents of the package structure to the write.txt file:

// Write struct to write.txt
fwrite(&chunk, sizeof(chunk), 1, wr); 

:

// Write whatever has been read so far to write.txt
fwrite(chunk.payload, sizeof(char), read, wr); 

malloc'd. , . , - fprintf , .

// write contents of 'chunk' to text file
fprintf( wr, "block_num= %d\n", chunk.block_num );
...
fwrite(chunk.payload, sizeof(char), read, wr);

, , MAXSIZE.

+1

- , . . , write.txt

,

typedef struct Test
{
int a,b,c, d;
}Ref;

main()  {

 fp=fopen("/home/test_prog/sampe.txt", "w+");
 sprintf (data, "%d %d %d %d", example.a, example.b, example.c, example.d);
 fwrite (data, sizeof(char), strlen(data) , fp  );
 fclose (fp);

}

0

fread fwrite / . , fscanf fgets , fprintf .

0

chunk . "" , . MAXSIZE, :

struct packet{
  unsigned short block_num;
  unsigned short block_size;
  unsigned short crc;
  unsigned char payload[MAXSIZE];
};

: , .. fwrite , fwrite .

, :

struct packet{
  unsigned short block_num;
  unsigned short block_size;
  unsigned short crc;
  unsigned char payload[0];
};

/*Create A dummy packet */
struct packet *chunk;
chunk = (struct packet *)malloc(sizeof(struct packet) + packetsize + 1);
chunk->block_num = 0;
chunk->block_size = 256;
chunk->crc = 0x101001;
chunk->payload[packetsize] = '\0';

fwrite, :

// Write struct to write.txt
fwrite(chunk, sizeof(*chunk) + packetsize, 1, wr);

. fwrite.

Also, be sure to add one for the NUL terminator if you want to keep it too.

0
source

All Articles