Thread: using fstream operator >>

  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256

    using fstream operator >>

    I have a file, which does open, that contains...
    Code:
    Include="C:\GCC"
    LibLink=mystds
    LibLink=comctl32
    LibLink=mystdspp
    LibLink=gdi32
    I would like to read the entire file contents into a string, but when I use the >> operator like so...
    Code:
    CFG >> Configuration; //CFG is the ifstream and Configuration is a C++ style string.
    ...and output the string Configuration to cout, I get this...
    Code:
    cout << "Configuration = (" << Configuration << ")" << endl;
    Code:
    Configuration = (0x22fe00)
    Am I misusing the operator?
    Code:
    void function(void)
     {
      function();
     }

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Yup. The >> operator reads tokens. Use getline() (in <string>) to get full lines. Not sure what to do if you want the entire file in a single string, in any case, you are probably better off processing line by line.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    even so, you should still be getting the entire first line... I suspect there's something else wrong with your code, but you're going to have to post it for us to see...
    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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Post at least the declaration of Configuration (maybe you accidentally made it a pointer or array).

  5. #5
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Alright. That's simple enough. Do ifstreams have a length member, or should I use some tells and seeks?

    Oh, and here's my code.
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char *argv[])
     {
      if(argc == 1)
        return EXIT_SUCCESS;
      else if(argc == 2)
       {
        string GCC = "C:\\GCC\\bin\\gcc ";
        string GPP = "C:\\GCC\\bin\\g++ ";
        string Includes = "-I\"C:\\GCC\\include\" ";
        string CPPIncludes = "-I\"C:\\GCC\\include\\c++\\3.4.2\" -I\"C:\\GCC\\include\\c++\\3.4.2\\mingw32\" -I\"C:\\GCC\\include\\c++\\3.4.2\\backward\" -I\"C:\\GCC\\lib\\gcc\\mingw32\\3.4.2\\include\" ";
        string Libraries = "-L\"C:\\GCC\\lib\" ";
        string CustomIncludes;
        string CustomLibraries;
        string CustomLibLinks;
        string inFile = argv[1];
        string inName = "\"" + inFile + "\" ";
        string inType;
        string outName = "\"";
        string arguments;
    
        ifstream CFG("rungcc.cfg");
        if(!CFG.is_open())
          cout << "Failed to open rungcc.cfg." << endl;
        stringstream CFGString;
        string ConfigLine;
        CFGString << CFG;
        CFGString >> Configuration;
        cout << "CFG = (" << CFG << ")" << endl;
        cout << "CFGString = (" << CFGString << ")" << endl;
        cout << "Configuration = (" << Configuration << ")" << endl;
        CFG.close();
        for(int index = Configuration.find("Include=", 0); index != string::npos; index = Configuration.find("Include=", index))
         {
          index += 7;
          CustomIncludes += "-I\"";
          for(; Configuration[index] != '\n'; ++index)
            CustomIncludes += Configuration[index];
          CustomIncludes += "\" ";
         }
        for(int index = Configuration.find("Library=", 0); index != string::npos; index = Configuration.find("Include=", index))
         {
          index += 7;
          CustomLibraries += "-L\"";
          for(; Configuration[index] != '\n'; ++index)
            CustomLibraries += Configuration[index];
          CustomLibraries += "\" ";
         }
        for(int index = Configuration.find("LibLink=", 0); index != string::npos; index = Configuration.find("Include=", index))
         {
          index += 4;
          CustomLibLinks += "-l\"";
          for(; Configuration[index] != '\n'; ++index)
            CustomLibLinks += Configuration[index];
          CustomLibLinks += "\" ";
         }
    
        int NameLength = strlen(inFile.c_str());
        int index;
        for(index = NameLength; index >= 0; --index)
          if(inFile[index] == '\\')
            break;
        for(++index; index < NameLength; ++index)
          if(inFile[index] != '.')
            outName += inFile[index];
          else
            break;
        outName += ".exe\" ";
    
        for(++index; index < NameLength; ++index)
          inType += inFile[index];
    
        if(inType == "c")
          arguments = GCC + Includes + Libraries + inName + "-o " + outName;
        else if(inType == "cpp" || inType == "cc" || inType == "cp" || inType == "cxx" || inType == "c++")
          arguments = GPP + Includes + CPPIncludes + Libraries + inName + "-o " + outName;
    
        cout << "CustomIncludes = (" << CustomIncludes << ")" << endl;
        cout << "CustomLibraries = (" << CustomLibraries << ")" << endl;
        cout << "CustomLibLinks = (" << CustomLibLinks << ")" << endl;
    
    //    system(arguments.c_str());
       }
      return EXIT_SUCCESS;
     }
    Code:
    void function(void)
     {
      function();
     }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >>
    Code:
    CFGString << CFG
    You can't just read a filestream into a stringstream like that, can you?

    >>
    Code:
    CFGString >> Configuration;
    Am I missing something? Where so you declare Configuration. Does this even compile?

  7. #7
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Oh, my mistake. I was in the middle of changing Configuration to ConfigLine. And ignore all the CFGString stuff. That was just desperation on my part. Use this...
    Code:
    ifstream CFG("rungcc.cfg");
    if(!CFG.is_open())
      cout << "Failed to open rungcc.cfg." << endl;
    string Configuration;
    CFG >> Configuration;
    That's what I had.
    Code:
    void function(void)
     {
      function();
     }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In order to find your problem, see what this outputs:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        ifstream CFG("rungcc.cfg");
        if(!CFG.is_open())
        {
            cout << "Failed to open rungcc.cfg." << endl;
            return EXIT_FAILURE;
        }
    
        string Configuration;
        CFG >> Configuration;
        cout << "Configuration = (" << Configuration << ")" << endl;
    }

  9. #9
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Hmm.....Well, that gives the first line of the file. But when I copy that into the rest of the code, I get nothing, literally. The output is Configuration = (). I'm including all those headers, using namespace std, everything.

    Bah, I found it. I just needed to specify the full file name. rungcc.cfg was in the the same folder as the program, but not the folder I was using the command line in. Thanks for helping.
    Code:
    void function(void)
     {
      function();
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM