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 10-30-2010, 11:11 AM   #1
Basel
Member
 
Registered: Feb 2004
Location: United States
Distribution: Ubuntu 10.10
Posts: 319

Rep: Reputation: 30
How to write using fwrite without affecting existing content of file?


How can I write to a file multiple times using fwrite without affecting the previous writes?

The method shown below accepts a file name, buffer and offset. The method opens the file in reading/writing mode and writes the content of the buffer at offset.

Code:
int writeData(char *filename, char *buffer, int offset){
    FILE *fp;
    fp = fopen(filename, "w+"); // this truncates the file :(
    fseek(fp, offset, SEEK_SET);
    fwrite(buffer, sizeof(char), strlen(buffer), fp);
    int pos = ftell(fp);
    fclose(fp);
    return pos;
}

void main(){
    writeData("alpha", "ABCDEFGH", 0);
    writeData("alpha", "WXYZ", 2);
}

// content of file: [^@^@WXYZ]
However, the intended behavior should be "ABWXYZGH." Using the "r+" mode gives the intended behavior if the file exists, which is not always the case. As a workaround, I try to open in "r+" first and create the file if it does not exist:

Code:
int writeData(char *filename, char *buffer, int offset){
    FILE *fp;
    fp = fopen(filename, "r+");
    if (fp == NULL) {
        fp = fopen(filename, "w");
        if (fp) {
            fclose(fp);
            fp = fopen(filename, "r+");
        } else // do not have sufficient permission
            return -1;
    }
    fseek(fp, offset, SEEK_SET);
    fwrite(buffer, sizeof(char), strlen(buffer), fp);
    int pos = ftell(fp);
    fclose(fp);
    return pos;
}
I am not sure that's the right way of doing it.
 
Old 10-30-2010, 11:13 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Use fopen(filename, "a") for appending at the end or replacing somewhere in the middle. But if you want to insert some bytes in the middle, you must read everything from insertion point to the buffer and then seek to insert position and write new bytes and buffer.

Last edited by eSelix; 10-30-2010 at 11:22 AM.
 
  


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
trying to append data to a file with php/fwrite steve51184 Linux - Server 2 08-16-2010 08:35 PM
Does fwrite() write entire file on jffs2? bjj2282 Linux - Software 0 10-26-2007 10:03 AM
Changing the content of already existing file superandrzej Slackware 3 02-02-2007 09:06 AM
Slow file writing with 'fwrite' function in C-code... scho Programming 5 08-07-2006 12:42 AM
LXer: How to write an article - Content LXer Syndicated Linux News 0 02-26-2006 04:01 PM

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

All times are GMT -5. The time now is 01:44 AM.

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