getline

Why doesn't std::getline block?

纵然是瞬间 提交于 2019-12-05 08:33:16
I have this code in an Objective-C class (in an Objective-C++ file): +(NSString *)readString { string res; std::getline(cin, res); return [NSString stringWithCString:res.c_str() encoding:NSASCIIStringEncoding]; } When I run it, I get a zero-length string, Every time. Never given the chance to type at the command line. Nothing. When I copy this code verbatim into main() , it works. I have ARC on under Build Settings. I have no clue what it going on. OSX 10.7.4, Xcode 4.3.2. It is a console application. It means there is input waiting to be read on the input. You can empty the input: cin.ignore

Using getline() with file input in C++

大兔子大兔子 提交于 2019-12-05 07:57:09
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.close(); The problem with this is that it outputs the entire line "John Smith 31". Is there a way I can

Why seekg does not work with getline?

自闭症网瘾萝莉.ら 提交于 2019-12-05 07:18:34
Seekg does not seem to work, when I reach EOF in myFile. ifstream myFile("/path/file"); for(int i; i < 10; i++){ myFile.seekg(0);//reset position in myFile while(getline(myFile, line)){ doSomething } } So, now I am opening input stream every loop: for(int i; i < 10; i++){ ifstream myFile("/path/file");//reset position in myFile while(getline(myFile, line)){ doSomething } } But I would rather seek to position 0. How can I achieve that? Joseph Mansfield Make sure you clear the error flags before the call to myFile.seekg() : myFile.clear(); After the EOF flag has ben set, you will not be able to

getline() skipping first even after clear()

自作多情 提交于 2019-12-04 19:51:24
问题 So I have a function that keeps skipping over the first getline and straight to the second one. I tried to clear the buffer but still no luck, what's going on? 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 } 回答1: Make sure you didn't use cin >> str .

Read table of numbers into arrays when number of rows and columns determined at runtime

廉价感情. 提交于 2019-12-04 18:12:53
I would like to ask you about data input. I have a text file in the following format: 7 2 X Y 1 0 2 0.048922 3 0.0978829 4 0.146908 5 0.196019 6 0.245239 7 0.294584 The first line contains the number of rows and columns to be read in. The second line are headers. From the third line onwards it's only data. I would like to read my data into a 2D array (mat[][]) and the headers into an array of strings (title[]), which could be easily referenced later. I came this far writing the script. It can read the array numbers into the array, but not a 2D one. I was trying to declare a pointer for the

Reading input from text file to array in c++

三世轮回 提交于 2019-12-04 06:54:02
问题 Alright, be gentle, since I'm very much a novice to programming. So far I've only studied C++ and I'm running Visual Studio 2010 as my compiler. For this program, I'm trying to read from a text input file and write the information to a set of three arrays. One array will handle a list of names, and the other two are for hours worked and hourly pay rate, respectively. I will use the latter two to calculate a set of earnings and output the whole thing to another text file as a report. My

Code to get user input not executing/skipping in C++

[亡魂溺海] 提交于 2019-12-04 06:28:51
问题 In the below code, I'm running into an error when I try to get the user to input their name. My program just skips it over and goes right over to making the function calls without allowing the user to enter their name. Despite the error, my program is compiling. I'm not sure what's going wrong as I wrote that part based off other examples I found on here. Any suggestions? #include <iostream> #include <string> #include <time.h> using namespace std; char showMenu(); void getLottoPicks(int[]);

c++ getline reads entire file in Windows

微笑、不失礼 提交于 2019-12-04 05:43:28
问题 This looks like a similar question to this one, however I think my case might actually be a bit different. The code is as below: void readOmronResults(string fileName) { ifstream inFile(fileName); ofstream testRead("test_read.txt"); string line; //getline(inFile, line); //cout << line << endl; while (getline(inFile, line)) { testRead << line << endl; } inFile.close(); testRead.close(); cout << "Finished reading omron results" << endl; } testRead is just used for debugging. The input file is a

Keep Leading zeros C

依然范特西╮ 提交于 2019-12-04 05:05:06
问题 I am trying to read the memory addresses from /proc//maps and I use the following code for (ptr = NULL; getline(&ptr, &n, file) > 0;) { if (ptr[0]== ' ') { continue; } sscanf(ptr, "%lx-%lx", &r0, &r1); printf("r0: %lx, r1: %lx\n", r0, r1); } Assume that file points to /proc//maps & ptr is the line pointer. But when you consider a maps file, it doesn't read the file proper. It drops the zero, it does not pick the zeros up. So consider: 00110000-00123000 r-xp 00000000 08:01 129925 /lib/i686

getline() skipping first even after clear()

早过忘川 提交于 2019-12-04 01:54:45
So I have a function that keeps skipping over the first getline and straight to the second one. I tried to clear the buffer but still no luck, what's going on? 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 } Make sure you didn't use cin >> str . before calling the function. If you use cin >> str and then want to use getline(cin, str) , you must call cin