Thread: read a line from file

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    5

    read a line from file

    Hi...

    I have a file myfile:
    Fri Apr 01 23:28:17 SGT 2005,hello
    Fri Apr 01 23:28:22 SGT 2005,how
    Fri Apr 01 23:28:22 SGT 2005,are
    Sat Apr 02 01:34:03 SGT 2005,you
    Sat Apr 02 01:34:07 SGT 2005,?

    How can i read a particular line from it?
    I tried to use this to read the first line:
    Code:
    char line[64];
    FILE *f = fopen("myfile", "rw");
    
    fscanf(f, "%s\n", &line);
    But it does not work. The string is empty...

    Anyone know how to do? Thank you.

  2. #2
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    Well when you use fscanf with a char array u can just send the variable not the address.
    Code:
    fscanf(f, "%s", line);
    That will only read until it reaches white space so Fri will be put into line.

    If you want to read an entire line into an array of 64 use fgets
    Code:
    while(fgets(line, 63, f) != 0){}
    That will read the entire file line by line.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    5
    Quote Originally Posted by stumpster123
    Well when you use fscanf with a char array u can just send the variable not the address.
    Code:
    fscanf(f, "%s", line);
    That will only read until it reaches white space so Fri will be put into line.

    If you want to read an entire line into an array of 64 use fgets
    Code:
    while(fgets(line, 63, f) != 0){}
    That will read the entire file line by line.
    It seems that i can only read the first word Fri from the first line...

    And i dont know how long the line is, so i also cannot use fgets..

    How can i do? Thank you..

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by DogsCanDrive
    And i dont know how long the line is, so i also cannot use fgets..
    For that same reason you cannot safely use %s with fscanf either. Choose a size, use fgets. *Wait* you already have chosen a size; use fgets.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    suppose u wanted to do xyz to each line...

    Now try proceeding this way :

    1 - open the file.

    2 - Do till EOF is reached

    get character and put it in an array till the '\n' character is reached.
    U've got ur string. Do xyz to it.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by just_A_student
    2 - Do till EOF is reached
    FAQ bait.
    Quote Originally Posted by just_A_student
    get character and put it in an array till the '\n' character is reached.
    Like fgets?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    12
    Sorry ! Got the point !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OPen a file and read it from the last line
    By c_geek in forum C Programming
    Replies: 14
    Last Post: 01-26-2008, 06:20 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. 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
  4. how can i read i line from a file
    By condorx in forum C Programming
    Replies: 2
    Last Post: 05-07-2003, 02:47 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM