-
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!!
-
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;
-
Thank you manasij, you are always SO helpful!! :)
-
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();
}
-
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.
-
Once again you have solved it! Gosh I wish I could beam your knowledge straight to my brain :D