Reading random line from a text file

This is a discussion on Reading random line from a text file within the C Programming forums, part of the General Programming Boards category; The opening and closing seems to be working, but could someone guide or give me a suggestions as to how ...

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    26
    The opening and closing seems to be working, but could someone guide or give me a suggestions as to how to store which lines have been used? Perhaps if I stored the indexes?

  2. #17
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by helloamuro View Post
    I can't use rewind (I'm familiar with it, but it hasn't been covered in my class) but would just opening and closing the file again be sufficient?
    If you cannot use rewind or fseek - then fclose/fopen will do the trick
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #18
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by helloamuro View Post
    The opening and closing seems to be working, but could someone guide or give me a suggestions as to how to store which lines have been used? Perhaps if I stored the indexes?
    You will need 2 dimantional array - to store each line in one member of this array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    26
    Or is there anyway I can just use a flag to tell if the line has been used or not?

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    26
    I don't really want to use a 2d array because I have been so often criticized for trying to use them before.

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    26
    I currently have this for getting the line number. What could I add to it to prevent the same one from being used again
    Code:
    while(fgets(temp,100,phraseFile)!=0){
    			++count;
    		}
    		
    		fclose(phraseFile);
    		fopen("clues.txt", "r");
    		line= rand()%count;
    		
    		while(line--){
    			fgets(temp, 100, phraseFile); /*gets line and stores in temp*/
    		}

  7. #22
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    seems to be ok except
    fopen("clues.txt", "r"); - you need to store the return value again

    bytheway - if line == 0 you will be left with the last read line in the buffer...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #23
    Registered User
    Join Date
    Apr 2008
    Posts
    26
    Would the line ==0 work though if my program is in a loop to be performed three times though? It seems like there could be a change that the first and third rounds of the program could use the same word...

  9. #24
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Elysia View Post
    Otherwise, you can do as MacGyver initially suggested: open the file and read everything into memory and deal with it from there.
    Whoa, how did I miss this? Elysia agreed with me for once.

    /me writes down the date and time.

  10. #25
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by helloamuro View Post
    Would the line ==0 work though if my program is in a loop to be performed three times though? It seems like there could be a change that the first and third rounds of the program could use the same word...
    I suppose you need to add line++
    before your
    while(line--) loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 06:13 PM
  4. end of line in text file
    By spveer in forum C Programming
    Replies: 5
    Last Post: 08-18-2005, 12:43 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21