LinuxQuestions.org
Visit Jeremy's Blog.
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 03-08-2011, 08:08 AM   #1
derchris
LQ Newbie
 
Registered: Feb 2011
Posts: 27

Rep: Reputation: 0
C: Manipulating file adresswise


Hey,

I'm looking for a special fileoperation. As we know, we can open a file as FILE * and than handle it as a stream. The problem is: In that way I can write and read only at the beginning (or write at the end).
But want I want to do, is to manipulate a special point within the file.
Example:
Assume my file stars at address 0, then I want to insert my 10 byte values at offset 526, so I only overwrite byte 526-536 and let the rest of the file unchanged.

The only idea I came up with was the read the whole file, write it back into a new file (with the sections I wanted being changed) and then delete the old file and replace it with the new one. But this solution is not very elegant.
So I wonder whether there is a better solution for me.
All I need is actually an address/pointer to the beginning of the file. Using memcpy it should not be a problem to overwrite the part of the file then.
Note: I'm not talking about textfiles, but binaries.
 
Old 03-08-2011, 08:12 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
start with
Code:
man fseek
Read that and some of the other functions pointed to from there. That should give you a good idea of your choices using file I/O.

http://www.cplusplus.com/reference/c.../cstdio/fseek/

Quote:
Originally Posted by derchris View Post
All I need is actually an address/pointer to the beginning of the file. Using memcpy it should not be a problem to overwrite the part of the file then.
That is also possible (map a file into your process's address space, so you can read and write it as memory). I'm guessing that isn't best for your needs. But if you think it might be, look at the man page for mmap.

I also did a google search for "mmap examples" and the top choice was this thread at LQ
http://www.linuxquestions.org/questi...5/#post2547332

Last edited by johnsfine; 03-08-2011 at 08:23 AM.
 
1 members found this post helpful.
Old 03-08-2011, 09:17 AM   #3
derchris
LQ Newbie
 
Registered: Feb 2011
Posts: 27

Original Poster
Rep: Reputation: 0
Thank you, I guess using fseek I'll get the job done pretty easily
 
  


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
Accessing file, and manipulating with with folders adamelody Programming 11 03-14-2010 10:32 AM
Need help manipulating a text file lt1776 Linux - Newbie 4 02-16-2010 11:11 AM
GPG - encode file while manipulating output file name itmozart Linux - Newbie 2 10-03-2009 12:28 PM
Manipulating Fields Of Flat File Prasun1 General 10 09-29-2005 11:42 AM

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

All times are GMT -5. The time now is 03:34 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