Thread: ifstream trouble

  1. #1
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19

    ifstream trouble

    How do I properly take all of the text in a file and display all of the contents instead of just one word (including spaces)?

    Well, I can get it to output but I cannot get it to include spaces and it cuts off the last letter of each word.

    Ex: message in file:
    This is my message

    Ex: pulling string to cout this is what I get:
    Thiimmessag

    Example of what I have:
    Code:
    int main()
    {
      string filename;
      string str;
      cout<<"Enter filename:";
      getline (cin, filename);
      ifstream a_file (filename.c_str());
      getline( a_file, str );
      cout<< str;
      cin.get();
    }
    Thank you!!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    There is a 'cheating' way.
    Code:
    cout<<a_file.rdbuf();
    Otherwise, just run a loop getting and printing each line.
    Code:
    while(getline(a_file,the_string))
        cout<<the_string<<endl;

  3. #3
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19
    Thank you manasij, you are always SO helpful!!

  4. #4
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19
    Hmm I found that the problem is diferent than what I had previously believed...I am using that char table we talked about a few days ago and it seems that when it takes in a white space it converts it to a strange character that will display in a text editor properly, but when displayed using cout it will show something like This?is?my?message.

    So the problem is not in displaying it, it is at the root of the text generator..So with that being said, any suggestions on how to run the string through the char table while still holding onto the white spaces as just a blank space?

    This is basically how its running now with char table algorithm changed and unnecessary text omitted:
    Code:
    void rep(char& c)
    {
        bool up_flag(c>='A'&&c<='Z');
        if(up_flag)c=tolower(c);
        static char table[]=
        {
            'a','b','c','d','e',
            'f','g','h','i','j',
            'k','l','m','n','o',
            'p','q','r','s','t',
            'u','v','w','x','y',
            'z'
        };
        c = table[c-'a'];
        if(up_flag)c=toupper(c);
    }
    
    int main()
    {
      string filename;
      string message;
         
        cout<<"Filename: ";
        getline (cin, filename);
        cout<<"*MESSAGE*\n";
        getline (cin, message);
        for_each(message.begin(),message.end(),rep);
        ofstream a_msg(filename.c_str());
        a_msg<<message;
        a_msg.close();
    }
    Last edited by MoonMan; 02-27-2012 at 07:28 AM.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    That table I wrote assumes only alphabets are given.
    If you want it to process everything, you've to put in the whole ascii table.

    If it is just for a space, add a clause at the beginning of the rep function.
    Code:
    if(c==' ')return;
    Last edited by manasij7479; 02-27-2012 at 07:34 AM.

  6. #6
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19
    Once again you have solved it! Gosh I wish I could beam your knowledge straight to my brain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble storing ifstream object in pair object
    By Programmer_P in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2012, 01:20 AM
  2. What is ifstream& for?
    By chickenlittle in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2011, 12:45 AM
  3. ios::in with ifstream? Why?
    By siavoshkc in forum C++ Programming
    Replies: 4
    Last Post: 01-30-2006, 10:26 AM
  4. ifstream
    By Klinerr1 in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2002, 11:18 PM
  5. having trouble with ifstream
    By Shy_girl_311 in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2001, 03:42 AM