LinuxQuestions.org
Help answer threads with 0 replies.
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 07-09-2009, 02:07 PM   #1
caverdave
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Rep: Reputation: 0
random file access from C


Hi,

I'm trying to find a way to open a file for random read/write access in C. Windows provides the O_RANDOM mode, but I can't seem to find anything on how to do this in Linux. Don't tell me you can't do this in Linux!

Thank you
 
Old 07-09-2009, 04:46 PM   #2
metrofox
Member
 
Registered: Jan 2009
Location: Palermo, Italy
Distribution: Slackware
Posts: 236

Rep: Reputation: 37
Quote:
Originally Posted by caverdave View Post
Hi,

I'm trying to find a way to open a file for random read/write access in C. Windows provides the O_RANDOM mode, but I can't seem to find anything on how to do this in Linux. Don't tell me you can't do this in Linux!

Thank you
If you talk about C and programming language you can do anything with Linux!
 
Old 07-09-2009, 04:48 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,517

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Are you refering to this?
From http://msdn.microsoft.com/en-us/libr...3z(VS.71).aspx
Quote:
The _open function opens the file specified by filename and prepares the file for reading or writing, as specified by oflag.

...

_O_RANDOM
Specifies that caching is optimized for, but not restricted to, random access from disk.
I don't believe there is an equivalent on Linux. This is just an optimization hint anyway, it doesn't affect the behaviour of your program. If you want actual random access you can use mmap.
 
Old 07-09-2009, 05:11 PM   #4
bannock
LQ Newbie
 
Registered: Jun 2009
Location: Toronto
Distribution: Ubuntu, Fedora
Posts: 24

Rep: Reputation: 18
Pretty much all file access is random access, unless you're reading something like a tape drive. Or maybe some special system files like /dev/random.
 
Old 07-09-2009, 07:33 PM   #5
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by bannock View Post
Pretty much all file access is random access, unless you're reading something like a tape drive. Or maybe some special system files like /dev/random.
True enough.

If you want to extract or insert data at an arbitrary point in a file, the fseek and ftell functions are your friends.
 
Old 07-10-2009, 04:29 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
With mmap() you can access a file just like an array. If you do a lot of random access to a file this can be very convenient en efficient. For huge files you may need to revert to fseek() and ftell(). See this thread for some mmap() links and an example.
 
Old 07-10-2009, 11:35 AM   #7
caverdave
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Thank you all.

Once I posted my question, I kind of realized that I was being dense. I was getting my signals crossed.
 
  


Reply

Tags
access, file, random


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 pick random file name from a list of filenames in a text file. pdklinux79 Linux - Newbie 9 06-20-2008 03:46 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 02:42 PM
Random file lines directed to a new file. In script an error. In command line no err leventis Programming 1 09-28-2006 08:16 AM
Strange file behavor using random access in C exvor Programming 4 05-19-2006 01:23 PM
C++ text stream random access Orkie Programming 6 10-26-2005 03:48 PM


All times are GMT -5. The time now is 07:14 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration