Thread: getting a line from a file...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    getting a line from a file...

    I want to know how to get a line from a file with read, because I had to use open for the file I have. I am trying though(sorry about all the questions) here is what I have but something isn't working
    Code:
     while((read(back_file_name,back_buffer,(1*(sizeof(char)))))!='\0'){
            }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Most people just use fgets. The FAQ also covers reading a line using fgets. You could also use fgetc if you want one character at a time.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Yeah but that is from a *stream. I used open so it is a file descriptor as an int. you know
    Code:
    int=open("file",O_RDWR);
    It isn't a *stream. What do I do?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just like fgets...
    Code:
    int bytesread = 0;
    
    bytesread = read( filedescriptor, buffertoreadinto, sizetoread );
    Always check your return value, and make sure it's what you expect it to be. Then handle the contents of the buffer, and repeat the process.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    yeah but how do I figure out sizetoread. The size of the line I can't do strenlen(line) because I don't have the line yet??

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by linuxdude
    yeah but how do I figure out sizetoread. The size of the line I can't do strenlen(line) because I don't have the line yet??
    He's trying to say you can't with a file descriptor. You'll have to read a character at a time until you read the EOL character.

    Or as suggested, change to file streams.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    thanx I figured it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM