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.
Hello!
I wnat to know how can I write into a file with any editor, than, the program can add a line at this file in place that was determined by me.
I try to find out at the net how to do it, but I find out just how to open file. I try to search here, and I don't find answere.
i mean, that the program get a string from the user, than,.it can open a file, put the string in line number x. That's all.
What you mean? How I do it in the program? From what I read use C is faster.
Distribution: Gentoo (desktop and server), Ubuntu (laptop), RedHat and Solaris (work)
Posts: 51
Rep:
The "sed" UNIX command can do that. It's not easy to get your mind wrapped around, but once you understand how to use sed it'll be a lot more useful to you than writing a single program to perform exactly one function.
I need to do it in linux. I need it for program that can get from user something like:
3*pow(x, 2)+2x -4=y.
And the program can take that thing, and solve it. I know how to solve it, the problem is how to use the string. I thugh on writing it in file, that solve it.
So I want to know how to do it in C.
Distribution: Gentoo (desktop and server), Ubuntu (laptop), RedHat and Solaris (work)
Posts: 51
Rep:
Personally, I prefer Perl. In this case, C would work and might even be an appropriate choice, but Perl is much safer and almost as fast (use google if you want to find comparison data, but I can tell you for a program like this the difference isn't significant).
In Perl (note that I didn't test this code, it should work fine, but if it doesn't tell me and I'll look at it):
Code:
#!/usr/bin/perl -w
use strict;
use warnings;
print "What file do you want to open/write to? ":
my $file = <stdin>;
print "What line to you want to insert text at? ";
my $line = <stdin>;
print "What text do you want to insert?? ";
my $text = <stdin>;
open (FH, $file) or die "Cannot open ".$file." for reading: $!\n";
my @allLines = <FH>;
close FH;
open (FH, "> ".$file) or die "Cannot open ".$file." for writing: $!\n";
my $lineNumber = 0;
for ( ; $lineNumber < $line ; $lineNumber++)
{
print OUT $allLines[$lineNumber];
}
print OUT $text."\n";
for (; $lineNumber < $allLines ; $lineNumber++)
{
print OUT $allLines[$lineNumber];
}
close OUT;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.