I want to read wstring from a txt file.
I store the file's name in a variable called SourceFileName and it is wstring varaible. In the txt file, it has the following strings:
112:abc
221:ghj
....
Now I want to read these numbers and its corresponding strings from the txt file into wstring strings, but when I was trying to do that, VS notifies me that no instance of overloaded function for std::getline macthes argument list.
std::wifstream map;
map.open(SourceFileName.c_str());
std::wstring fileID;
std::wstring fileName;
std::getline(map, fileID, L":");//error happens here.
How to fix it?
I also tried std::getline(map, fileID, ":");, doesn't work too.
There is no overload of std::getline that would accept a string literal as the third argument. Both overloads that have it require the delimiter to be a character:
std::getline(map, fileID, L':');
Hint: When the compiler does not find a matching overload, it helps to take a look at what type of arguments the overloads do accept.
Related
I have a stack defined and it accepts only strings.
Now my goal is to read each character, if it is operator like +, I need to push it to the stack.
when i push it, it saves the ACSII calue of + which is 43
how do i achieve to save char in to string?
i tried to use STL
stack<string> s1;
string post;
if (exp[0] == '+')
{
s1.push(to_string(exp[0]));
}
cout<<s1.top();
i need to save +, eventually i am writing code to convert infox to postfix expression. My input will be numbered with operators
The to_string function only has overloads for converting numerical values to strings, which is why you're getting the string "43". You could just change your code to this:
s1.push("+");
in which string's from c-string constructor is used to implicitly convert from the const char* to a string. Alternately, to convert from a single char to a string, the easiest way is to use the fill constructor, in which case you would write this:
s1.push(string(1, exp[0]));
I'm trying to append my path and contain a variable as part of the path but I'm getting an error.
What's wrong with it?
fstream fin("E:\\Games\\maps\\" + this->MapNumber + ".map", ios::in|ios::binary|ios::ate);
this->MapNumber is a USHORT
error: 13 IntelliSense: expression must have integral or unscoped enum type
In C++ you can't use + to concatenate literal strings. You can use + with std::strings to concatenate them, but that won't work with integer or other types. You need to use a stream instead. Insertion and extraction into a stream will cause the types that support it to represent themselves as text, but you probably already knew this from general I/O.
Try with something like this:
std::stringstream filename;
filename << "E:\\Games\\maps\\" << this->MapNumber << ".map";
std::fstream fin(filename.str().c_str(), ios::in|ios::binary|ios::ate);
Just like with everything else, to use something you need to include the header that declares it first. In order to use std::stringstream you need to include <sstream>.
You can't use operator+ on a string and another type like string or so you can either:
Option1: turn all variables into strings to append them
string s = string("E:\\Games\\maps\\") + string(itoa(this->MapNumber)) + string(".map");
option2: use stringstream as #k-ballo explained
option3: use the good old C fprintf (my personal favourite)
char str[100];
fprintf(str, "E:\\Games\\maps\\ %d .map", this->MapNumber);
I have a problem. I load the whole file and then getline through it to get some info. However in the map format there may be 0 or 20 "lines" with the info. I need to know how to getline through std::string. There is a function (source stream, destination string, decimal) but I need (source string, destination string, decimal). Searching in streams isn't possible in C++ (only using many temp string and extracting and inserting many times, it's unclear and I don't want to do it that messy way). So I want to know how to getline from a std::string.
Thans
You seem to want std::istringstream, which is in the header <sstream>:
std::string some_string = "...";
std::istringstream iss(some_string);
std::string line;
while (std::getline(iss, line))
{
// Do something with `line`
}
I am using the following method to read a txt file
modelStream.open("file.txt", ios::in);
if (modelStream.fail())
exit(1);
model = new Model(modelStream);
but i want to know how i can pass in a string as a parameter
string STRING;
modelStream.open(STRING, ios::in);
if (modelStream.fail())
exit(1);
model = new Model(modelStream);
does anyone know if this is possible and if it is how would I do it?
For legacy reasons, iostreams in C++03 expects a C-style, null-terminated string as argument and doesn't understand std::string. Fortunately, std::string can produce a C-style, null-terminated string, with the function std::string::c_str():
modelStream.open(STRING.c_str(), ios::in);
This was actually "fixed" in C++11, so if you were using it your original code would be functional.
Also, an all-caps variable name is not recommended; neither is a variable called "string". Make the name describe the meaning.
Simply use c_str () method of std::string
modelStream.open(STRING.c_str (), ios::in);
the standard streams doesn't accept a standard string, only c-string! So pass the string using c_str():
modelStream.open(STRING.c_str(), ios::in);
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.