This question already has an answer here:
No matching function - ifstream open()
(1 answer)
Closed 7 years ago.
I'm trying to make a program that asks for the file that they user would like to read from, and when I try to myfile.open(fileName) I get the error: "no matching function for call to std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'" at that line.
string filename;
cout<<"Enter name of file: ";
cin>>filename;
ifstream myFile;
myFile.open(filename); //where the error occurs.
myFile.close();
In the previous version of C++ (C++03), open() takes only a const char * for the first parameter, instead of std::string. The correct way of calling it would then be:
myFile.open(filename.c_str());
In current C++ (C++11) that code is fine, though, so see if you can tell your compiler to enable support for it.
Related
This question already has answers here:
The difference between using fstream constructor and open function
(4 answers)
Why do C++ standard file streams not follow RAII conventions more closely?
(5 answers)
Closed 1 year ago.
I am currently writing a program to read data from a big .csv file and wanted to know if there's any difference between using:
ifstream handle("filename");
and
ifstream handle;
archivo.open("filename", ios::in);
when opening the file.
I have tried both so far and the two have worked in reading the data to then store it in an STL container. I wanted to know if there's any concrete difference in use, efficiency and/or memory use.
Thanks in advance!
There is no differences between them.
ifstream handle("filename"); - you make ifstream type variable and then you open a file in one part of code;
ifstream handle; - you make ifstream type variable;
handle.open("filename", ios::in); - you open file in another part of code.
Second version can be useful when you need to have an ifsteam like a member of a class.
For example:
class myClass
{
public:
myClass(std::string str)
{
file.open(str);
}
private:
ifstream file;
...
};
This question already has an answer here:
Read data from fstream
(1 answer)
Closed 3 years ago.
How can I check if there is more content in a text file in c++ and if there is continue to read it?
I am trying to read a some words from a text file but the number of words is not specified.
Check out this link here.
Also, std:: is a reference to the namespace of code you are calling. When you include a file that is in the standard libraries, such as string, vector, fstream, iostream, you need to either declare that your file will use the namespace std with using namespace std; OR you append std:: to the method or variable.
Use std::vector and std::string. Use a correct form of reading a file:
std::string word;
std::vector<std::string> word_database;
while (text_file >> word)
{
word_database.push_back(word);
}
std::cout << "Words read: " << word_database.size() << "\n";
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)
This question already has answers here:
How to convert std::string to LPCSTR?
(9 answers)
Closed 8 years ago.
I have a list of file names in a .txt document, and I would like to move each of these files from one folder to another.
Using MoveFileA() I am getting the error, "no suitable conversion between std::string and LCPSTR".
Here is my code, after opening up my .txt file:
while (std::getline(myfile, line))
{
std::string oldLocation = "C:\\Users\\name\\Desktop\\docs\\folder1\\" + line;
std::string newLocation = "C:\\Users\\name\\Desktop\\docs\\folder2\\" + line;
MoveFileA(oldLocation, newLocation);
}
If I type in the full path as arguments for MoveFileA, instead of sending it a variable, it works but I am unable to iterate over .txt file this way.
Any suggestions on how I might fix this?
LCPSTR means long constant pointer to a string, which means it's a null terminated c string.
std::string is an object. It is something different. But it luckily provides a convenience method c_str the provides a pointer to a constant c style string. So as the comment says you should go by:
MoveFileA(oldLocation.c_str(), newLocation.c_str());
It is worth of explicitly noting, that you can't drop it in every place instead of char*, but only when the string won't be modified. It returns const char*. This is where the C in LCPSTR gets important.
This question already has answers here:
Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?
(5 answers)
Closed 8 years ago.
I have a .txt file containing 6291456 numbers and nothing else. After reading all out and push_back into a vector, the vector.size() function returns 6291457. Where does this additional element come from?
int disparity;
ifstream disparity_txt;
disparity_txt.open(path);
while(!disparity_txt.eof())
{
disparity_txt >> disparity;
vec_disparities.push_back(disparity);
}
cout << vec_disparities.size() << endl;
disparity_txt.close();
Don't use while(!disparity_txt.eof()) it does not do what you think (eof will only be set after the end of the stream is read, so typically the last iteration is wrong) :
Do :
while(disparity_txt >> disparity)
{
vec_disparities.push_back(disparity);
}
Using while (!in.eof()) is almost always wrong
Either stop looping when extracting a number from the stream fails (as shown in quantdev's answer) or use the standard library facilities meant for populating a container from a stream:
std::ifstream disparity_txt(path);
vec_disparities.assign(std::istream_iterator<int>(disparity_txt),
std::istream_iterator<int>());
You can open an fstream using its constructor, and the destructor will close it, you don't need explicit open and close calls.
In C++11 it's even simpler:
vec_disparities.assign(std::istream_iterator<int>{std::ifstream{path}}, {});