how to append in a existing file descriptor in linux and also insert a new line.
ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
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.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
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.
"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.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Quote:
Originally Posted by mot12
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.