Thread: from file to struct.

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    from file to struct.

    I know how to open and display a text file, just wondering if you can use the text from a terxt file to work for a struct,

    eg.

    Txt file


    "blablabla" , 3

    or

    "blablabla" 3

    and to read the bla bla bla on the screen and compare it to 3?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I have no idea what you want to do. Do you want to put data from a text file into a struct? Do you want to compare what somebody types (I guess that might be what you mean by "read what's on the screen") to a struct, or a text file, or both?

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Sorry I will be a bit more specific.

    I have a question scruct which questions in it,

    Code:
    RiddleGenerator::QnA RiddleGenerator::mQnA[RiddleGenerator::QuestionCount] = {
            { "What shape as 4 sides?\n\t1)Triangle\n\t2)Circle\n\t3)Square", 3 },
            { "What is the capital of England?\n\t1)Paris\n\t2)London\n\t3)Washington", 2 }
    
            // NEW QUESTIONs CAN GO IN THE ABOVE SPACE <---!
    };
    I was wondering if I could put that info (Question, Answer) into a text document and load it in from there so I can just add questions on the fly without re compiling

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, yes, I suppose. You'll have to be competent about how you organize your struct/class -- for instance, you can't specify RiddleGenerator::QuestionCount, but rather the data will. There's no need to write it like it's C, because it's not C, it's a text file. Just read in the file and stuff it into a vector as you go.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    I can only find stuff about saving out variables but not the rest, i tried googling but cant find anything useful.

    Wondering if anyone knwos a book or a reference for this?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Aliaks View Post
    I can only find stuff about saving out variables but not the rest, i tried googling but cant find anything useful.

    Wondering if anyone knwos a book or a reference for this?
    There was a "rest" besides how to save data from a file into a variable? What was it?

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Not entire lines of words and then a number like with the above structure

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then I guess your magic word is "parsing", if you mean "I want to split my line that I just read in on the comma".

  9. #9
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Yes I would say so,

    I just wish to read the questiosn and answers from a text file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 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