Thread: Reading in fields of text from a .txt file.

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    16

    Reading in fields of text from a .txt file.

    Hello,

    I am trying to read fields seperated by colons and spaces into variables.

    For example:
    Genesis 1:2: And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.

    I want "Genesis" in one variable, "1" in another, "2" in another variable and then the rest into yet another variable.

    How would I go about doing this?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If everything is on one line you could use getline() with the optional third parameter (the colon ':') to extract this information into std::strings. Of course you wouldn't need the third parameter for the final extraction.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Certain verses throughout the file are on two seperate lines.
    Like so:
    Genesis 1:3:
    And God said, Let there be light: and there was light.

    How would I deal with this?

    This file contains the entire Bible. I trying to read it and then format it.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What separates verses?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I suggest you show your current code, along with a small sample of your input file.

    Are there any "embedded" colons in the text portion of your input?

    Jim

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    here is a sample of the file:
    Code:
    Genesis 1:1: In the beginning God created the heaven and the earth.
    Genesis  1:2: And the earth was without form, and void; and darkness was upon  the face of the deep. And the Spirit of God moved upon the face of the  waters.
    Genesis 1:3: 
    And God said, Let there be light: and there was light.
    Genesis 1:4: And God saw the light, that it was good: and God divided the light from the darkness.
    Genesis 1:5: And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day.
    Genesis 1:6: 
    And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters.
    Genesis  1:7: And God made the firmament, and divided the waters which were  under the firmament from the waters which were above the firmament: and  it was so.
    Genesis 1:8: And God called the firmament Heaven. And the evening and the morning were the second day.
    Genesis 1:9: 
    And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so.
    Genesis  1:10: And God called the dry land Earth; and the gathering together of  the waters called he Seas: and God saw that it was good.
    code wise I am working on verse object. I will make an array of verses based. Each verse will have a book, chapter, and verse number. I will then do something similar will another file, although It is formatted a little differently so I will have to deal with that in a different manner. The end goal is to read in the German Luther 1545 and King James Version and then display them in parralel for printing.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm... you may need to check each line to see if it begins with the name of a book, and if so, then you treat it as the start of a new verse.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Ok! I have an array with all the book names and I could scan through that and compare it to the first word.

    So then how would I get the book, chapter, verse number and verse into variables?

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you need to parse the line - you could use istream to retrieve the numerical / character values.

    But i am not clear on your end goal - you say it is for printing - what exactly do you mean by that? storing lookups of chapter verse etc could mean there is an interface, so what kind of interaction does a user have with the program? This will inform what you need to do with the data- in order to make it accessible
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    For now I will be the only one intefacing with it. A friend of mine is a missionary to Germany and he wants a KJV/German Luther 1545 parralel Bible. So I'm trying to make one. I will be reading in the two texts and then format em to his liking and then make a .pdf file out of it.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    I figured the book names, chapters and all that should be stored so I can have special formatting at the beginning of each chapter and book. Also, having storing the ferences for each verse will allow the program to track each English verse and its equivalent German verse.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    So could read in a line and store it in a temporary string variable I could then check to see if it begins with a book name and if it does, stick it in the first index of a dynamic string array. Each time it doesn't begin with a book name, I just concatenate it to the string in the last index of the dynamic array.

    How would I then detemine the chapter and verse of each verse in the array?

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by goldfish
    How would I then detemine the chapter and verse of each verse in the array?
    Parse the input some more. If the verses end up looking something like this:
    Code:
    lines[0] = "Genesis 1:1: In the beginning God created the heaven and the earth.";
    Then how about:
    Code:
    #include <string>
    #include <sstream> // for the string stream parser
    
    struct BibleRef
    {
        string bookName;
        int bookChapterRef;
        int bookVerseRef;
    };
    
    struct BibleVerse
    {
        BibleRef loc;
        string verseText;
    };
    
    BibleVerse thisVerse;
    istringstream parser(lines[0]);
    parser >> thisVerse.loc.bookName >> thisVerse.loc.bookChapterRef >>  match_token(":") >> thisVerse.loc.bookVerseRef >> match_token(":");
    getline(parser, thisVerse.verseText);
    So much of this is fluff, but you can make classes if you want or not. I don't expect you to use this as is. I just wanted all the variables I was going to use to stay orderly. Thank you laserlight for writing match_token as well.

    Hope this helped.

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Ok, great!! Thanks for the help!!! I will give this a shot later today or tomorrow. I will probably start a new thread if I have any further issues that I cannot solve myself.

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Actaully, I do have one more question before I start cranking out the code. How do I deal with books whose names consist of multiple words(eg. "Song of Solomon" or "I Kings")?

    Here are some examples in the text file:
    Code:
    I  Kings 4:34: And there came of all people to hear the wisdom of Solomon,  from all kings of the earth, which had heard of his wisdom.
    Code:
    Song  of Solomon 1:4: Draw me, we will run after thee: the king hath brought  me into his chambers: we will be glad and rejoice in thee, we will  remember thy love more than wine: the upright love thee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  2. Replies: 9
    Last Post: 12-08-2008, 10:27 AM
  3. Reading from text file
    By wiz23 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2005, 04:30 AM
  4. Cleaning all Text fields
    By publikum in forum C++ Programming
    Replies: 3
    Last Post: 01-15-2005, 12:46 AM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM