getline

Read multiple strings from a file C++

Hi I need to read different values stored in a file one by one. So I was thinking I can use ifstream to open the file, but since the file is set up in such a way that a line might contain three numbers, and the other line one number or two numbers I'm not sure how to read each number one by one. I was thinking of using stringstream but...

User input... How to check for ENTER key

I have a section of code where the user enters input from the keyboard. I want to do something when ENTER is pressed. I am checking for '\n' but it's not working. How do you check if the user pressed the ENTER key? if( shuffle == false ){ int i=0; string line; while( i<20){ cout << "Playing: "; songs[i]->prin...

read pair of characters separated by \t c++

Friends, I want to read a pair of characters separated by \t. I want to continue reading the input until user enters z for any of the characters. Here are the options I thought: while (cin>>ch1>>ch2) { // process ch1 & ch2 } std::string str; while (getline(cin, str) ){ //split string } Also, I want to validate the input to make...

problem using getline with a unicode file

UPDATE: Thank you to @Potatoswatter and @Jonathan Leffler for comments - rather embarrassingly I was caught out by the debugger tool tip not showing the value of a wstring correctly - however it still isn't quite working for me and I have updated the question below: If I have a small multibyte file I want to read into a string I use the...

Why doesn't this for-loop execute?

I'm writing a program for an exercise that will read data from a file and format it to be readable. So far, I have a bit of code that will separate a header from the data that goes under it. Here it is: int main() { ifstream in("records.txt"); ofstream out("formatted_records.txt"); vector<string> temp; vector<string> hea...

C++ Detecting ENTER key pressed by user

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 ){ ...

String Vector program exits before input

So, I have a project that must add, delete, and print the contents of a vector... the problem is that, when run the program exits before I can type in the string to add to the vector. I commented the function that that portion is in. Thanks! #include <iostream> #include <cstdlib> #include <vector> #include <string> using namespace std...

Getline and 16h (26d) character

Hi, in VC++ environment Im using (string) getline function to read separate lines in opened file. Problem is that getline takes character 1Ah as end of file and if it is present on the line, whole reading ends prematurely. Is there any solution for this? Code snippet: fstream LogFile (Source,fstream::in); string Line while (getline(L...

Python equivalent of C++ getline()

In C++ we can enter multiple lines by giving our own choice of delimiting character in the getline() function.. however I am not able to do the same in Python!! it has only raw_input() and sys.stdin.readline() methods that read till I press enter. Is there any way to customize this so that I can specify my own delimiter? ...

C++ getline or cin not accepting a string with spaces, I've searched Google and I'm still stumped!

First of all, thanks to everyone who helps me, it is much appreciated! I am trying to store a string with spaces and special characters intact into MessageToAdd. I am using getline (cin,MessageToAdd); and I have also tried cin >> MessageToAdd;. I am so stumped! When I enter the sample input Test Everything works as intended. Ho...

C++ timeout on getline

I need all of my threads to check periodically that they should still be running, so that they can self-terminate when the program ends. For all but one of them, this is just a matter of checking a state variable, but the last one is a user-interaction thread, and its loop will wait indefinitely on user input, only checking the state var...

Unexpected behaviour of getline() with ifstream

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; ...

[C++] getline returning empty string

I'm having problems with the getline instruction from fstream. this is a snippet from my code: boolean_1=true; while(true) { if(boolean_1) { //some stuff } else { save_file.open("save.txt", fstream::in); //some stuff save_file.close(); } mission_file.open(filename, fstream::in); ...

Using getline when reading from a textfile

I'm initially building an index-like mechanism, read each line of the textfile using getline, checking if it matches a known header declaration (string.compare) and saving the tellg position as an index to that point. My intention is then to use seekg(index,ios::beg) to seek to the place in the file where the header is. After reading th...

Program is skipping over Getline() without taking user input

This is a very strange problem, when my program asks the user for the address, instead of waiting for input, it seems to skip the getline() function completely Answerinput: cout << "would you like to add another entry to the archive? (Y/N):"; cin >> answer; cout << endl; cout << endl; answer = toupper(answer); switch(answer) ...

C++ reading in specific segments of data from a file redirected to my program

I'm working on a program that takes a redirected file as input. For example, if my program was called foo I would call the program with ./foo < input.txt. The files I'm running through my program are supposed to be formatted with a single integer on the first line, two integers on the second line. So something like 3 1 8 I'm finding t...

cin.getline is skipping one line of input and taking the next

Why does cin.getline start working for the second line on the body input but break on the first? Example Program run: Enter name: Will Enter body: hello world hello again <= It accepts this one char* name = new char[100]; char* body = new char[500]; std::cout << "Enter name: "; std::cin.clear(); std::cin.ignore(std::numeric_l...

C++ std::getline size limit on Mac OSX

I'm having problems with std::getline on Mac OSX Snow Leopard. For some reason it limit the size of the input, while on Debian/Ubuntu it's unlimited size? std::getline(std::cin, input) Any clues about the limit? ...

Loop problem. Cin C++ getline clear buffer

I am having a problem, but I cannot figure out what I'm doing wrong. I'm not sure if it's a problem with my loop, or the cin buffer is not being cleaned. I am doing a program that transforms a C-style string to uppercase, however if the user enters more than 11 characters, then the function should only display the first 11, and anything ...

problem with getline() function

Hello, I am a beginner at C++ and I'm trying to use the getline() function for the first time. When I wrote this code, 2 errors showed up. What is this code supposed to do? It is supposed to read 4 numbers from read.txt then calculate it to find the mean and write the output in output.txt. The 4 numbers (in read.txt) are all on separ...