C++, function that can take either ifstream or istringstream - c++

I have a function do_something that reads unsigned characters from a stream.
The stream can be created from a file given the file name. Or it can be created from the given string by considering it as data. I would like to reuse the function in both cases.
The code below gives an error in the second case: "error C2664: 'do_something: cannot convert argument 1 from 'std::basic_istringstream' to 'std::basic_istream'".
What is the proper way to do this?
static void do_something(std::basic_istream<unsigned char>& in)
{
in.get();
}
static void string_read(unsigned char* in)
{
std::basic_ifstream<unsigned char> file(std::string("filename"));
do_something(file);
std::basic_istringstream<unsigned char> str(std::basic_string<unsigned char>(in));
do_something(str);
}

Your code is experiencing something called a vexing parse. The line:
std::basic_istringstream<unsigned char> str(std::basic_string<unsigned char>(in));
is interpreted as a function declaration. str here is a function that returns a std::istringstream and takes as its parameter a variable of type std::string called in. So when you pass it into the function there's an obvious type mismatch.
To change it into a variable declaration you can use curly braces:
std::basic_istringstream<unsigned char> str{std::basic_string<unsigned char>(in)};

Related

"Error: expected a '{' introducing a lambda body" when trying to get char from map<char,char> C++

char Substitution::encodeChar(char a, std::map<char, char>&){
return &[a];
}
This is my implementation attempt (based on a pre defined Class header which I may not change for the assignment). In Visual Studio I get the error (see title) over the semicolon?
Trying
&.find(a)
instead gives me "expected an expression" over the period.
I think I spotted somewhere saying something about const char vs. char for this problem, but I can't wrap my head around it. I've used map char char earlier this way, but somehow using it in this context doesn't work.
It looks like you're trying to treat & as a variable name. It's not. Variable names consist only of letters, digits, and underscores.
Actually, in the function parameters, std::map<char, char>& means that the type of the parameter is a "reference to std::map<char, char>". Note that I said "reference to". That's what the & means. It's part of the type and makes the parameter a reference parameter.
So you need to give your parameter a name and then use that name:
char Substitution::encodeChar(char a, std::map<char, char>& my_map){
return my_map[a];
}
We can read the parameter std::map<char, char>& my_map as saying the my_map is a "reference to std::map<char, char>". Then, my_map[a] accessing the key a in that map.

Piping from Istringstream into templates

I have the following questions: I have a map from string to string which is called psMap. I.e. psMap["a"]="20", psMap["b"]="test", psMap["c"]="12.5", psMap["d"]="1" (true) so the map stores string-expressions of various basic-data types.
The following function foo should (given a key), copy the mapped value to a corresponding type variable, i.e;
int aa;
foo("a", aa);
=> aa=20.
Explicitly, I want to have one function for all possible data-types (so no manually cast), so I tried with templates exploiting the automatic conversion of istringsteram, namely
template<class PARAMTYPE>
void foo(string _name, PARAMTYPE& _dataType) {
PARAMTYPE buff;
istringstream(psMap[_name]) >> buff;
_dataType = buff;
}
The problem is, that the ">>" operation gives an error: Error: no match for »operator>>« in »std::basic_stringstream<char>((* ....
What is going wrong here? Does the stringstream not recognize the correct data type and tries to pipe into an abstract type of "template"? How could I make my code work?
Tank you for your effort :)
You've created a temporary std::istream, which means that it
cannot bind to a non-const reference. Some of the >> are
member functions, and they will work, but others are free
functions with the signature:
std::istream& operator>>( std::istream&, TargetType& );
and these will not work (or even compile).
To avoid the problem either Just declare an std::istringstream
and use it, or call a member function on the temporary which
does nothing, but returns a (non-const) reference:
std::istringstream( psMap[name] ).ignore(0) >> buff;
(Personally, I find the separate variable more readable.)
You use reference as the template argument, so if you call
foo("a", aa);
without '& it should be fine (the way you tried the operator>> for pointer was needed). You also need to modify the last template line:
_dataType = buff;
Try this implementation:
template<class R>
R get_value(const std::string& name) {
R result{};
std::istringstream buffer{psMap[name]};
buffer >> result;
return result;
}
client code:
int x = get_value<int>("a");
Also, do not use identifiers starting with an underscore. That is reserved for library implementers.

Write to user specified file in C++

Can I specify what file I want to write into in C++? I want to be able to type in the filename and write into that file. When I try making myfile.open("example.txt") myfile.open(var), I get a big error...
error: no matching function for call to ‘std::basic_ofstream >::open(std::string&)’
/usr/include/c++/4.2.1/fstream:650: note: candidates are: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits]
Can you make any sense of this or explain what I am doing wrong? I have a feeling this is very simple, as this is my first week using C++.
If var is an std::string, try:
myfile.open(var.c_str());
The error tells you exactly what's wrong, although the precision of the template types named doesn't help make that crystal clear. Take a look at the reference for .open(). It takes a const char * for the filename, and another optional mode parameter. What you are passing is not a const char *.
Like the error says, it is trying to match the parameters with a character pointer and std::string is not a character pointer. However std::string::c_str() will return one.
try:
myfile.open(var.c_str());
In short, yes you can specify a file to open and write into many different ways.
If you're using an fstream and want to write plain text out, this is one way:
#include <string>
#include <fstream>
int main()
{
std::string filename = "myfile.txt";
std::fstream outfile;
outfile.open( filename.c_str(), std::ios::out );
outfile << "writing text out.\n";
outfile.close();
return 0;
}
Is var a std::string? If so, you should be passing var.c_str() as there is not a variant of .open() that takes a std::string.
Is your variable a string, char[], or char*? I think the open() method wants a c-style string, which would be char[] or char*, so you'd need to call the .c_str() method on your string when you pass it in:
myfile.open(var.c_str());
There is a second parameter to the open call. it should be like myfile.open("example.txt", fstream::out)
The error message is quite clear. It says: the basic_ofstream class (your file object) does not have a member function that's called "open" and takes a single argument of type string (your var). You need to go from string to const char * - for that, you use var.c_str().

