GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Dec-2008, 11:50
james_carter james_carter is offline
New Member
 
Join Date: Dec 2008
Posts: 10
james_carter is on a distinguished road

How to read a file line by line in C/UNIX


Hello everybody
i need to read a text file line by line in unix system programming. i know that to read a file we need to use the read (fd, buf, BUF_SIZE) function but how to read this file line by line. do i need to use the getline() function?
thank you
  #2  
Old 03-Dec-2008, 12:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: how to read a file line by line in C/UNIX


Quote:
Originally Posted by james_carter
Hello everybody
i need to read a text file line by line in unix system programming. i know that to read a file we need to use the read (fd, buf, BUF_SIZE) function but how to read this file line by line. do i need to use the getline() function?
thank you

The low level read() function is not a standard C library function. It has been there "forever," and, in fact I thank that the low level functions (open(), read(), write(), etc.) are used to implement the standard functions fopen, fgets, printf, etc., but it is not an official part of the standard C library, and, therefore, applications that use this may not be very portable.

I think that most programmers stick with standard functions: Open with fopen(), read lines with fgets(). Stuff like that.

Here's a program to read all lines of a file and print them out with line numbers. A summary of the number of lines is printed:
CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    char *inname = "test.txt";
    FILE *infile;
    char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
    char line_number;

    infile = fopen(inname, "r");
    if (!infile) {
        printf("Couldn't open file %s for reading.\n", inname);
        return 0;
    }
    printf("Opened file %s for reading.\n", inname);

    line_number = 0;
    while (fgets(line_buffer, sizeof(line_buffer), infile)) {
        ++line_number;
        /* note that the newline is in the buffer */
        printf("%4d: %s", line_number, line_buffer);
    }
    printf("\nTotal number of lines = %d\n", line_number);
    return 0;
}

This works with compilers for Windows platforms, Linux platforms, and anything else that has I/O that complies with the standard language and library implementation.

Regards,

Dave

Footnote: The getline() function is not in the C standard library. If your system has low-level I/O functions (open(), read(), etc.), you can certainly write a getline() function that uses low-level read() operations. You can also write any other convenient functions (scanf(), printf(), etc.) using low-level read() and write(), but the work has already been done with the standard library buffered I/O functions.
  #3  
Old 05-Dec-2008, 12:34
james_carter james_carter is offline
New Member
 
Join Date: Dec 2008
Posts: 10
james_carter is on a distinguished road

Re: How to read a file line by line in C/UNIX


Thank oyu Dave your reply was very helpful
  #4  
Old 12-Dec-2008, 20:22
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

Re: How to read a file line by line in C/UNIX


can someone please explain to me the while (fgets (line_buffer line and how it read the file.. whats sizeof??

Daniel
  #5  
Old 12-Dec-2008, 22:28
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,377
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: How to read a file line by line in C/UNIX


Quote:
Originally Posted by nowocien
can someone please explain to me the while (fgets (line_buffer line and how it read the file.. whats sizeof??

Daniel
When you looked up the functions, what was it you didn't understand?
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 20:03
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 03:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:59.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.