LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2009, 03:22 PM   #1
justaguy_75ae
Member
 
Registered: May 2008
Posts: 42

Rep: Reputation: 16
fread and fwrite errors with mapped memory


I am learning about mapping files in to memory and I saw in an example that you would point a charter pointer to the address that mmap returned and you could use putchar to display the contents of the file.


I got to thinking what about a a file with a link list so i put a file pointer to the address that mmap returned and tried to use fread and fwrite on it and I kept getting segment faults....

the code for reading the linked list works if not using mmap but i was curious on how to get fread and fwrite to work with mapped files, or why it will not work with mapped files.
 
Old 09-08-2009, 12:11 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Sounds like you don't get it what mmap() actually is.

mmap() is an alternative way to read/write files. Alternative to fread()/fwrite().

Once a file is mmap()-ed reading and writing to the file is much more convenient than using read()/write()/fread()/fwrite().

You can read/write bytes to the files just by reading/writing an array of bytes. This is much more easier/convenient than fread()/fwrite() and/or linked lists.

On the downside opening and mmap()-ing the file is little more programming work. Also it may be a little slower than read()/write(), and appending to a file can be a bit troublesome.

So why making your program more complicated and slower by laying a linked list on top of an array which is more convenient than linked list already. And why fread()/fwrite() when you can just read/assign values to an array (which will end up inside the file at the right position)?

Or to put it the other way around: why mmap() a file if you prefer to fread()/fwrite() to it?

Just choose: Either read/write to/from an array (which actually is the bytes in the file), or fread()/frwite() the "old-fashioned" way.

Here is some example code for reading/writing to a file as an array of bytes.

Last edited by Hko; 09-08-2009 at 12:12 PM.
 
Old 09-08-2009, 01:44 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
In addition to the useful info from Hko, here's another way to look at it. fread and fwrite provide linear access to a file, whereas mmap provides random access. Additionally, fread is a wrapper in the C library that implements a buffer on top of read. In between fread and read, the C library interprets the input to allow it to be treated as text, e.g. for use with fgets. In the same way you can't mix mmap and fread, you can't mix read and fread. This is because fread assumes it's the only thing accessing the contents of the file; if that isn't the case, things don't work as they should (as you've seen.)
Kevin Barry
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to access Physical memory not mapped in kernel sbc Programming 0 02-09-2009 09:13 AM
Kernel crashes while accessing the IO mapped memory rohshall Linux - Embedded & Single-board computer 0 12-10-2008 09:52 AM
best way of using fread and fwrite rohanak Programming 1 05-02-2008 07:26 AM
C programming fread and fwrite using structures exvor Programming 4 09-26-2006 08:56 AM
Memory Mapped file IO problems legogt Programming 0 08-01-2003 01:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration