File content -> string?

This is a discussion on File content -> string? within the C++ Programming forums, part of the General Programming Boards category; Is there a way to get the contents of a file and place it into a string? So far I ...

  1. #1
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309

    File content -> string?

    Is there a way to get the contents of a file and place it into a string?
    So far I have the following:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int main()
    {
        ifstream file;
        string word;
        int count;
        map<string, int> table;
        file.open("main.cpp");
        if(!file)
        {
            return 1;
        }
        word = "#include";
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    I've tried setting word = file, doing a while(file>>word), and some other things (such as setting word = file.open(), file.count, and things that generally didn't work).
    Is there a way to set the contents from that file into the string for manipulation?
    To code is divine

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try somethign along these lines:
    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    
    int main()
    {
        std::string word;
        std::fstream file("Untitled1.dat",std::ios::in);
        getline(file,word,static_cast<char>(0x3));
        std::cout<<word;
        std::cin.get();
        return 0;
    }
    in case you're wondering, 0x3 is the hex value for ETX, or End of Text. I don't know how reliable it is, but it worked in the case I tried it in.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    THANK YOU!
    Now why can't google pop up with stuff like this? ><
    To code is divine

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    read the first hit, second block of example code: http://www.google.com/search?num=100...2B&btnG=Search

    Last edited by major_small; 03-30-2005 at 08:33 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Figures I would search for the wrong thing...

    Anyway, I hate to be a bother but is there any speacific reason why "no" always returns as 0?
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        string search = "#include";
        std::string word;
        vector<string> vs;
        //int no= 0;
        std::fstream file("main.cpp",std::ios::in);
        while(getline(file,word,static_cast<char>(0x3)))
        {
            vs.push_back(word);
        }
        int no = count(vs.begin(),vs.end(),search);
        cout<<word;
        cout<<no;
        std::cin.get();
        return EXIT_SUCCESS;
    }
    As far as I can tell I'm using the count() functions correctly, and there is obviously more than 0 #includes in the string...

    Bleh ><
    To code is divine

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    getline() reads in a line up to a newline, not just any white space, so vs[0] would be "#include <cstdlib>" instead of just "#include". I suggest using the >> operator rather than getline(). Alternatively, you could use a function search through the strings for "#include".
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #7
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    I'm sorry, joshdick, but I'm not sure what you are saying to do. Could you "dumb" it down a bit?


    EDIT: I managed to get it to work on a per line basis, but I can't find a way to search for a substring (so I can search for include instead of #include....)
    Last edited by 7smurfs; 03-30-2005 at 09:44 AM.
    To code is divine

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If you have code like
    getline(cin, name);
    and I enter "John Q. Public" and hit enter
    then name == "John Q. Public".
    But if instead you have code link
    cin >> name;
    and I enter "John Q. Public" and hit enter,
    only "John" will be stored in name.

    getline() keeps reading in until a newline (or EOF) is reached.
    cin >> name; only reads up to whitespace.

    If you'd prefer to store one word at a time without whitespace, use cin >>. If you want to store an entire line with spaces and tabs and such in it, then use getline(). I think for your purposes it'd be better to do the former.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    string a = "Hello, World!";
    cout << a.find("lo");
    This will output 3 because that is the index of the substring "lo" in a.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  10. #10
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Meh...works good enough (the number of includes was just something I wanted to add...), just wish I could get it to stop right before "using"...
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        string search = "using";
        std::string word;
        vector<string> vs;
        int no= 0;
        std::fstream file("main.cpp",std::ios::in);
        while(getline(file,word,'{'))
        {
            vs.push_back(word);
        }
        cout<<"Showing all includes, using namespaces, and arguments of main"<<'\n';
        cout<<vs[0]<<"\n";
        std::cin.get();
        return EXIT_SUCCESS;
    }
    To code is divine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

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