Thread: My program don't read all lines of a text file

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    My program don't read all lines of a text file

    I don't know why my program is reading only first line. The text.file has 2 lines actually.

    The first line is printing.

    But the second line is not printing
    The output is always:

    The first line is printing.

    Here is my program:

    Code:
    #include<stdio.h>
    int main(){
    
    
        FILE *in=fopen("satu.txt","rt");
        char hello[100];
        
        fgets(hello,60,in); /*what is the use of in? Why writing in this format?*/
        
        printf("first line of \"text.txt\": %s", hello); 
        
        fclose(in); /*closing file*/
    
    
        return 0;
    
    
    
    
    }
    I am now learning how to read from a file. But here I don't know what is the use of in . I found this in a tutorial. If I delete it, the program doesn't work.
    Last edited by Netcode; 04-13-2012 at 07:24 PM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    well to start, "in" is the name of the FILE pointer, so without it there will be syntax errors. Also, are you sure you mean "rt" for the mode or do you mean "r+"?

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Quote Originally Posted by camel-man View Post
    well to start, "in" is the name of the FILE pointer, so without it there will be syntax errors. Also, are you sure you mean "rt" for the mode or do you mean "r+"?
    I am not sure. I saw that wt is used for writing in a new file. So, might be rt is used for reading. What is the use of r++?

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    r - open for reading
    w - open for writing (file need not exist)
    a - open for appending (file need not exist)
    r+ - open for reading and writing, start at beginning
    w+ - open for reading and writing (overwrite file)
    a+ - open for reading and writing (append if file exists)

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Quote Originally Posted by camel-man View Post
    r - open for reading
    w - open for writing (file need not exist)
    a - open for appending (file need not exist)
    r+ - open for reading and writing, start at beginning
    w+ - open for reading and writing (overwrite file)
    a+ - open for reading and writing (append if file exists)
    Thank you. I changed rt to r+. But still the program is reading only the first line.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    fgets stops reading after new line or EOF, which ever comes first. If you want to read more than one line, you have to use a loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read lines of text
    By Cevris in forum C Programming
    Replies: 8
    Last Post: 05-12-2011, 07:19 AM
  2. 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
  3. 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
  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. Replies: 5
    Last Post: 02-01-2003, 10:58 AM