Variables not equivalent fstream vs. declaration

Basically I'm reading the contents of a file using fstream then converting it to const char* type. I'm supplying this to Lua, and Lua will do something with this. This however does not work. What does work is if I do:
const char* data = "print('Hello world')";
luaL_pushstring(L, data);
luaL_setglobal(L, "z");
They both are in the type const char* type and they are both the same string (e.g. I compared the two lengths). Except one works, and the other. I'm baffled. Any help here? Here is the code:
std::string line,text;
std::ifstream in("test.txt");
while(std::getline(in, line))
{
text += line;
}
const char* data = text.c_str();
luaL_pushstring(L, data);
luaL_setglobal(L, "z");
Here is the Lua code:
loadstring(z)()
To diagnose this, you probably want to know more about what Lua thought. I'd write the Lua side as assert(loadstring(s))() instead. If loadstring fails, your current code at best prints an error from the attempt to call nil. With the assert() in the sequence, the call to nil will be replaced by a more informative error about what went wrong.
Don't you have to set the global before you push the value? Anyways, what's up, Camoy :P

Pass path to file / filename as argument to a function that prints the file to screen in C++

As the title says, is there any way to pass the path to the file / filename to open as an argument in the function?
I've written a short code for printing a .txt-file to the screen in C++, but instead of having all the code in the main(), I'd rather have it as an own function that I can call with the filename of the file to open as the only input argument.
Basically the beginning of the function would look like
void printFileToScreen()
{
ifstream fin;
char c;
fin.open("FILE_TO_OPEN.txt", ios::in);
blablabla
}
Now is there any way to pass "FILE_TO_OPEN.txt" when I call the function?
I've tried
void printFileToScreen(string str)
{
ifstream fin;
char c;
fin.open(str, ios::in);
blablabla
}
where I call the function like printFileToScreen("FILENAME.txt"), but with no luck, so I'm not sure how to do this.
Hope anyone can help :)
Unfortunately, the iostream functions deal with const char* types rather than with std::string (the iostream functions were developed independently of the STL). You instead could use std::string::c_str() to obtain a const char*:
fin.open(str.c_str(), ios::in);
As a general design rule, I would not pass the file name to the called function. I would pass the already opened std::istream object to read from. This allows you to do the job of printing in a function, and to do the job of opening the file and dealing with non-existent files in another. This has the bonus of being able to pass std::cin to your function!
Try changing your function to look like this :
void printFileToScreen(const string &str);
//If you pass a const char*, a string will be constructed
or this :
void printFileToScreen(const char *);
The function you wrote expects an instance of std::string to be passed by value.
Never mind, after some more trying and failing I found out that I needed to pass a char pointer, and not a string. :)
Of course you can pass the filename as a function parameter. If in doubt, pass a "const char*" rather than a string. I should work.