istream

C++: ifstream::getline problem

会有一股神秘感。 提交于 2021-02-19 05:25:53
问题 I am reading a file like this: char string[256]; std::ifstream file( "file.txt" ); // open the level file. if ( ! file ) // check if the file loaded fine. { // error } while ( file.getline( string, 256, ' ' ) ) { // handle input } Just for testing purposes, my file is just one line, with a space at the end: 12345 My code first reads the 12345 successfully. But then instead of the loop ending, it reads another string, which seems to be a return/newline. I have saved my file both in gedit and

C++: ifstream::getline problem

*爱你&永不变心* 提交于 2021-02-19 05:24:57
问题 I am reading a file like this: char string[256]; std::ifstream file( "file.txt" ); // open the level file. if ( ! file ) // check if the file loaded fine. { // error } while ( file.getline( string, 256, ' ' ) ) { // handle input } Just for testing purposes, my file is just one line, with a space at the end: 12345 My code first reads the 12345 successfully. But then instead of the loop ending, it reads another string, which seems to be a return/newline. I have saved my file both in gedit and

How does cin object converts characters to different types as user needes?

孤者浪人 提交于 2021-02-05 12:27:06
问题 How does std::cin object deal with different types while it is an instance of basic_istream<char> ( istream )? 回答1: The class std::basic_istream<CharT, Traits> models an input stream of characters of type CharT . It provides both relatively low-level and relatively high-level access to that input stream. You can, for example, call std::cin.get() in order to retrieve the next character from the input stream; this will always return CharT , since that's the underlying type of characters in the

How does cin object converts characters to different types as user needes?

淺唱寂寞╮ 提交于 2021-02-05 12:25:15
问题 How does std::cin object deal with different types while it is an instance of basic_istream<char> ( istream )? 回答1: The class std::basic_istream<CharT, Traits> models an input stream of characters of type CharT . It provides both relatively low-level and relatively high-level access to that input stream. You can, for example, call std::cin.get() in order to retrieve the next character from the input stream; this will always return CharT , since that's the underlying type of characters in the

C++ read string with spaces

我的梦境 提交于 2021-02-05 06:35:07
问题 I have file like this: 59 137 New York 137 362 Syracuse 216 131 New Jersey ... .. . and I would like to read it to a structure: X - Y - name of a city char city[100]; int x , y; f.open("map.txt"); f >> x >> y >> city; while (!f.fail()) { f >> x >> y >> city; } f.close(); Problem is, that city reads only until next space, so from New York it reads only New. How should I read whole rest of a line, in some easy and smart way ? 回答1: The format of your file seems to imply that the name of the city

How do I read and parse input from a user that is comma separated by receiving an std::istream object in c++?

天涯浪子 提交于 2021-01-29 10:11:16
问题 I have a class in c++ called Airplane. I need to create a read function using std::istream that lets a user type after a prompt in the console a line that is comma separated. This line of input will then be split up using the commas and assigned to different private data members of the class. As an example, if the user types into the console "abc,12345,hello," then I would need to parse that line and assign abc to one variable, 12345 to another and hello to the last. I believe after the user

C++ Detect if input doesn't satisfy conditions

孤街浪徒 提交于 2021-01-28 08:31:25
问题 Update: Please guid me through a clear answer and small example of usage I am reading data from a binary file in which the first number indicates the number of inputs or values to read, the second tells the length of the first input then the input itself, after that the length of the second input then the input itself and so on. So wrote the following code: std::ifstream infile(filename, std::ios_base::binary); unsigned int NumberOfInputs; infile.read((char *) &NumberOfInputs, sizeof(unsigned

C++ istream: Is gcount() always set after a read() even if it fails?

空扰寡人 提交于 2021-01-28 01:32:03
问题 I am reading some data using an istream and read(). I would like to know if I can just test gcount() for the bytes or if I need to test some combination of good(), eof(), etc before calling gcount(). In other words, is gcount() always set after a read() even if that read failed due to EOF or some other internal problem? Also if this is described in the standard or somewhere that you can cite. I'm using cplusplus.com as a reference and it says that gcount "Returns the number of characters

Istream function to read with istream parameter

本小妞迷上赌 提交于 2020-07-07 06:58:17
问题 I'm trying to understand this code istream &read(istream &is, Sales_data &item) { double price = 0; is >> item.bookNo >> item.units_sold >> price; item.revenue = price * item.units_sold; return is; } ostream &print(ostream &os, const Sales_data &item) { os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os; } I don't understand what are these function , also I can't understand why we use istream for reading and ostream for printing instead of

Istream function to read with istream parameter

ⅰ亾dé卋堺 提交于 2020-07-07 06:58:10
问题 I'm trying to understand this code istream &read(istream &is, Sales_data &item) { double price = 0; is >> item.bookNo >> item.units_sold >> price; item.revenue = price * item.units_sold; return is; } ostream &print(ostream &os, const Sales_data &item) { os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os; } I don't understand what are these function , also I can't understand why we use istream for reading and ostream for printing instead of