Parsing text files.

This is a discussion on Parsing text files. within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use an if statement with a bunch of different phrases that are accessible outside of the code, ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Parsing text files.

    I'm trying to use an if statement with a bunch of different phrases that are accessible outside of the code, and I determined the best way to do this at my skill level would be to parse a text file.

    E.G. I would make a list of names and then any time someone input one of those names into the field I would create, they would get one message, and if the name wasn't in the file, they would get another.
    If someone could help me figure out how to do this it would be much appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    Doing my best to understand what you mean . . .

    In other words, you are creating a list of names, let's say, "names.txt", that contains something much like:
    Code:
    Allen
    Brian
    Crystal
    David
    Ellen
    If someone enters the name "Brian", then your program will print "The name 'Brian' is in the list of names." -- whereas, if they enter "Varun", then the program will print "The name 'Varun' is not in the list of names."?

    If so, then you will need to use ifstream. One example of a program that reads in words, then prints them out to the screen on their own lines could be:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    
    int main(int argc, char *argv[]) {
        if(argc != 2) {
            std::cout << "usage: " << argv[0] << " filename" << std::endl;
            exit(1); /* NOTE: I'm not sure, in C++, if exit() is actually std::exit() or not. Probably. */
        }
        std::ifstream file(argv[1]);
        if(!file.is_open()) exit(1);
        bool done = false;
        do {
            std::string word;
            file >> word;
            std::cout << word << std::endl;
            if(file.eof()) done = true;
        } while(!done);
        file.close();
        return 0;
    }
    In C++, it is possible to compare strings against each other (string1 == string2), since operator==(const std::string &) is implemented by default.

    That should be enough to get you started, at least.
    Last edited by strange; 12-10-2009 at 12:13 AM.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by strange View Post
    Doing my best to understand what you mean . . .

    In other words, you are creating a list of names, let's say, "names.txt", that contains something much like:
    Code:
    Allen
    Brian
    Crystal
    David
    Ellen
    If someone enters the name "Brian", then your program will print "The name 'Brian' is in the list of names." -- whereas, if they enter "Varun", then the program will print "The name 'Varun' is not in the list of names."?

    If so, then you will need to use ifstream. One example of a program that reads in words, then prints them out to the screen on their own lines could be:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    
    int main(int argc, char *argv[]) {
        if(argc != 2) {
            std::cout << "usage: " << argv[0] << " filename" << std::endl;
            exit(1); /* NOTE: I'm not sure, in C++, if exit() is actually std::exit() or not. Probably. */
        }
        std::ifstream file(argv[1]);
        if(!file.is_open()) exit(1);
        bool done = false;
        do {
            std::string word;
            file >> word;
            std::cout << word << std::endl;
            if(file.eof()) done = true;
        } while(!done);
        file.close();
        return 0;
    }
    In C++, it is possible to compare strings against each other (string1 == string2), since operator==(const std::string &) is implemented by default.

    That should be enough to get you started, at least.
    Why are you using exit(1)? If I'm not incorrect, it simply terminates right then and there, and doesn't clean up the stack or anything. This means your file object is leaking memory in case of failure.

    If it doesn't affect stack objects in the nearest scope, then it definitely affects everything else.
    On top of that, it's pointless. You can just throw a return in there, and the stack frame gets cleaned up properly.

    I know it might be a big issue in this particular program, but seeing as you used it two times in this very small program, where you should/could have used return, it looks like you have a bad habit that you shouldn't be teaching others. No offense.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by IceDane View Post
    Why are you using exit(1)? If I'm not incorrect, it simply terminates right then and there, and doesn't clean up the stack or anything. This means your file object is leaking memory in case of failure.

    If it doesn't affect stack objects in the nearest scope, then it definitely affects everything else.
    On top of that, it's pointless. You can just throw a return in there, and the stack frame gets cleaned up properly.

    I know it might be a big issue in this particular program, but seeing as you used it two times in this very small program, where you should/could have used return, it looks like you have a bad habit that you shouldn't be teaching others. No offense.
    exit - C++ Reference

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by valaris View Post
    Oops. My bad. I guess I should have checked that out before going with something someone had told me.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    2
    Thanks a lot, strange. That's exactly what I was looking for. You were a big help.
    Is there a name for this?
    Last edited by mwesclark; 12-10-2009 at 08:52 PM.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    Quote Originally Posted by IceDane View Post
    Why are you using exit(1)?

    . . .

    I know it might be a big issue in this particular program, but seeing as you used it two times in this very small program, where you should/could have used return, it looks like you have a bad habit that you shouldn't be teaching others. No offense.
    You are correct, it is a very bad habit that I have. And you're right, I shouldn't be teaching others my bad habits.

    I realized after I posted it that I could have just used return, but I was tired at the time and didn't feel like editing it. (Excuses, excuses . . .)

    mwesclark: I would personally call it an argument, but I don't think that's what you were referencing . . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 12:21 PM
  2. Replies: 5
    Last Post: 02-11-2008, 01:36 AM
  3. Parsing Text File and gathering input variables
    By azamsharp1 in forum C Programming
    Replies: 2
    Last Post: 10-26-2005, 09:43 AM
  4. pointers into largish text files
    By donkeypunch in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2004, 04:04 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

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