I have some text files in a folder named foo1, foo2,...,foo5. I tried to write a C++ program
to print out the contents of the files but the compiler is giving an error.
Here is the program.
int main(int argc, char *argv[])
{
string common="foo";
for(int count=1;count<=5;++count)
{
//Convert count to an string.
stringstream ss;
ss<<count;
string numstring=ss.str();
string filename=common+numstring;
ifstream infile(filename);
string line;
//Print out the lines from the file.
while(getline(infile,line))
{
cout<<line<<endl;
}
}
return 0;
}
The compiler is giving an error like
g++ -Wall c++.cpp
c++.cpp: In function ‘int main(int, char**)’:
c++.cpp:26: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/fstream:440: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/iosfwd:81: note: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)
Can someone help me out with this? The output of the program should be what I would get if I typed cat foo* at the terminal.
Replace:
ifstream infile(filename);
with
ifstream infile(filename.c_str());
The error message that you posted shows you this. fstream is a typedef for basic_fstream<>. Your error message says, in essence, that there is no fstream constructor that takes a string&, but there is one that takes a char*.
You can't do this:
ifstream infile(filename);
The ifstream class doesn't know how to take a string as a filename. I does know how to take a character array:
ifstream infile(filename.c_str());
You should have worked this out with a single input file, before you introduced the iteration. Start simple and build up. Test all the way. Never add to code that doesn't work.
Related
I am reading abc.cpp file which is placed under /home/documents/abc.cpp. To open file I am performing file operation open("t.open("/home/documents/abc.cpp"). where i am able to perform open operation on file.
I want to try to read file name using command line argument. so what i am trying here is in command line
./a.out abc.cpp , passing argv[1] as file name and concatenate string path + argv[1], when i compile the code i will thrown with compilation errors, how to solve this issue please help.
#include <iostream>
#include <fstream>
#include <sstream>
#include<string.h>
#include <ext/stdio_filebuf.h>
using namespace std;
int main(int argc, char *argv[])
{
ifstream t;
string path = "/home/documents/";
string file = path + argv[1];
t.open(file);
//t.open("/home/documents/abc.cpp");
string buffer;
string line;
while(t)
{
getline(t, line);
// ... Append line to buffer and go on
buffer += line;
buffer += "\n";
}
t.close();
return 0;
}
compilation error
g++ cmdLine.cpp
cmdLine.cpp: In function ‘int main(int, char**)’:
cmdLine.cpp:13:32: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
t.open(file);
^
cmdLine.cpp:13:32: note: candidate is:
In file included from cmdLine.cpp:2:0:
/usr/include/c++/4.8.2/fstream:538:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s, ios_base::openmode __mode = ios_base::in)
^
/usr/include/c++/4.8.2/fstream:538:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
t.open(file.c_str()); will solve your problem. Until C++11 the only function declaration was
void open( const char *filename,
ios_base::openmode mode = ios_base::in );
The error message informs you very clearly: no known conversion from ‘std::string’ to ‘const char*’.
Sometimes I get incredibly long errors in my code that I don't understand so I just rework my code to avoid whatever was causing the error. I had another one today that I simply can't avoid.
My code:
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
void readFile(string);
class info {
public:
int rows;
int cols;
vector < string > data;
};
int main(int argc, char **argv){
string filename1;
filename = argv[1];
readFile(filename);
return 0;
}
//should read onle line at a time from a file and print it
void readFile(string filename1){
fstream datafile;
datafile.open(filename1);
while (!datafile.eof()){
string line;
getline(datafile,line);
cout<<line<<endl;
}
datafile.close();
}
The error stems from trying to get the name of the file from argv[1]. It was working fine when I just gave it the file name.
The error:
project2.cpp: In function ‘int main(int, char**)’:
project2.cpp:22:2: error: ‘filename’ was not declared in this scope
filename = argv[1];
^
project2.cpp: In function ‘void readFile(std::string)’:
project2.cpp:32:25: error: no matching function for call to ‘std::basic_fstream<char>::open(std::string&)’
datafile.open(filename1);
^
project2.cpp:32:25: note: candidate is:
In file included from project2.cpp:2:0:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/fstream:889:7: note: void std::basic_fstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s,
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/fstream:889:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
I am using Cygwin. I used it last semester as well when I was writing code in C, and my professor had us check certain installation options at the time. Could these installation options be the root of the problem? Or are errors like this common in C++? Thanks.
Just read the error:
project2.cpp: In function ‘int main(int, char**)’: project2.cpp:22:2:
error: ‘filename’ was not declared in this scope filename = argv[1];
^
Here it says that filename is not declared. i.e. You have to declare it or something wrong with the declaration
Looking at the code you have
string filename1;
One assumes you meant
string filename;
Fix this error - then try again
The first error:change filename1 to filename
The second error: you should set a open()functions in the class info.then you can use it
I'm using a Unix shell compiler and need to import a Windows .dat file for input. Unfortunately this means there exists native '\r\n' components for carriage returns in the input file.
I'm hoping to scrub these out with something along the lines of the following:
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
inputFile.open("myFile.dat");
string array[100];
int i = 0;
while(getline(dataIn, str))
{
str.erase(remove(str.begin(), str.end(), '\n'), str.end());
str.erase(remove(str.begin(), str.end(), '\r'), str.end());
array[0] = str;
i++;
}
return 1;
}
However this is providing the following error:
error: cannot convert ‘__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >’ to ‘const char*’ for argument ‘1’ to ‘int remove(const char*)’
for the first erase(), followed by
error: request for member ‘erase’ in ‘temp.std::basic_string<_CharT, _Traits, _Alloc>::c_str [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]()’, which is of non-class type ‘const char*’
for the second.
I've attempted str.c_str().erase but this has resulted in duplicates of the second error. Any suggestions would be greatly appreciated...
Two problems in the code:
You if you want to use the algorithm function remove, you need to add #include <algorithm>.
To ensure the ::remove (which is a function that removes the file named by the char * argument) isn't picked up, use std::remove.
I need to create a bunch of different files and I want to name them something like:
"signal1.dat", "signal2.dat", "signal3.dat", ... , "signal100.dat"
This is what I have so far
std::stringstream ss;
std::string out = "signal";
std::string format = ".dat";
std::string finalName;
int TrkNxtSig = 1;
...
[code]
...
ss << out << TrkNxtSig << format;
finalName = ss.str();
ofstream output(finalName);
TrkNxtSig++;
I get this error
error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)’
/usr/include/c++/4.4/fstream:623: note: candidates are: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/fstream:608: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/iosfwd:84: note: std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)
I'm not sure what that error message is telling me. What is wrong?
It's telling you that there's no constructor for ofstream that takes a std::string as parameter.
Use ofstream output(finalName.c_str()); instead.
This has been fixed in C++11. If you're using gcc, try passing it -std=c++0x.
This question already has an answer here:
No matching function - ifstream open()
(1 answer)
Closed 7 years ago.
void searchString(const string selection,const string filename)
{
ifstream myfile;
string sline;
string sdata;
myfile.open(filename);
while(!myfile.eof())
{
getline(myfile,sline);
sdata = sdata + sline;
}
How do i use string filename as myfile.open(filename)
Initially i was using file.txt instead, but if i use a variable that pass in by the function, like string filename, it give me an error
myfile.open("file.txt");
Error message is as followed:
main.cpp:203:25: error: no matching function for call to ‘std::basic_ifstream<char>::open(const string&)’
main.cpp:203:25: note: candidate is:
/usr/include/c++/4.6/fstream:531:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char, _Traits = std::char_traits<char>, std::ios_base::openmode = std::_Ios_Openmode]
/usr/include/c++/4.6/fstream:531:7: note: no known conversion for argument 1 from ‘const string {aka const std::basic_string<char>}’ to ‘const char*’
make: *** [main.o] Error 1
The constructor for std::ifstream::open (for the particular standard of C++ that you're using) doesn't allow a std::string argument, so you have to use:
myfile.open(filename.c_str());
The constructor expects type const char * which you can obtain from a std::string object using its c_str() member function.