Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uflorida!rex!samsung!uunet!overload!dillon From: dillon@overload.Berkeley.CA.US (Matthew Dillon) Newsgroups: comp.sys.amiga.tech Subject: Re: I/O of complex data structures in C Message-ID: Date: 6 Aug 90 21:48:11 GMT References: <140087@sun.Eng.Sun.COM> <25884@mimsy.umd.edu> Lines: 48 >In article <25884@mimsy.umd.edu> chris@mimsy.umd.edu (Chris Torek) writes: >In article <140087@sun.Eng.Sun.COM> jasonf@cetemp.Eng.Sun.COM >(Jason Freund) writes: >>"fwrite(pointer, sizeof, *pointer, items, stream)" which somehow magically >>saves every piece of data (specified in the arguments) in such a way that it >>will be able to read in every piece of data back into their correct cells in >>the data structure? > >No. > >The primary rule of magic is this: `There is no magic'. Fread and fwrite > ... fwrite(ptr, blockSize, numBlocks, fi); const void *ptr; pointer into memory, fwrite does not know or care what the pointer is actually pointing to! const means the memory is not modified by fwrite() (obvious). int blockSize; fwrite writes blockSize * numBlocks bytes int numBlocks; of data from the memory location given by 'ptr' to the file, no interpretation of the data is done. FILE *fi; da file. Why am I interjecting what has already been said? Because nobody has yet explained the one point that confused the hell out of me when I first started learning C, and that is WHY are there two size arguments?!! fwrite() simply multiplies numBlocks * blockSize to determine how many bytes to write to the file. The ONLY difference between these two arguments is that fwrite() returns the number of successful blocks that were written, i.e. it returns bytes_written_without_error / blockSize or a negative number on total error. I do not even think fwrite() tries to keep low level flushes (write()s) on block boundries. -Matt -- Matthew Dillon dillon@Overload.Berkeley.CA.US 891 Regal Rd. uunet.uu.net!overload!dillon Berkeley, Ca. 94708 USA