Thread: Reading random line from a text file

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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,210
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07: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, 07: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