Thread: read data out of *.txt file

  1. #1
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140

    Arrow read data out of *.txt file

    hi

    can someone tell me how to read data out of a *.txt file?

    my file 'names.txt' contains the following data:

    Code:
    ACHERON
    APHRODITE
    APOLLON
    ARES
    ARTEMIS
    ATHENE
    BAKCHOS
    CHARITEN
    CHARON
    DEMETER
    DIONYSOS
    EOS
    ERINNYEN
    EROS
    HADES
    HELIOS
    HEPHAISTOS
    HERA
    HERMES
    HOREN
    IRIS
    KERBEROS
    PAN
    PERSEPHONE
    POSEIDON
    SATYRN
    STYX
    TETHIS
    ZEUS
    i know how to read the whole data out of the file and print it out on the screen. but i am not sure how to read one single line with a randomizer function. *sigh* can you help me?

  2. #2
    Barjor
    Guest
    1. Find how many lines you have in the file.
    2. Get a random number < them # of lines in file.
    3. Read the file one char at a time and look for \n.
    4. When # of \n = random #. read in every char and voila you have your word.

    ~Barjor

  3. #3
    Barjor
    Guest
    Please forgive my horrible misstake.

    1. Find how many lines you have in the file.
    2. Get a random number < then # of lines in file.
    3. Read the file one char at a time and look for \n.
    4. When # of \n == random #. read in every char and voila you have your word.

    ~Barjor

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    When you use fgets, it moves along with the file pointer and gets each successive line without intervention.

    Use an array of pointers. Each pointer of course points to the first element of the next string and so on. Use malloc/free in functions that allocate and deallocate resources from within.

    Another interesting thought is to read them into a linked list of structures.

    Sort the array/list in a "randomiser" function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Read data from file - C program ?
    By Pawan Sangal in forum C Programming
    Replies: 2
    Last Post: 08-22-2008, 04:28 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM