![]() |
|
#1
|
|||
|
|||
How to read a file line by line in C/UNIXHello 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
|
|||
|
|||
Re: how to read a file line by line in C/UNIXQuote:
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:
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
|
|||
|
|||
Re: How to read a file line by line in C/UNIXThank oyu Dave your reply was very helpful
|
|
#4
|
|||
|
|||
Re: How to read a file line by line in C/UNIXcan someone please explain to me the while (fgets (line_buffer line and how it read the file.. whats sizeof??
Daniel |
|
#5
|
||||
|
||||
Re: How to read a file line by line in C/UNIXQuote:
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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