getline

std::getline throwing when it hits eof

雨燕双飞 提交于 2020-01-21 04:48:45
问题 std::getline throws exception when it gets an eof . this is how I am doing. std::ifstream stream; stream.exceptions(std::ifstream::failbit|std::ifstream::badbit); try{ stream.open(_file.c_str(), std::ios_base::in); }catch(std::ifstream::failure e){ std::cout << "Failed to open file " << _file.c_str() << " for reading" << std::endl; } while(!stream.eof()){ std::string buffer = ""; std::getline(stream, buffer); //process buffer //I do also need to maintain state while parsing } In the above

Program is skipping over Getline() without taking user input [duplicate]

情到浓时终转凉″ 提交于 2020-01-20 07:04:12
问题 This question already has answers here : Why does std::getline() skip input after a formatted extraction? (3 answers) Closed 5 years ago . This is a very strange problem, when my program asks the user for the address, instead of waiting for input, it seems to skip the getline() function completely Answerinput: cout << "would you like to add another entry to the archive? (Y/N):"; cin >> answer; cout << endl; cout << endl; answer = toupper(answer); switch(answer) { case 'Y': Entrynumber++; cout

Program is skipping over Getline() without taking user input [duplicate]

六眼飞鱼酱① 提交于 2020-01-20 07:01:10
问题 This question already has answers here : Why does std::getline() skip input after a formatted extraction? (3 answers) Closed 5 years ago . This is a very strange problem, when my program asks the user for the address, instead of waiting for input, it seems to skip the getline() function completely Answerinput: cout << "would you like to add another entry to the archive? (Y/N):"; cin >> answer; cout << endl; cout << endl; answer = toupper(answer); switch(answer) { case 'Y': Entrynumber++; cout

getline函数

▼魔方 西西 提交于 2020-01-16 02:14:57
在我的印象中,getline函数常常出如今自己的视野里,模糊地记得它常常常使用来读取字符串 。可是又对它的參数不是非常了解,今天又用到了getline函数,如今来细细地总结一下: 首先要明确设计getline函数的目的,事实上非常easy,就是从流中读取字符串。并且读取的方 式有非常多,包含依据限定符,依据已读取的字符的个数。从这个函数的名称来看,它的直观 意义是从流中读取一行,可是大家不要被这表面的现象所迷惑。事实上假设让我来为这个函数 去一个名字的话,也许我会取一个getString,由于它的目的本来就是从流中读取字符的序 列,而不是像get函数那样一次读取一个字符。 另外要注意,C++中有两个getline函数,一个是在string头文件里,定义的是一个全局的 函数,函数声明是istream& getline ( istream& is, string& str, char delim )与 istream& getline ( istream& is, string& str );还有一个则是istream的成员函数,函 数声明是istream& getline (char* s, streamsize n )与istream& getline (char* s, streamsize n, char delim )

getline()函数

怎甘沉沦 提交于 2020-01-16 02:14:03
在我的印象中,getline函数经常出现在自己的视野里,模糊地 记得它经常用来读取字符串 。但是又对它的参数不是很了解,今天又用到了getline函数, 现在来细细地总结一下: 首先要明白设计getline函数的目的,其实很简单,就是从流中 读取字符串。而且读取的方 式有很多,包括根据限定符,根据已读取的字符的个数。从这 个函数的名称来看,它的直观 意义是从流中读取一行,但是大家不要被这表面的现象所迷惑 。其实如果让我来为这个函数 去一个名字的话,或许我会取一个getString,因为它的目的本 来就是从流中读取字符的序 列,而不是像get函数那样一次读取一个字符。 另外要注意,C++中有两个getline函数,一个是在string头文 件中,定义的是一个全局的 函数,函数声明是istream& getline ( istream& is, string& str, char delim )与 istream& getline ( istream& is, string& str );另一个则 是istream的成员函数,函 数声明是istream& getline (char* s, streamsize n )与 istream& getline (char* s, streamsize n, char delim );注意第二个getline是将读取

C++ 从键盘、文件读入

会有一股神秘感。 提交于 2020-01-13 14:45:12
Background Info EOF是C语言中为了区分有效数据和输入结束符的。 EOF的输入由系统锁定,windows下是 ctrl+z ,linux/unix下是 ctrl+d . 在 C++ 中,输入输出流被定义为类。C++ 的I/O库中的类称为流类(stream class)。 用流类定义的对象称为流对象。   输入和输出是数据传送的过程,数据如流水一样从一处流向另一处。C++ 形象地将此过程称为流(Stream)。C++的输入输出流是指由若干字节组成的宇节序列,这些宇节中的数据按顺序从一个对象传送到另一对象。流表示了信息从源到目的端的流动。在输入操作时,字节流从输入设备(如键盘、磁盘)流向内存,在输出操作时,字节流从内存流向输出设备(如屏幕、打印机、磁盘等)。流中的内容可以是ASCII字符、二进制形式的数据、图形图像、数字音频视频或其他形式的信息。   实际上,在内存中为每一个数据流开辟一个内存缓冲区,用来存放流中的数据。当用cout和插入运算符“<<”向显示器输出数据时,先将这些数据送到程序中的输出缓冲区保存,直到缓冲区满了或遇到endl,就将缓冲区中的全部数据送到显示器显示出来。在输入时,从键盘输入的数据先放在键盘的缓冲区中,当按回车键时,键盘缓冲区中的数据输入到程序中的输入缓冲区,形成cin流,然后用提取运算符“ >>”从输入缓冲区中提取数据送给程序中的有关变量

getline(cin, name) 逐行复制函数

前提是你 提交于 2020-01-09 16:38:30
1.直接使用cin的>>操作符从键盘输入字符串时,空格会被当做输入的分隔符。例如,输入字符串“123 ABC"时,那么被读入的字符串就是“123”,而“ABC”将作为下一波被读入。 所以若你想不以中间的空格作为输入的分隔符,可以使用头文件string中定义的getline,并且输入字符串时只以换行符作为分隔符。 2.getline还允许在输入字符串时增加其他分隔符,使用方法是吧可以作为分隔符的字符作为第三个参数传递 给getline。如getline(cin,s2,','); 即把“,”也作为分隔标志,“,”以后的部分作为下一个输出流。 例子: 1 #include<iostream> 2 #include<string> 3 #include<stdlib.h> 4 using namespace std; 5 6 int main() 7 { 8 for(int i=0;i<2;i++) 9 { 10 string city,state; 11 getline(cin,city,','); 12 getline(cin,state); 13 cout<<"City:"<<city<<" State:"<<state<<endl; 14 } 15 system("pause"); 16 return 0; 17 } 输出的结果: 输入流:Beijing,China 输出:

Getting user input in C++ [closed]

 ̄綄美尐妖づ 提交于 2020-01-09 10:33:53
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . I am writing a program that allows a student to write a question and store that Question (or string) in a variable, can anyone please

Getting user input in C++ [closed]

心已入冬 提交于 2020-01-09 10:33:25
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . I am writing a program that allows a student to write a question and store that Question (or string) in a variable, can anyone please

std::getline input not working properly in C++ [duplicate]

狂风中的少年 提交于 2020-01-05 04:38:10
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Need help with getline() getline not asking for input? I am working on the following code: int main() { int num; string str; cin>>num; int points[num][2]; for(int i=0;i<num;i++) { cout<<"\nPoint"<<i<<":"; getline (cin,str); points[i][0]=atoi(&str[0]); points[i][1]=atoi(&str[2]); } for(int i=0;i<num;i++) { cout<<"\npoint"<<i<<" = "<<points[i][0]<<" "<<points[i][1]; } The problem with the above code that I am