Thread: How can I make a program to read the first line "There_is_a_dog" consecutively?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    26

    How can I make a program to read the first line "There_is_a_dog" twice?

    Dear all,

    In my application, there are some sentences (each per line) to be read from the txt file, e.g.

    There_is_a_dog
    There_are_two_men
    ...

    I used fscanf_s to read them each per line (actually, it should be "each per white space"). However, how can I make a program to read the first line "There_is_a_dog" twice? E.g. after executing the first "fscanf_s" to read "There_is_a_dog", how can I guide the program to read "There_is_a_dog" again in the stream? Is there a way of doing that? I wanna google it, while haven't find any answer yet and it is hard for me to find some keywords to describe the problem.

    Hopefully, I described it crystal clear.

    Bests,

    Qing
    Last edited by qingxing2005; 06-16-2008 at 02:55 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would firstly suggest you're better off with fgets.
    Then you must use fseek to seek back in the stream, to before the There_is_a_dog.
    Then you can ready the same line again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by qingxing2005 View Post
    Hopefully, I described it crystal clear.
    Not really. Do you want to be able to read each line of the input file twice? Do you want to have a string with that line in it that you can read from? What?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    From what I understand:
    Read first line
    Seek back before first line
    (Read first line again)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >However, how can I make a program to read the first line "There_is_a_dog" consecutively?
    You can use fgets() to read a whole line:
    Code:
    while (fgets(line, sizeof line, fp) != NULL)
    {
    	printf("%s\n", line);
    }
    See the following FAQ entry: How do I... (Level 1) > Get a line of text from the user/keyboard (C)

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by tabstop View Post
    Not really. Do you want to be able to read each line of the input file twice? Do you want to have a string with that line in it that you can read from? What?
    Hey tabstop,

    I reformulated it a little bit. So my question is that if it is possible to read the line "THERE_IS_A_DOG" again after reading it by using fscanf_s?

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by Elysia View Post
    I would firstly suggest you're better off with fgets.
    Then you must use fseek to seek back in the stream, to before the There_is_a_dog.
    Then you can ready the same line again.
    Hey Elysia,

    In my application, since I have to extract the words "THERE","IS","A","DOG" respectively from the sentence "THERE_IS_A_DOG" according to "_", so is there a way of using "fscanf_s" to do it? Maybe it is too complex so I should not use fscanf_s...

    Bests,

    Qing
    Last edited by qingxing2005; 06-16-2008 at 03:18 PM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should read the entire line and split it via strtok.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by Elysia View Post
    You should read the entire line and split it via strtok.
    Hey Elysia,

    I figure it out, thanks for the hints. I used fscanf_s plus fseek.

    Bests,

    Qing

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Scanf is not the best function since it may leave crap in the input buffer and requires a hardcoded size when reading:
    Read more.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  3. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 12:35 PM
  4. how do you make a program send data to the printer?
    By Sintu in forum C++ Programming
    Replies: 19
    Last Post: 10-20-2005, 07:22 PM
  5. How do I read file line by line?
    By Blurr in forum C Programming
    Replies: 1
    Last Post: 09-22-2001, 12:32 AM