LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-29-2011, 06:34 AM   #1
mot12
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
how to append in a existing file descriptor in linux and also insert a new line.


hello,

In my code i opened a file and write in it now i want each time i reopen it,data must be written from next line.
my code snippet is:

unsigned char buffer[8]={0};
int cmd=1;
int tt=100;
tt<<16;
tt|cmd;
sprintf((char*)buffer,"%d",tt);
FILE *fp;
fp=fopen("root/test.txt","w");
if(fp==NULL)
perror("file pointer:");
fwrite(buffer 8,1,fp);
fclose(fp);

I also searched but did'nt got any right answer.
Thanks in advance
 
Old 08-29-2011, 07:14 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
What you want to do is fopen to append; e.g.,
Code:
fp = fopen ("root/test/txt","a");
"a" will open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

"a+", on the other hand, will open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

Hope this helps some.
 
Old 08-29-2011, 07:15 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
What you want to do is fopen to append; e.g.,
Code:
fp = fopen ("root/test/txt","a");
"a" will open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

"a+", on the other hand, will open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

Hope this helps some.
 
Old 08-29-2011, 11:43 PM   #4
mot12
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tronayne View Post
What you want to do is fopen to append; e.g.,
Code:
fp = fopen ("root/test/txt","a");
"a" will open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

"a+", on the other hand, will open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

Hope this helps some.
thanks for reply but i want that my next stream will come at the beginning of the next line and so on.........please help me out.
 
Old 08-30-2011, 07:48 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Quote:
Originally Posted by mot12 View Post
thanks for reply but i want that my next stream will come at the beginning of the next line and so on.........please help me out.
That's precisely where it will be -- the "beginning of the next line" is right after the last line feed (line feed is the terminator of a line of text). If you write to the file, that's where you'll write.

If you were to write a little program something like this
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int     main    (void)
{
        FILE    *outfile;

        if ((outfile = fopen ("junk", "a")) == (FILE *) NULL) {
                (void) fprintf (stderr, "Can't write names\n");
                exit (EXIT_FAILURE);
        }
        /*      write the time on to the end of "junk"  */
        (void) fprintf (outfile, "%ld\n", time (0));
        exit (EXIT_SUCCESS);
}
compile it and execute it, wait a while, execute it again, wait a while, execute it again, then cat junk. From what you're saying, this will do exactly that.

Hope this helps some.
 
  


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 append text to the second line of a file SentralOrigin Programming 23 11-06-2010 07:19 PM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 09:14 PM
[Java] How to append something to each line in a text file ? Kunsheng Programming 3 04-15-2009 09:08 PM
how to append contents of one file to another existing file amit_pansuria Programming 1 11-17-2008 10:01 AM
'\n' eats line when stdout opened from file descriptor rsood Programming 4 08-31-2008 12:14 AM

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

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