我正试图通过面试街学习二郎.我现在只是学习这门语言所以我什么都不知道.我想知道如何从stdin读取并写入stdout.
我想写一个写"Hello World!"的简单程序.stdin收到的次数.
所以用stdin输入:
6
Run Code Online (Sandbox Code Playgroud)
写到标准输出:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Run Code Online (Sandbox Code Playgroud)
理想情况下,我会一次读取stdin一行(即使在这种情况下它只是一位数)所以我想我将使用get_line.这就是我现在所知道的.
谢谢
谢谢
我想知道如何在getline功能中使用2个或更多分隔符,这是我的问题:
该程序读取一个文本文件......每一行都是如下:
New Your, Paris, 100
CityA, CityB, 200
Run Code Online (Sandbox Code Playgroud)
我正在使用getline(文件,行),但我得到整条线,当我想要获得CityA,然后是CityB,然后是数字; 如果我使用','分隔符,我不知道下一行是什么时候,所以我试图找出一些解决方案..
虽然,我怎么能用逗号和\n作为分隔符?顺便说一句,我正在操纵字符串类型,而不是char,所以strtok是不可能的:/
一些划痕:
string line;
ifstream file("text.txt");
if(file.is_open())
while(!file.eof()){
getline(file, line);
// here I need to get each string before comma and \n
}
Run Code Online (Sandbox Code Playgroud) 我目前正在进行的实验使用的软件库具有复杂的源历史记录,并且没有明确定义的许可证.合理化事物并在固定许可下发布将是一项相当大的工作.
它也打算运行一个随机的unixish平台,只有我们支持的一些libc有GNU getline,但现在代码期望它.
有没有人知道在限制较少的许可下可以重新实现GNUgetline语义?
编辑::我问,因为谷歌没有帮助,我想尽可能避免写一个(这可能是一个有趣的练习,但它不能最好地利用我的时间.)
更具体地说,有问题的界面是:
ssize_t getline (char **lineptr, size_t *n, FILE *stream);
Run Code Online (Sandbox Code Playgroud) 这是我第一次使用stackoverflow.我一直无法找到关于getline的所需信息.我是一个简单的工程转移编程类,所以我们编写的代码非常简单.我在这里尝试做的就是将用户定义的问题和答案放入两个不同的数组中.我的while循环看起来像这样(我正在使用for循环,但切换到只是为了看它是否会停止破坏):
int main ()
{
srand((unsigned)time(0));
string quest1[100], answ1[100];
int size1, x = 0, num, count1, visit[100], shuffle[100];
fstream flashcard1;
cout << "flashcard.cpp by NAME\n" << endl;
cout << "This program allows user to manipulate questions and answers for studying.\n" << endl;
cout << "\nHow many flash cards will be entered(MAX 100)? ";
cin >> size1;
cout << endl;
while(x < size1)
{
cout << "Enter Question: ";
getline(cin , quest1[x]);
cout << endl;
x = x++;
/*
cout << …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用std:getline()但遇到一个奇怪的运行时错误:
malloc:*对象0x10000a720的错误:未释放指针被释放*在malloc_error_break中设置断点以进行调试
这是产生此错误的代码:
//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;
}
Run Code Online (Sandbox Code Playgroud)
在每个单词之前我都会收到此错误.从评论中看起来似乎是OSX/XCode特定的错误.有什么提示吗?
更新: 错误仅在调试模式下打印.如果我在发布模式下构建此代码,一切都很好.
更新2: 可以在此处找到有关该问题的更多信息.
解:
组
_GLIBCXX_FULLY_DYNAMIC_STRING = 1
在目标信息构建选项卡中的预处理器宏中.
系统信息:
OSX 10.6.2 | XCode 3.2 | g ++ 4.2 | 调试i386的配置
我写了一个处理GPU上数据的应用程序.代码运行良好,但我有一个问题,输入文件的读取部分(~3GB,文本)是我的应用程序的瓶颈.(从HDD读取速度很快,但逐行处理很慢).
我用getline()读取一行,将第1行复制到向量,将第2行复制到向量,并跳过第3和第4行.依此类推其余11条mio行.
我尝试了几种方法来尽可能地获取文件:
我发现最快的方法是使用boost :: iostreams :: stream
其他人是:
任何建议如何让它运行得更快?
void readfastq(char *filename, int SRlength, uint32_t blocksize){
_filelength = 0; //total datasets (each 4 lines)
_SRlength = SRlength; //length of the 2. line
_blocksize = blocksize;
boost::iostreams::stream<boost::iostreams::file_source>ins(filename);
in = ins;
readNextBlock();
}
void readNextBlock() {
timeval start, end;
gettimeofday(&start, 0);
string name;
string seqtemp;
string garbage;
string phredtemp;
_seqs.empty();
_phred.empty();
_names.empty();
_filelength = 0;
//read only a part of the file i.e the first 4mio lines
while (std::getline(in, …Run Code Online (Sandbox Code Playgroud) 所以我有一个功能,一直跳过第一个getline,直接跳到第二个getline.我试图清除缓冲区,但仍然没有运气,发生了什么?
void getData(char* strA, char* strB)
{
cout << "Enter String 1: "; // Shows this line
cin.clear();
cin.getline(strA, 50); // 50 is the character limit, Skipping Input
cout << endl << "Enter String 2: "; // Showing This Line
cin.clear();
cin.getline(strB, 50); // Jumps Straight to this line
}
Run Code Online (Sandbox Code Playgroud) 在POSIX awk中,如何通过处理输出command 后获取退出状态(返回代码)command | getline var?exit 1如果command以非零退出状态退出,我想要我的awk脚本.
例如,假设我有一个名为的awk脚本foo.awk,如下所示:
function close_and_get_exit_status(cmd) {
# magic goes here...
}
BEGIN {
cmd = "echo foo; echo bar; echo baz; false"
while ((cmd | getline line) > 0)
print "got a line of text: " line
if (close_and_get_exit_status(cmd) != 0) {
print "ERROR: command '" cmd "' failed" | "cat >&2"
exit 1
}
print "command '" cmd "' was successful"
}
Run Code Online (Sandbox Code Playgroud)
然后我想要发生以下事情:
$ …Run Code Online (Sandbox Code Playgroud) 我用于简单字符串拆分的常见代码如下所示:
inline std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
Run Code Online (Sandbox Code Playgroud)
有人提到这会默默地"吞噬"发生的错误std::getline.当然,我同意这种情况.但是我想到了,在实践中我可能会出现什么问题,我需要担心.基本上这一切归结为:
inline std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim)) {
elems.push_back(item);
}
if(/* what error can I catch here? */) {
// *** How did we get here!? ***
}
return elems;
}
Run Code Online (Sandbox Code Playgroud)
A stringstream由a支持string,因此我们不必担心与从文件读取相关的任何问题.此处没有类型转换,因为getline只需读取直到它看到行分隔符或EOF.所以我们不能得到任何类似的错误boost::lexical_cast …
我正在阅读C++ Primer一书并尝试所有代码示例.我对这个很感兴趣:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string line;
while (getline(cin,line))
cout << line << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译这段代码之前,我猜测编译会失败,因为我没有使用
while (std::getline(cin,line))
Run Code Online (Sandbox Code Playgroud)
为什么getline在全局命名空间中?据我所知,这应该只在我使用时发生
namespace std;
Run Code Online (Sandbox Code Playgroud)
要么
using std::getline;
Run Code Online (Sandbox Code Playgroud)
我在Linux Mint Debian Edition上使用g ++版本4.8.2.