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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-09-2009, 01:07 PM
|
#1
|
|
LQ Newbie
Registered: Jul 2009
Posts: 9
Rep:
|
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
|
|
|
|
07-09-2009, 03:46 PM
|
#2
|
|
Member
Registered: Jan 2009
Location: Palermo, Italy
Distribution: Slackware
Posts: 236
Rep:
|
Quote:
Originally Posted by caverdave
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! 
|
|
|
|
07-09-2009, 03:48 PM
|
#3
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,015
|
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.
|
|
|
|
07-09-2009, 04:11 PM
|
#4
|
|
LQ Newbie
Registered: Jun 2009
Location: Toronto
Distribution: Ubuntu, Fedora
Posts: 24
Rep:
|
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.
|
|
|
|
07-09-2009, 06:33 PM
|
#5
|
|
Member
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Rep:
|
Quote:
Originally Posted by bannock
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.
|
|
|
|
07-10-2009, 03:29 AM
|
#6
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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.
|
|
|
|
07-10-2009, 10:35 AM
|
#7
|
|
LQ Newbie
Registered: Jul 2009
Posts: 9
Original Poster
Rep:
|
Thank you all.
Once I posted my question, I kind of realized that I was being dense. I was getting my signals crossed.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:42 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|