void FileIO :: openFile(const char* m_FileName,const char* m_FileMode);
I am getting error:
FileIO.cpp: In static member function ‘static void FileIO::openFile(const char*, const char*)’:
FileIO.cpp:12:45: error: no matching function for call to ‘std::basic_ifstream<char>::open(const char*&, const char*&)’
FileIO.cpp:12:45: note: candidate is:
In file included from FileIO.h:1:0:
/usr/include/c++/4.7/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.7/fstream:531:7: note: no known conversion for argument 2 from ‘const char*’ to ‘std::ios_base::openmode {aka std::_Ios_Openmode}’
std::basic_ofstream::open doesn't take two const char*s. (note: your subject says ofstream but from your comments it appears you're talking about ifstream).
http://en.cppreference.com/w/cpp/io/basic_ifstream/open
void open( const char *filename,
ios_base::openmode mode = ios_base::in );
void open( const std::string &filename,
ios_base::openmode mode = ios_base::in ); (since C++11)
The problem is the second, not the first argument.
ifstream ifs;
ifs.open("hello", "rb" /*<-- problem, this is a const char* not flags.*/);
Instead, you need to pass it std::ios_base flags
ifstream ifs("hello", std::ios_base::in | std::ios_base::binary);
or
ifstream ifs;
ifs.open("hello", std::ios_base::in | std::ios_base::binary);
--- EDIT ---
Looking at your comments following the post (why didn't you edit the post?) you are also trying to check for 'NULL'.
In C and C++ 'NULL' is a macro which is #defined as 0. So, checking for NULL can check for a null pointer, but it also can test for numeric values. If you want to check if the file opened, you will need to do:
m_FileInput.open("hello", std::ios_base::in | std::ios_base::binary);
if (!m_FileInput.good()) // checks if the file opened.
And you should try to use 'nullptr' instead of 'NULL' when possible.
You're trying to use C's FILE* syntax to call a C++ open function. The mode (read/write/append) arguments are NOT string literals in C++ but enumerated values possibly ORed together.
Related
The below program tries to open a rom file and loads it to std::array.
#include <array>
#include <fstream>
#include <iostream>
const std::string ROM_FILE = "cpu_instrs.gb";
int main()
{
std::array<uint8_t, 0x8000> m_Cartridge;
std::ifstream istream(ROM_FILE, std::ios::in | std::ios::binary);
istream.seekg(0, std::ios::end);
size_t length = istream.tellg();
istream.seekg(0, std::ios::beg);
if (length > m_Cartridge.size())
{
length = m_Cartridge.size();
}
istream.read(m_Cartridge.data(), length);
for (const uint8_t& b : m_Cartridge)
{
std::cout << b << std::endl;
}
return 0;
}
When I ran the program above using g++, I got the following error
test.cpp: In function 'int main()':
test.cpp:21:34: error: invalid conversion from 'std::array<unsigned char, 32768>::pointer' {aka 'unsigned char*'} to 'std::basic_istream<char>::char_type*' {aka 'char*'} [-fpermissive]
21 | istream.read(m_Cartridge.data(), length);
| ~~~~~~~~~~~~~~~~^~
| |
| std::array<unsigned char, 32768>::pointer {aka unsigned char*}
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/fstream:38,
from test.cpp:2:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/istream:486:23: note: initializing argument 1 of 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::read(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::char_type = char; std::streamsize = long long int]'
486 | read(char_type* __s, streamsize __n);
| ~~~~~~~~~~~^~~
It seems to me that std::istream only works with char not unsigned char. When I change type from uint8_t to char. The program compiles and ran without problems. Is there any way to make std::istream work with uint8_t?
std::istream is written in terms of char_type being simply char, and similarly, std::istream::read is the same.
The conventional approach would be to reinterpret_cast<char*> the pointer that you are reading to:
istream.read(reinterpret_cast<char*>(m_Cartridge.data()), length);
Although using reinterpret_cast is often frowned upon in many cases, this is one such case where it's both necessary and also safe to perform, since pointers to char are capable of aliasing any object with the same reachability as the parent object (in this case, the same reachability as the entire array object).
In fact, if you check the std::istream::read page on cppreference, even it uses a similar example to read the raw binary:
std::uint32_t n;
if(raw.read(reinterpret_cast<char*>(&n), sizeof n)) { ... }
That said, there is technically an alternative way you can do this -- though it's not recommended. The various stream classes are templated on the character type (basic_*stream<...>), of which std::ifstream is an alias of. Technically you could try to instantiate std::basic_ifstream<unsigned char> and read from this without a cast. However, in doing so, you may need to implement a custom std::char_traits -- as this is not required by the C++ standard to work with signed or unsigned character types (e.g. std::char_traits<unsigned char> is not guaranteed to be valid)
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
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.
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.