getline

Function to insert text line by line from a file into string array passed by pointer

喜夏-厌秋 提交于 2019-12-24 10:10:01
问题 I'm trying to create a function read_lines that takes a file *fp, a pointer to char** lines, and pointer to int num_lines. The function should insert each line of text into lines, and increase num_lines to however many lines the file has. Its probably really simple but I've been trying to insert the text for several hours now. This is what main.c would look like. Everything but read_lines is already defined and working. int main(int argc, char* argv[]){ char** lines = NULL; int num_lines = 0;

Why does libc++ getline block when reading from pipe, but libstdc++ getline does not?

好久不见. 提交于 2019-12-23 10:06:30
问题 TL;DR A program that uses the libc++ version of the getline function will block when it reads input from a pipe until the pipe's buffer is full. The same is NOT true for the libstdc++ version of the getline function: Here the function immediately reads and returns a line of input as soon as it becomes available. Should I expect this difference in behaviour between libstdc++ and libc++ ? [ EDIT: I am not fishing for an opinion here, I simply don't know enough about pipes nor the difficulties

Erlang read stdin write stdout

假装没事ソ 提交于 2019-12-23 07:01:10
问题 I'm trying to learn erlang through interviewstreet. I just learning the language now so I know almost nothing. I was wondering how to read from stdin and write to stdout. I want to write a simple program which writes "Hello World!" the number of times received in stdin. So with stdin input: 6 Write to stdout: Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Ideally I will read the stdin one line at a time (even though it's just one digit in this case) so I think I

haskell -skipping getLine

懵懂的女人 提交于 2019-12-23 05:13:08
问题 hey - great coders and haskellers, i'm a haskell freshman and have a problem with a program it boils down to the following situaition main :: IO () main = do putStrLn "\nplease give me some input" input1 <- getLine putStrLn "\nplease give me another input" input2 <-getLine putStrLn ("\nyour inputs were "++show(input1)++" and "++ show(input2)") putStrLn "restart ?? yY or nN" c <- getChar restart c where restart c |elem c "yY" = do main |elem c "nN" = putStrLn "\nExample Over" |otherwise = do

C++ Detecting ENTER key pressed by user

我们两清 提交于 2019-12-23 04:27:03
问题 I have a loop where I ask the user to enter a name. I need to stop when the user presses the ENTER key..... or when 20 names have been entered. However my method doesn't stop when the user presses the ENTER key //loop until ENTER key is entered or 20 elements have been added bool stop = false; int ind = 0; while( !stop || ind >= 20 ){ cout << "Enter name #" << (ind+1) << ":"; string temp; getline(cin, temp); int enterKey = atoi(temp.c_str()); if(enterKey == '\n'){ stop = true; } else{ names

Unexpected behaviour of getline() with ifstream

為{幸葍}努か 提交于 2019-12-22 18:24:56
问题 To simplify, I'm trying to read the content of a CSV-file using the ifstream class and its getline() member function. Here is this CSV-file: 1,2,3 4,5,6 And the code: #include <iostream> #include <typeinfo> #include <fstream> using namespace std; int main() { char csvLoc[] = "/the_CSV_file_localization/"; ifstream csvFile; csvFile.open(csvLoc, ifstream::in); char pStock[5]; //we use a 5-char array just to get rid of unexpected //size problems, even though each number is of size 1 int i =1; /

Problem with getline and threads

梦想的初衷 提交于 2019-12-22 11:08:55
问题 I have client that works on 2 threads. One is sending data and second one is receiving data. In sending data I have std::getline(std::cin,string) . Now in this thread I have infinite loop which ends if second threads end or if user put EXIT command, everything work great expect that when second thread is ended and infinite loop is ended, program still waits for pressing button because of std::getline . Now to question: How can I send data to getline to "fake" pressing button so I don't have

Problem with getline and threads

混江龙づ霸主 提交于 2019-12-22 11:08:17
问题 I have client that works on 2 threads. One is sending data and second one is receiving data. In sending data I have std::getline(std::cin,string) . Now in this thread I have infinite loop which ends if second threads end or if user put EXIT command, everything work great expect that when second thread is ended and infinite loop is ended, program still waits for pressing button because of std::getline . Now to question: How can I send data to getline to "fake" pressing button so I don't have

getline in if statement

不想你离开。 提交于 2019-12-22 08:11:50
问题 From what I've read, getline() used in a Boolean context returns an implicitly conversion to void* . I haven't found anywhere on the web any real reference to this statement. Everywhere it says that implicit conversion doesn't exist and that in a Boolean context pointers should be of the same kind (and if ptr == 0 than 0 is converted to type of the pointer ptr ). Also in the standard says in a Boolean context it is converted to an unspecified-Boolean-type. What does that even mean? 回答1: In

Using getline() with file input in C++

旧街凉风 提交于 2019-12-22 05:19:40
问题 I am trying to do a simple beginner's task in C++. I have a text file containing the line "John Smith 31". That's it. I want to read in this data using an ifstream variable. But I want to read the name "John Smith" into one string variable, and then the number "31" into a separate int variable. I tried using the getline function, as follows: ifstream inFile; string name; int age; inFile.open("file.txt"); getline(inFile, name); inFile >> age; cout << name << endl; cout << age << endl; inFile