ifstream trouble

This is a discussion on ifstream trouble within the C++ Programming forums, part of the General Programming Boards category; How do I properly take all of the text in a file and display all of the contents instead of ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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;
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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, 01: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-23-2002, 12:18 AM
  5. having trouble with ifstream
    By Shy_girl_311 in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2001, 03:42 AM

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