I would like to obtain the first character from a text string and assign it to a variable. I tried to create an ifstream object to open a text file and using its get() function but was unsuccessful.
This is a discussion on how do I get a single character from a text string? within the C++ Programming forums, part of the General Programming Boards category; I would like to obtain the first character from a text string and assign it to a variable. I tried ...
I would like to obtain the first character from a text string and assign it to a variable. I tried to create an ifstream object to open a text file and using its get() function but was unsuccessful.
Show the code where you tried to open and read from the file.
Jim
Code:char letter ifstream reader("poem.txt"); if (!reader) { cout << "Error opening input file" << endl; return -1; } else reader.get(letter); cout << letter; reader.close();
Other than missing a semicolon, a main function and include files your snippet looks correct.
Jim