No matching function for call to 'get line' while exploding - c++

No matching function for call to 'getline' when using this code:
ifstream myfile;
string line;
string line2;
myfile.open("example.txt");
while (! myfile.eof() )
{
getline (myfile, line);
getline (line, line2, '|');
cout<<line2;
}
In the example.txt i have info like this:
1|Name1|21|170
2|Name2|34|168
etc...
I really want to get the line till the | char...
I try few explode functions but they are only in string type but i need:
1st to be int
2nd to be char
3rd and 4th to be float.
It's really complicated that i want to do, and i can't explain it well. I hope someone will understand me.

getline receives as first argument an instance of the template basic_istream. string doesn't meet that requirement.
You could use stringstream:
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string line;
string line2;
ifstream myfile("test/test.txt");
while (getline(myfile, line))
{
stringstream sline(line);
while (getline(sline, line2, '|'))
cout << line2 << endl;
}
return 0;
}

Related

How to read from a text file and split it into values in C++

This is my text file (mytext.text):
rho_0,10
kp_0,8
Beta_kp,6
x_min,5
x_max,8
y_min,9
y_max,5
z_min,4
z_max,7
I want to read from this text file line by line, and store each value in the parameter at the same line.
For example, for the first line, store 10 in rho_0.
I have written this code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
ifstream myfile("mytext.txt");
if (myfile.is_open())
while (getline(myfile, line))
{
cout << line << endl;
}
}
But I don't think it will save the value for the corresponding parameter.
I know that I should split each line by delimiter, and then convert string to double (float), but I don't know how to implement it for each line.
You'll want to use a map data structure of some sort to store parameter values. You'll probably want std::unordered_map for this. Like #user4581301 said, you can use std::getline with a specific delimiter, like a comma.
std::unordered_map<std::string, float> params;
std::ifstream myfile("mytext.txt");
std::string param_name;
float param_value;
while (std::getline(myfile, param_name, ',')) {
myfile >> param_value;
myfile.get(); // discard newline
params.insert({ param_name, param_value });
}
This code doesn't handle invalid input, so you can add error handling if you want.

Counting newlines in a file

I am trying to read the number of newlines in a text file. However, my counter is not working. Is it because of the string comparison?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
ifstream myFile;
int temp_height = 0;
myFile.open("Levels.txt");
while (!myFile.eof())
{
getline(myFile,line);
if (line == "\n")
temp_height++;
}
cout<<"\n Newlines: "<<temp_height;
myFile.close();
}
Change:
while (!myFile.eof())
{
getline(myFile,line);
to
while (getline(myFile, line))
{
This means you are actually reading before you check, and also that you check for other failures. You almost never want to actually check eof, it probably doesn't work quite how you expect.
Edit: Ok, you want blank lines. getline discards the '\n' character, so check for
if (line.empty())
for a final loop:
while (getline(myFile, line))
{
if (line.empty())
++temp_height;
}
You can find documentation for std::getline here.

Why is getline() in C++ not working? (no matching function for call to 'getline(std::ofstream&, std::string&)'

I'm trying to read from a file, but C++ is not wanting to run getline().
I get this error:
C:\main.cpp:18: error: no matching function for call to 'getline(std::ofstream&, std::string&)'
std::getline (file,line);
^
This is the code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
int main(){
string line;
std::ofstream file;
file.open("test.txt");
if (file.is_open())
{
while ( file.good() )
{
getline (file,line);
cout << line << endl;
}
file.close();
}
}
std::getline is designed for use with input stream classes (std::basic_istream) so you should be using the std::ifstream class:
std::ifstream file("test.txt");
Moreover, using while (file.good()) as a condition for input in a loop is generally bad practice. Try this instead:
while ( std::getline(file, line) )
{
std::cout << line << std::endl;
}
std::getline reads characters from an input stream and places them into a string. In your case your 1st argument to getline is of type ofstream. You must use ifstream
std::ifstream file;

Won´t read whitespace with ifstream

I am relatively new to C++, so be gentle.
I have a text-file I want to read, but when i read the file it skips the whitespace (space) between separated words.
I tried to take away as much junk-code as possible so it would be easier to read.
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
int main(.....)
{
ifstream in_file;
string filename;
string status;
readStringToMem(in_file, status);
cout << "Type in the filename : ";
getline(cin, filename);
in_file.open(filename);
readStringToMem(in_file, status);
}
void readStringToMem(ifstream& in_file, string& string_value)
{
string input_string;
getline(in_file, input_string, '|');
stringstream myInputStream(input_string);
myInputStream >> string_value;
}
My file may look like this:
Status is ok | 100
But when I read it, it comes out like this:
Status 100
Thanks in advance! Any help will be great!
You are trying too hard, this
void readStringToMem(ifstream& in_file, string& string_value)
{
string input_string;
getline(in_file, input_string, '|');
stringstream myInputStream(input_string);
myInputStream >> string_value;
}
should be this
void readStringToMem(ifstream& in_file, string& string_value)
{
getline(in_file, string_value, '|');
}
Much simpler, in fact readStringToMem is so simple I wonder if it's worth putting into a separate function.
I think you were probably confused by the integer case. In that case you have to convert the string you have read with getline to an integer. And you would do that using stringstream. But in the string case you already have a string so there is no conversion to do and no need for stringstream.

is it possible to read from a specific character in a line from a file in c++?

Hey all so I have to get values from a text file, but the values don't stand alone they are all written as this:
Population size: 30
Is there any way in c++ that I can read from after the ':'?
I've tried using the >> operator like:
string pop;
inFile >> pop;
but off course the whitespace terminates the statement before it gets to the number and for some reason using
inFile.getline(pop, 20);
gives me loads of errors because it does not want to write directly to string for some reason..
I don't really want to use a char array because then it won't be as easy to test for the number and extract that alone from the string.
So is there anyway I can use the getline function with a string?
And is it possible to read from after the ':' character?
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
string fname;
cin >> fname;
ifstream inFile;
inFile.open(fname.c_str());
string pop1;
getline(inFile,pop1);
cout << pop1;
return 0;
}
ok so here is my code with the new getline, but it still outputs nothing. it does correctly open the text file and it works with a char array
You are probably best to read the whole line then manipulate the string :-
std::string line;
std::getline(inFile, line);
line = line.substr(19); // Get character 20 onwards...
You are probably better too looking for the colon :-
size_t pos = line.find(":");
if (pos != string::npos)
{
line = line.substr(pos + 1);
}
Or something similar
Once you've done that you might want to feed it back into a stringstream so you can read ints and stuff?
int population;
std::istringstream ss(line);
ss >> population;
Obviously this all depends on what you want to do with the data
Assuming your data is in the form
<Key>:<Value>
One per line. Then I would do this:
std::string line;
while(std::getline(inFile, line))
{
std::stringstream linestream(line);
std::string key;
int value;
if (std::getline(linestream, key, ':') >> value)
{
// Got a key/value pair
}
}