C++ - No matching function for call to 'getline' - c++

I cannot figure out the proper syntax for a file istream getline() call
I've tried so many variations of calling getline() with all different kinds of parameters and after looking at several different pieces of documentation and it just won't work.
std::ifstream in("file.txt");
char tmp;
std::getline(tmp, in);
This one results in
../directory/file.cpp:178:2: error: no matching function for call to 'getline'
std::getline(tmp, in);
^~~~~~~~~~~~
But other documentation says
std::ifstream in("file.txt");
char tmp;
in.getline(tmp);
which also spits out
../directory/file.cpp:179:5: error: no matching member function for call to
'getline'
in.getline(tmp);
^~~~~~~~~~~~
All I need to do is read a file line by line and I can't figure it out. Could someone please point me in the right direction? I can provide more information if needed.

getline() reads string, but you pass it a single char.
Use it like this:
std::ifstream in("file.txt");
std::string tmp;
std::getline(in, tmp);

Related

How to use string as ifstream variable name in C++

I have one text file variables_n_paths.txt with these entries :
nsf_ttj somepath1.txt
nsf_zz somepath2.txt
hsf_ttw somepath3.txt
hsf_wz somepath4.txt
What I want is something like this (by using a loop):
ifstream nsf_ttj(somepath1.c_str());
ifstream nsf_zz(somepath2.c_str());
ifstream hsf_ttw(somepath3.c_str());
ifstream hsf_wz(somepath4.c_str());
What I do to achieve above is :
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ifstream variable;
string path;
ifstream readfile("variables_n_paths.txt");
while(true){
if(readfile.eof()) break;
readfile >> variable >> path; //it gives error here
}
return 0;
}
This is the error I am getting :
error: ambiguous overload for ‘operator>>’ (operand types are ‘std::ifstream {aka std::basic_ifstream}’ and ‘std::ifstream {aka std::basic_ifstream}’)
I am wondering if this is even possible. Any hints will be appreciated. Thanks in advance.
You are trying to create objects whose names are based on the input file's contents. This is not possible with pure c++ as the object names must be known at compile time.
One alternative is to read in the 'variable names' and filenames as strings, store them in a map and iterate through the map.
If you absolutely must create variable names from the file's contents, you will need to to use an external preprocessor that parses the text file and generates the corresponding c++ code containing the proper variable names.
Yes, it is possible to extract a string from a (file) stream. Your problem, as the error explains in more technical terms, is that variable is an ifstream and you cannot extract an ifstream from a stream. Simply change the type of variable to std::string and the extraction works.
Now that you have the name of the file in a string, you can stream the file:
std::string variable, path;
while(true) {
readfile >> variable >> path;
std::ifstream foo(path);
You can then go on and stream the contents of the file, and perhaps store them in a std::map using variable as key - or whatever you wish to do with your variables.

C++ Getline issues (No instance of overloaded function "getline"

I know I know.
This question has been asked before, but I've looked at all the answers and none seem to solve my problem. When I use the getline function to get the contents of a line in the file, it doesn't work.
getline(file, line);
'File' is declared here:
ifstream File;
File.open("fruit.txt");
and 'line' is declared here:
int line = 0;
Getline is underlined in red with this message:
getline
no instance of overloaded function "getline" matches the argument list
argument types are :(std::ifstream, int)
What this means is no instance of getline has the argument list of the file stream and an integer.
This makes no sense as all the other questions on this matter state exactly that, that the arguments are the file stream and an integer.
What am I doing wrong?
EDIT:
Here is the full code:
ifstream fruitFile;
fruitFile.open("fruit.txt");
int line = 0;
int C_FRUIT = getline(fruitFile, line);
fruitFile.close();
The first line should be a number, and I need it.
getline() will read one line of text. It can't read directly an int. This is why you get your error message.
You have to be aware that there are two getline(). There is one which is istream::getline() and std::getline(). Both have different signatures. The first is a member function of a stream and is defined in the stream header; the latter is defined in the <string> header.
But pay attention: the return value of std::getline() is not an int ! It's a stream reference. This is why you get a second compiler error.
Finally if you want to read an integer x, it's easier to use extractors:
int value;
fruitFile >> value;
fruitFile.ignore(SIZE_MAX, '\n'); // in case you'd need to go to next line
Or if you really want to read an int in a full line:
string line;
getline(fruitFile, line);
stringstream sst(line); // creates a string stream: a stream that takes line as input
sst >> value;
The second argument of getline needs to be a string: http://www.cplusplus.com/reference/string/string/getline/
I think what you try to achieve is:
ifstream fruitFile;
fruitFile.open("fruit.txt");
int line = 0;
fruitFile >> line
fruitFile.close();
I faced the same error. add this to your code to solve the problem
Add the string library
include <string>
Add the below function call, where string_variable should be of type string.
std::getline(cin, sting_variable)

reading input in from a file

so I'm a newbie at C++, and I've been poking around on the internet on how to do this, and so far I have this:
void includeFile(string name){
ifstream ifs;
ifs.open(name);
string commands;
while (getline(ifs,commands)){
commandReader(ifs);
}
ifs.close();
}
(commandReader is a function that takes an istream)
When I try to compile, I get the error "no matching function for call" and then gives me the line number for the ifs.open(name) line. I've included fstream, so not sure why it's doing this
Sorry, never mind; found the answer right after I posted this.
The solution was to have name.c_string() as the parameter instead, as string support was only added in c++11
As #chris pointed out, pre-C++11, ifs.open expects a char*, not an std::string. Try ifs.open(name.c_str()).

c++: Reading a file line by line

I'm wondering if there's a C++ way of opening a file and reading the input line by line.
I encountered the following code that accomplishes the task:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream myfile;
myfile.open ("example.txt");
return 0;
}
I'm encouraged to not use any C functions or commands.
The thing is, my "example.txt" is in the form of a string, and using str.c_str() is a C function, so I guess I have two ways to solve the issue.
Is there another way to read input from a file line by line? Perhaps using something that will accept a string as a parameter for the filepath? Is there a C++ way of doing things? :)
Or, is there another way to convert the string in to a const char *, which is what the myfile.open() function needs?
Many thanks in advance!
EDIT: My lack of practivity and research led me to think c_str() was a C function, and it isn't. My apologies. Since it isn't I have found my answer.
C++11's fstream constructor accepts string. In most cases, you want to use fstream's constructor, rather than .open() - you save one line and one function call.
For reading the file line-by-line, you should use std::getline().
Also note that string::c_str() is still C++ function, not C one, as well as fstream's constructor taking const char *. Most of (if not all, I'm not 100% sure) C standard library function are also included in C++ standard.
Since the issue about str.c_str() is already answered, I'm just gonna add a bit about getting inputs line by line. for example, you wanna take 2 ints input per line, extract them, and put it into a vector.
fstream fs(filename.c_str(), ios_base::in);
string line;
stringstream ss;
int a,b;
vector<int> d;
int numlines;
int i;
for (i = 0; getline(fs, line); i++) {
for (ss.str(line); ss >> a >> b; d.push_back(a), d.push_back(b)) {}
ss.clear();
}
numlines = i;
Hope you get the idea of using getline() and fstream()
It's going to look very similar. You'll want an ifstream instead of an ofstream, you'll want the >> operator, and assuming your file has more than one line, you'll need a loop and the ifstream::feof() function.

Can't getline() from istringstream (C++)

I have a previously declared char c[64]; and I'm trying to look at the first word of the output of a pipe:
read(pipe_replacement_to_main[READ_END], c, BUF_SIZE);
istringstream response_stream(string(c));
string response_string;
getline(response_stream, response_string, ' ');
And gcc gives me the following at that fourth line:
error: no matching function for call to ‘getline(std::istringstream (&)(std::string), std::string&, char)’
I can't even figure out how it's trying to call the function. Did I declare the istringstream wrong?
Most vexing parse, add a pair of parenthesis inside the constructor of response_stream.
istringstream response_stream((string(c)));
A nice demonstration of the true "power" of C++.
The way you declared the response_stream variable, it is actually a function rather than type istringstream.