LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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-14-2005, 09:22 AM   #1
omerlh
LQ Newbie
 
Registered: Oct 2005
Location: Israel
Distribution: Mandriva
Posts: 14

Rep: Reputation: 0
Write to file


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.
 
Old 10-14-2005, 09:42 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
example?

using what? C or a unix tool or don't care?
 
Old 10-15-2005, 11:30 AM   #3
omerlh
LQ Newbie
 
Registered: Oct 2005
Location: Israel
Distribution: Mandriva
Posts: 14

Original Poster
Rep: Reputation: 0
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.

Last edited by omerlh; 10-15-2005 at 11:32 AM.
 
Old 10-15-2005, 12:52 PM   #4
destuxor
Member
 
Registered: Oct 2005
Posts: 51

Rep: Reputation: 16
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.
 
Old 10-15-2005, 01:34 PM   #5
omerlh
LQ Newbie
 
Registered: Oct 2005
Location: Israel
Distribution: Mandriva
Posts: 14

Original Poster
Rep: Reputation: 0
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.
 
Old 10-17-2005, 03:39 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
what is it for? business or pleasure.

you'll need to write a parser.
If you don't know C it will not be easy.
It's not a beginner problem.

what about using dc
 
Old 10-17-2005, 04:02 AM   #7
destuxor
Member
 
Registered: Oct 2005
Posts: 51

Rep: Reputation: 16
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;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
grip : no write access to write encoded file bidouilleur Linux - Software 5 10-09-2010 09:23 PM
Can not write to file in Java irfanhab Programming 7 12-05-2005 12:01 AM
forrtl: severe (47): write to READONLY file, unit 5, file /dev/pts/1 terrence Programming 1 10-01-2005 10:22 PM
How do I write to the beginning of a file? abefroman Programming 1 06-30-2005 11:56 AM
Grip- "no write access to write encoded file" Alvis Linux - Software 4 01-06-2004 04:18 PM


All times are GMT -5. The time now is 09:32 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration