Thread: Function to read lines of text

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    17

    Function to read lines of text

    Could someone tell me which function shoud I use to read text from any txt file?
    With fscanf() I read data with specific format. I don't want that...I want to read just any text and output some strings in a specific way.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Typically,
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, sizeof buff, fp ) != NULL ) {
      // do stuff with a line in buff, which typically ends with \n\0
      // and always ends in \0
    }
    If you have unusually long lines, and this is important information, then you can easily detect whether fgets() has returned a 'short' line, and you can adjust accordingly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    In my program I want to read text from a file and inverse those words that are equal to a word given. I don't know the length of each line, so I can't assign a BUFSIZ which will always be enough.
    For example, if I read 1000 characters each time, how can I deal with the situation in which a word exists between line[998] and line[1002]? The word will be separated into two pieces and won't be checked correctly for equality with the word given.
    Last edited by Cevris; 05-12-2011 at 06:50 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    First what are you trying to do?

    With fscanf() I read data with specific format. I don't want that...I want to read just any text and output some strings in a specific way.
    'I want to read just any text' is not very helpful for us.
    You want to read entire line? or what?

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    I have a .txt file which includes text. It can have one line, or two or ten. Maybe it's only one word. In any case I want to check if any of the word(s) in the text are equal to a specific word given to me. I thought that I could read a line or 1000 chars each time using fgets() and the splitting the string into words using strtok(). But with that possible solution, I need to find a way to deal with the situation which I describe in my previous post.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    fgets reads text up to the size of the buffer provided, or until it hits a newline character. If it fills the buffer, there will be no newline in the buffer. At that point, you can allocate memory for a secondary buffer to hold what you have + some extra amount, copy what you have into the new buffer and read again into the original buffer. Repeat as necessary. When you've hit the newline, copy the final data into the secondary buffer and use it.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    words?
    Then use fscanf()?
    Or write your own function if you're not sure what'll be maximum length of word.

    eg.

    Code:
    while( fscanf(fp,"%100s",word) != EOF ) {
       if( strcmp(word, ..... ) == 0  ) {
      
       }
    }

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Quote Originally Posted by rags_to_riches View Post
    fgets reads text up to the size of the buffer provided, or until it hits a newline character. If it fills the buffer, there will be no newline in the buffer. At that point, you can allocate memory for a secondary buffer to hold what you have + some extra amount, copy what you have into the new buffer and read again into the original buffer. Repeat as necessary. When you've hit the newline, copy the final data into the secondary buffer and use it.
    Yes, that's clever enough. When fgets() hits a newline character, it stops reading right before '\n' without reading it or it reads that too?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If there is room in the buffer for the \n, then there will be a \n present, and it will be the last character in the buffer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  2. Best way to read lines out of a text file
    By movl0x1 in forum C Programming
    Replies: 9
    Last Post: 05-29-2007, 12:45 PM
  3. read lines
    By jujubeats in forum C++ Programming
    Replies: 18
    Last Post: 11-02-2006, 08:07 PM
  4. read a text file with lines of variable length
    By raymond in forum C Programming
    Replies: 7
    Last Post: 06-24-2006, 06:41 PM
  5. getline function to read 1 line from a text file
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:21 PM