getline

Can you specify what ISN'T a delimiter in std::getline?

混江龙づ霸主 提交于 2019-12-01 07:21:14
问题 I want it to consider anything that isn't an alphabet character to be a delimiter. How can I do this? 回答1: You can't. The default delimiter is \n : while (std::getline (std::cin, str) // '\n' is implicit For other delimiters, pass them: while (std::getline (std::cin, str, ' ') // splits at a single whitespace However, the delimiter is of type char, thus you can only use one "split-character", but not what not to match. If your input already happens to be inside a container like std::string ,

Ignore Spaces Using getline in C++ [duplicate]

百般思念 提交于 2019-12-01 00:52:44
This question already has an answer here: Need help with getline() 7 answers Hey, I'm trying to write a program that will accept new tasks from people, add it to a stack, be able to display the task, be able to save that stack to a text file, and then read the text file. The issue comes when I am trying to accept input from the user, whenever you enter a string with a space in it, the menu to choose what to do just loops. I need a way to fix this. Any help would be greatly appreciated. // basic file io operations #include <iostream> #include <fstream> #include <stack> #include <string> using

Use getline() without setting failbit

China☆狼群 提交于 2019-12-01 00:29:04
问题 Is it possible use getline() to read a valid file without setting failbit ? I would like to use failbit so that an exception is generated if the input file is not readable. The following code always outputs basic_ios::clear as the last line - even if a valid input is specified. test.cc: #include <iostream> #include <string> #include <fstream> using namespace std; int main(int argc, char* argv[]) { ifstream inf; string line; inf.exceptions(ifstream::failbit); try { inf.open(argv[1]); while

std::getline throwing when it hits eof

会有一股神秘感。 提交于 2019-11-30 20:30:19
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 code getline is throwing exception as it gets eof How to handle this situation ? EDIT std::string buffer =

gawk / awk: piping date to getline *sometimes* won't work

无人久伴 提交于 2019-11-30 20:22:44
I'm attempting to convert dates from one format to another: From e.g. "October 29, 2005" to 2005-10-29. I have a list of 625 dates. I use Awk. The conversion works -- most of the time. Hovewer, sometimes the conversion won't happen at all, and the variable supposed to hold the (converted) date remains undefined. This always happens with the exact same rows. Running `date' explicitly (from the Bash shell) on the dates of those weird rows works fine (the dates are properly converted). -- It's not the textual contents of those rows that matters. Why this behavior, and how can I fix my script? Her

AWK - Transmission of a variable with getline to system ()?

元气小坏坏 提交于 2019-11-30 20:15:59
I have a theoretical question: 1) How pass a variable to the system of getline ()? awk 'BEGIN{var="ls"; var | getline var; system("echo $var")}' 2) How to assign a variable the output system ("ls") and print the result in awk? awk 'BEGIN{var="system("ls")"; print '$var'}' 3) Can you assign a variable in the system (var = "ls") and print the result in awk? awk 'BEGIN{system(var="ls"); print "'"$var"'"}' Thank you for the information. EDIT: torek : Thank you for your response. I understand that in the first example, you can do this: awk 'BEGIN { while ("ls -l" | getline var) system("echo " var )

Ignore Spaces Using getline in C++ [duplicate]

折月煮酒 提交于 2019-11-30 19:03:05
问题 This question already has answers here : Need help with getline() [duplicate] (7 answers) Closed 4 years ago . Hey, I'm trying to write a program that will accept new tasks from people, add it to a stack, be able to display the task, be able to save that stack to a text file, and then read the text file. The issue comes when I am trying to accept input from the user, whenever you enter a string with a space in it, the menu to choose what to do just loops. I need a way to fix this. Any help

std::getline() reads wrong data after reading formatted input from stream

坚强是说给别人听的谎言 提交于 2019-11-30 18:11:28
问题 I looked at some other questions and I'm too newbish at C++ to know if they applied to my question here.. Basically when show the output of "name", if I type in my full name it only shows the second word. Before, it wasn't even taking anything at all, it just skipped it. I'm confused at the moment for something so seemingly simple. THanks. #include <iostream> #include <string> using namespace std; int main() { double money; string name; int age; // Prompt for age and receive cout<<"How old

Getline keeps on getting newline character. How can I avoid this?

时光怂恿深爱的人放手 提交于 2019-11-30 14:28:36
Basically I first takes an integer as input and then test case follows. My each test case is an string. I am suppose to print the string back if the starting patten of string matches "HI A" and it is case-insensitive. I wrote the code below to accomplish to this. My problem is that when I press enter after each input, getline takes newline character as new input. I have tried to tackle this by using extra getline after each input but the issue is still there. Program gets stuck in the loop even though I have put a break condition. What am I doing wrong? #include <iostream> #include <string>

very fast text file processing (C++)

我是研究僧i 提交于 2019-11-30 12:57:21
问题 i wrote an application which processes data on the GPU. Code works well, but i have the problem that the reading part of the input file (~3GB, text) is the bottleneck of my application. (The read from the HDD is fast, but the processing line by line is slow). I read a line with getline() and copy line 1 to a vector, line2 to a vector and skip lines 3 and 4. And so on for the rest of the 11 mio lines. I tried several approaches to get the file at the best time possible: Fastest method I found