getline

How to read until ESC button is pressed from cin in C++

和自甴很熟 提交于 2019-11-29 08:03:06
I'm coding a program that reads data directly from user input and was wondering how could I read all data until ESC button on keyboard is pressed. I found only something like this: std::string line; while (std::getline(std::cin, line)) { std::cout << line << std::endl; } but need to add a portable way (Linux/Windows) to catch a ESC button pressed and then break a while loop. How to do this? EDIT: I wrote this, but still - works even if I press an ESC button on my keyboard: #include <iostream> #include <string> using namespace std; int main() { const int ESC=27; std::string line; bool moveOn =

Reading getline from cin into a stringstream (C++)

和自甴很熟 提交于 2019-11-29 07:16:09
So I'm trying to read input like this from the standard input (using cin ): Adam English 85 Charlie Math 76 Erica History 82 Richard Science 90 My goal is to eventually store each data piece in its own cell in a data structure I have created, so basically I want to parse the input so each piece of data is individual. Since each row of input is inputted by the user one at a time, each time I get an entire row of input that I need to parse. Currently I am trying something like this: stringstream ss; getline(cin, ss); string name; string course; string grade; ss >> name >> course >> grade; The

3、c++的getline与get

半世苍凉 提交于 2019-11-29 06:10:12
1、getline:面向行的输入 读取整行,通过回车键输入的换行符去欸的那个输入的结束。 可以使用cin.getline(name,20)。 第一个参数是用来存储输入行的数组的名称 getline在读取指定数目的字符或遇到换行符时停止读取。(注意与cin的区别) const int size = 20; char name1[size]; char name2[size]; cout << "enter your name:\n"; cin.getline(name1, size); cout << "enter your desert:\n"; cin.getline(name2, size); cout << "I have some delicious " << name2 << " for you," << name1<< endl; 2、get的用法 与getline的用法基本相同,但get不再丢弃换行符,而是将其留在输入队列中。 由于第一次调用后,换行符被留在输入队列,因此第二次调用时看到的第一个字符就是换行符,此时get认为已到达末尾,而没有发现任何可以读取的内容。 而不带参数的cin.get()可以读取下一个字符,因此可以用来处理换行符,为读取下一行输入做准备。 使用cin.get(name1,size);cin.get();等价于cin.get(name1

awk中的getline函数

匆匆过客 提交于 2019-11-29 04:26:05
getline getline函数得到下一行可能的返回值为: 1 如果能够读取一行。 0 如果到了文件末尾。 -1 如果遇到错误。 从文件中读取 getline函数除了能读取正常的输入流外,还可以从文件或管道中读取。 while((getline<"filepath")>0) print 将输入赋给一个变量 读取下一行并赋值给变量input: getline input 从管道读取输入 执行一个命令并将结果用管道输送到getline。 "who am i"|getline 当一个命令的输出结果被用管道输送给getline且包含多个行时,必须创建一个循环来执行getline。 while("who"|getline) who_out[++i]=$0 获取用户标准输入 1.getline<"-" 2.getline<"/dev/tty" 例子: awk 'BEGIN{printf "Enter your name:";getline<"-";print}' 三个$1 #!/bin/bash filename:awk_whoami.sh awk ' BEGIN{"who am i"|getline name= $1 FS=":" } name ~ $1 {print $5} ' $1 第一个$1:getline读取下一行将其赋给$0,故$1指新输入行的$1。 第二个$1:指第三个

Awk使用及站点日志分析

北城以北 提交于 2019-11-29 04:22:10
Awk 使用及站点日志分析 Awk 简单介绍 概述 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。 awk有3个不同版本号: awk、nawk和gawk,未作特别说明,一般指gawk。 awk程序的报告生成能力通经常使用来从大文本文件里提取数据元素并将它们格式化成可读的报告。最完美的样例是格式化日志文件。 awk程序同意从日志文件里仅仅过滤出你想要看的数据 Awk 使用 awk 命令格式和选项 语法形式 awk [options] 'script' var=value file(s) awk [options] -f scriptfile var=value file(s) 经常使用命令选项 -F fs fs指定输入分隔符,fs能够是字符串或正則表達式,如-F: -v var=value 赋值一个用户定义变量,将外部变量传递给awk -f scripfile 从脚本文件里读取awk命令 -m[fr] val 对val值设置内在限制。-mf选项限制分配给val的最大块数目;-mr选项限制记录的最大数目。这两个功能是Bell实验室版awk的扩展功能。在标准awk中不适用。 模式: /正則表達式/:使用通配符的扩展集。

awk的getline命令

女生的网名这么多〃 提交于 2019-11-29 04:21:39
原文出处:生活费的博客,http://www.cnblogs.com/276815076/archive/2011/12/05/2276625.html#undefined 两种情况:getline左右没有重定向符|或< 1. 当getline左右没有重定向符|或<时,getline读去当前文件的第一行并将数据保存到变量中,如果没有变量,则数据保存到$0中; 由于awk在处理getline之前已经读入了一行,所以getline得到的返回结果是隔行的。 2. 当getline左右有重定向符|或<时,getline作用于定向输入文件,由于该文件是刚打开,awk并没有读入一行数据,而getline读入了一行数据,那么getline返回的是该文件的第一行,而不是隔行。 root@myfreelinux pub]# awk ‘BEGIN{“cat kecheng.dat”|getline var;print var;}’ [root@myfreelinux pub]# awk ‘BEGIN{“cat kecheng.dat”|getline;print $0;}’ [root@myfreelinux pub]# awk ‘BEGIN{getline var<”kecheng.dat”;print var;}’ [root@myfreelinux pub]# awk ‘BEGIN

C++ using getline() prints: pointer being freed was not allocated in XCode

元气小坏坏 提交于 2019-11-29 04:04:12
I'm trying to use std:getline() but getting a strange runtime error: malloc: * error for object 0x10000a720: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug This is the code that produces this error: //main.cpp #include <iostream> #include <sstream> int main (int argc, char * const argv[]) { std::istringstream my_str("demo string with spaces"); std::string word; while (std::getline(my_str, word, ' ')) { std::cout << word << std::endl; } return 0; } Before each word I get this error. From the comments it seems to be a OSX/XCode specific error. Any hints

mmap slower than getline?

给你一囗甜甜゛ 提交于 2019-11-29 03:58:55
问题 I face the challenge of reading/writing files (in Gigs) line by line. Reading many forum entries and sites (including a bunch of SO's), mmap was suggested as the fastest option to read/write files. However, when I implement my code with both readline and mmap techniques, mmap is the slower of the two. This is true for both reading and writing. I have been testing with files ~600 MB large. My implementations parse line by line and then tokenize the line. I will present file input only. Here is

std::getline() returns

三世轮回 提交于 2019-11-28 17:53:41
I have a loop that reads each line in a file using getline() : istream is; string line; while (!getline(is, line).eof()) { // ... } I noticed that calling getline() like this also seems to work: while (getline(is, line)) What's going on here? getline() returns a stream reference. Is it being converted to a pointer somehow? Is this actually a good practice or should I stick to the first form? Charles Anderson The istream returned by getline() is having its operator void*() method implicitly called, which returns whether the stream has run into an error. As such it's making more checks than a

cin>> not work with getline()

这一生的挚爱 提交于 2019-11-28 14:42:21
#include <iostream> #include <string> using namespace std; int main () { string str; int age; cout << "Please enter age: "; cin>>age; cout << "Please enter full name: "; getline (cin,str); cout << "Thank you, " << str << ".\n"; } Why function getline() not work when I using uperator >> to input integer ? What is better use for int input ? You still have a newline in the stream after cin>>age; , which is giving you an empty string for the name. You could solve it by just adding another getline() call after getting the age and throwing away the result. Another options is to call cin.ignore(BIG