delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/09/04:38:14

Xref: news2.mv.net comp.os.msdos.djgpp:3611
From: Rikard Thunberg <thunberg AT algonet DOT se>
Newsgroups: comp.os.msdos.djgpp
Subject: i/o file handling - integers
Date: Mon, 06 May 1996 23:40:13 +0200
Organization: Informia
Lines: 18
Message-ID: <318E71BD.904@algonet.se>
NNTP-Posting-Host: bengt.algonet.se
NNTP-Posting-User: a37bfb8671f5d77b3
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

What is the appropriate way to read/write integers from/to a file with the same size as it is stored in 
memory, i.e. a long integer (32bit) would occupy 4 bytes in the file. I have reached a solution but it seems 
rather clumsy:

#include<stdio.h>
void main()
{
 FILE *fileh;
 unsigned long my_long=0x41424344;
 char *charpek;
 fileh = fopen("test.dat","wb");
 charpek= (char *) &my_long;
 fprintf(fileh,"%c%c%c%c",*charpek,*(charpek+1),*(charpek+2),*(charpek+3));
 fclose(fileh);
}


/Rikard Thunberg, thunberg AT algonet DOT se

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019