LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-25-2009, 04:52 AM   #1
amani_87
LQ Newbie
 
Registered: Jul 2009
Posts: 3

Rep: Reputation: 0
open text file and read line by line


Hello,
i want to know how i can open text file and read line by line using C language under Fedora.
 
Old 11-25-2009, 07:06 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
That gets just a little bit tricky; i.e., what do you mean by "a line of text?" (and, no, I'm not being a smartypants, it matters). In Linux (and all Unix-like systems) a line of text is terminated by a line feed (and there's no carriage return). Windows, on the other, uses a carriage return and line feed pair to terminate lines (and you'll need to deal with that carriage return character). You may also have fixed-length records (where a "line" is exactly, for example, 128 bytes and there is no line termination character). Then, do you mean you want to read a line from a file, stop reading and do something or just read and display without stopping?

At its simplest, here is a sample that requires the file name to be in the source code (not a good way to do things, but it's easy for now). All this program does is open the file, read it and write the output on the standard output (that'll be your terminal window):
Code:
#include <stdio.h>
#include <stdlib.h>

int     main    (void)
{
        int     c;
        FILE    *infile;

        if ((infile = fopen ("your_text_file_name", "r")) == (FILE *) NULL) {
                (void) fprintf (stderr, "Can't open your_text_file_name\n");
                exit (EXIT_FAILURE);
        }
        while ((c = getc (infile)) != EOF)
                (void) putchar (c);
        (void) fclose (infile);
        exit (EXIT_SUCCESS);
}
If you save this program as, say, prog01.c, use a text editor and change all the your_text_file_name to your actual text file name and you would then enter
Code:
make prog01
and
prog01 (to execute it)
As you can imaging, this ain't the best way to do this.

Here's a better way: this is a sample program that accepts two arguments, an input file name and an output file name and it copies from the input file to the output file:
Code:
#include <stdio.h>
#include <stdlib.h>

int     main    (int argc, char *argv [])
{
        int     c;
        FILE    *infile, *outfile;

        if (argc != 3) {
                (void) fprintf (stderr, "%s:\tbad arg count\n", argv [0]);
                exit (EXIT_FAILURE);
        }
        if ((infile = fopen (argv [1], "r")) == (FILE *) NULL) {
                (void) fprintf (stderr, "%s:\tCan't read %s\n", argv [0], argv [1]);
                exit (EXIT_FAILURE);
        }
        if ((outfile = fopen (argv [2], "w")) == (FILE *) NULL) {
                (void) fprintf (stderr, "%s:\tCan't write %s\n", argv [0], argv [2]);
                exit (EXIT_FAILURE);
        }
        while ((c = getc (infile)) != EOF)
                (void) putc (c, outfile);
        (void) fclose (infile);
        (void) fclose (outfile);
        exit (EXIT_SUCCESS);
}
You could save this one as prog02.c, enter make prog02 and then execute it.

The first sample read from a file and displays what it read on your terminal screen; the second one is essentially a really simple version of the cp utility.

So, how do you handle winders files (that pesky carriage return character)? Well, in the while loop you can simply
Code:
        while ((c = getc (infile)) != EOF) {
                if (c == '\r')
                        continue;
                (void) putc (c, outfile);
        }
The braces ({}) are important! What this says is, if you see a carriage return, just go get the next character; i.e., don't "putc" that charcter on the output.

How about stopping at the end of a line? This is quick and dirty (which means you really don't want to do it too often)
Code:
        while ((c = getc (infile)) != EOF) {
                if (c == '\r')
                        continue;
                (void) putc (c, stdout);
                if (c == '\n') {
                        (void) fprintf (stdout, "Hit enter:");
                        (void) getc (stdin);
                }
        }
What that says is, if you see a carriage return ignore it, put the character you just read on the output, if that character is a line feed, put "Hit enter:" on the output and sit there and wait until the user hits the enter key.

Now, you really need to find yourself an introduction to C programming book (I recommend Steven Kochan's Programming in C, 3rd Edition (see http://www.kochan-wood.com); it's well written, covers all the basics (and a little more). Excellent book.

Hope this helps some.

Last edited by tronayne; 11-25-2009 at 07:11 AM.
 
1 members found this post helpful.
Old 01-06-2010, 01:07 AM   #3
Carsto
Member
 
Registered: Aug 2009
Posts: 97

Rep: Reputation: 17
My question in different words

I think what he means is how to access raw coding in a text editor. I searched and downloaded and did all else, yet I have still to see a coherent write-up on how to start simple coding in C or whatever. I know about Emacs and vi/m, but how do you do it?

You may type up a program and then how do you get it to compile in say C? Then how do you run it? How do you attach a desk top icon to it for instant use?

It's all very well to say that Linuxers are more knowledgeable than Windozers, but this simple stuff makes it unnecessarily difficult for newbies.

Yes, that reminds me of the Lattice C compiling environment I had in the 1980's on my Amiga A500. Editor, debugger the works, all in one. Just couldn't get it to compile since I had no proper support to solve problems. TCP/IP was a garage project then. Spent about US$10 grand and got nowhere. The US$1.00 then was ZAR0.95.

However the heady wine of freedom is highly intoxicating.
 
Old 01-06-2010, 01:37 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by Carsto
yet I have still to see a coherent write-up on how to start simple coding in C or whatever. I know about Emacs and vi/m, but how do you do it?

You may type up a program and then how do you get it to compile in say C? Then how do you run it?
Do u mean that u want to know how to compile and execute simple c programs in linux ?

If so look here http://www.linfo.org/create_c1.html

Quote:
Originally Posted by Carsto
How do you attach a desk top icon to it for instant use?
To attach a desktop icon, you may create a soft link w.r.t anything u want to access from the desktop !

Try this:
http://www.cyberciti.biz/faq/creatin...symbolic-link/

Last edited by Aquarius_Girl; 01-06-2010 at 01:40 AM.
 
1 members found this post helpful.
Old 01-13-2010, 03:01 AM   #5
Carsto
Member
 
Registered: Aug 2009
Posts: 97

Rep: Reputation: 17
Thanks, great help!

Anisha, Yes on both counts as in your links. After my last post I tried several other things yet could not get it to work. However; your hints will definitely be followed up.

This is great!! Fell around all over the place.
 
Old 01-13-2010, 03:18 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
I am glad I could help you !!
 
Old 01-13-2010, 03:30 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by Carsto
How do you attach a desk top icon to it for instant use?
Did u mean the following ?

There is a C file in your home folder and u want to attach a desktop icon to access that C file ?

If so, there is a way much simpler than creating a soft-link !

1. Right click on your desktop.
2. Select "Create URL link...".
3. In the URL, u should fill the exact path of your C file !

Last edited by Aquarius_Girl; 01-13-2010 at 03:33 AM.
 
Old 01-13-2010, 01:29 PM   #8
Carsto
Member
 
Registered: Aug 2009
Posts: 97

Rep: Reputation: 17
Create a link, right click on DT.

Anisha, that's where the frustration started. I installed the Evermore Integrated Office suite and created a link like that. But! but, but I got two other files; a tmp and another one with it on the desktop. That is in the Desktop Folders view. Fedora 11 was uninstalled in the meanwhile. Re-installed EIO and added an icon through right click on the kick off menu. Works.

Then did tar firefox.gz and it's all there, but no icon, even an "icon" file in CLI, nil else. Then tried to get one for Firefox through another method, but just got a hollow gear for an Icon and it did not open the app. Had to go Find Files as a quicker method than console. Click on the firefox-bin file to open it. Neither kick off or classic start menu has Firefox as an icon.

Just tried to get into the Firefox directory to make sure that I actually did do 'make install', but now it says there is no such file or folder.

Maybe download Fedora 12 to see of there are any changes in that installation package.
 
Old 01-14-2010, 02:40 AM   #9
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by Carsto
Then did tar firefox.gz and it's all there, but no icon, even an "icon" file in CLI, nil else. Then tried to get one for Firefox through another method, but just got a hollow gear for an Icon and it did not open the app. Had to go Find Files as a quicker method than console. Click on the firefox-bin file to open it. Neither kick off or classic start menu has Firefox as an icon.
I couldn't understand this!
Kindly elaborate!

Is Firefox already installed and running and u want to create a shortcut for it ?

OR

Firefox is not installed at all ?
 
Old 01-15-2010, 12:54 AM   #10
Carsto
Member
 
Registered: Aug 2009
Posts: 97

Rep: Reputation: 17
Firefox is installed but not running like in the Windows Start Menu. I'll put my money on it that I did not do the "make install" command after "make". It may be that this is the reason why the Firefox installation is not quite complete.

Yes, I do want a desktop icon. Actually, my Windows computer has ten toolbars with some 100 shortcuts. I do PC management and business directly from the Desktop. Haven't yet seen this facility on Fedora, but between the task tray and selected DT icons I can get by on Fedora. Then again, Linux does not need all that management, while the System Settings are more than enough to do excellent management. Here I need only Dolfin, Evermore Office, Firefox, Thunderbird and some small open source utitlities ("inside" Fedora) to do all I want. This is no Windows mentality. Linux will not compete if it cannot match or better Windows.

Computers aren't gods, they're tools - very quick idiots. Alas, one cannot throw this tool away if it does not work, things have become too fast already.

I'm new to this, so there would've been confusion in trying to get working habits embedded. I'm still writing down everything I do since it does happen that the thing crashes and you have to do it all over again. Trade-off between speed and learning.
 
Old 01-15-2010, 01:11 AM   #11
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
First thing is that, some version of firefox is supposed to be installed by default in fedora !

Open a terminal, type firefox, post the output here.

If firefox doesn't open this way, it means it is not installed there. The firefox executable is not in /bin.

From shell Navigate to the directory where u untared firefox .
Now type firefox and see what happens and tell the results .
 
Old 01-18-2010, 02:35 PM   #12
Carsto
Member
 
Registered: Aug 2009
Posts: 97

Rep: Reputation: 17
No firefox.

Yes, Fedora 3 had FF as part of it. FC 11 not so. Did "locate firefox" weeks ago and got no result. Before that I downloaded Firefox and the Bird and saved the files. They were installed and I had icons on the desktop. Used the saved files with this install to save downloads and to learn untarring.

Did what you asked with "firefox" and got "command not found" on both counts.

Then did "locate firefox" and got reams of stuff - all bits and pieces needed to make firefox work. There are two main directories shown:-

/home/cardi/.mozilla /firefox/.........
/home/cardi/usr/Firefox/firefox/.........

Then did cd /home/cardi
did "ls"
and got several directories and files yet no .mozilla there.

Then did cd /home/cardi/usr
did "ls"
and got Firefox Thunderbird.
I made these in an effort to get some organisation going. I untarred both apps in these. Hence the second /firefox.

Now when I do cd Firefox it gives me "No such file or directory".

It seems Linux is given to working very directory specific. I also tried yum remove firefox without any real conviction of getting results. Sure enough, there was an error about some name resolution not successful.

I think I will simply strip and start over.

One question please. In Konsole, how do I delete a File or Directory?
 
Old 01-18-2010, 10:56 PM   #13
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Don't despair,

Download Firefox 3.5.7 from the following link and save it in your home folder:
http://download.mozilla.org/?product...nux&lang=en-US

Open Konsole. Copy paste the EXACT following line on your shell prompt!
Code:
tar -xf firefox-3.5.7.tar.bz2
The above code shall untar the compressed firefox tar ball in your home folder and store it automatically in a folder named firefox

Copy paste the EXACT following lines one by one on your shell prompt!
Code:
su
ln -s /home/cardi/firefox/firefox /usr/bin/
Now type firefox on the terminal and tell the results

___Explanation of the above code___

Whenever you try to execute a file from the shell prompt, the shell tries to find the command in some special directories owned by the root.

The /usr/bin is one of those special directories!

Now to be able to run firefox from anywhere, you have to create a softlink in /usr/bin/ directory with respect to the executable to want to run from anywhere!

su
This asks u for the root password

ln -s /home/cardi/firefox/firefox /usr/bin/
And this creates a softlink in /usr/bin/ directory with respect to the executable file namely "firefox" stored in your home in the folder named firefox

To remove a file, type
Code:
rm -f filename -i
To remove a folder, type
Code:
rm -rf foldername -i
Check this out:
http://www.fsid.cvut.cz/cz/U201/linux.html

Last edited by Aquarius_Girl; 01-19-2010 at 12:00 AM.
 
1 members found this post helpful.
Old 01-18-2010, 11:57 PM   #14
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
If every thing above succeeds, then you can create a shortcut (softlink) for firefox on your desktop by the following code

Code:
ln -s /usr/bin/firefox ~/Desktop/
 
Old 01-20-2010, 01:56 AM   #15
Carsto
Member
 
Registered: Aug 2009
Posts: 97

Rep: Reputation: 17
Thanks Anisha, this really helps me very much. Do please give me some time to get to all this as I'm a bit pushed to earn a crust.

Yes, I did use the correct commands literally copied from some site. Did do my homework. Firefox actually works when I use KFind (the Find files and folders app I mentioned earlier as a speedy access method) on the GUI. It is the file /home/cardi/usr/Firefox/firefox/firefox-bin. But, I'm a sucker for running a slick box. Funny how old lessons stick. Our tutor warned against using a computer as a calculator.

I'd just like to see if I can create a link on the desktop from that icon in that dialog. Will let you know.

Despair I won't. Still a bit of a drag, yet it's amazing how the pathways are trampled open again.

Thanks, more than anything else, you save me huge amounts of time. This is very valuable.
 
  


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
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM
read line by line form text file in java. spank Programming 1 10-18-2006 02:46 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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