I want to create a file with C++ and write something in it. I have two classes, one with a Vererbung::writer(string name) and another subclass called Vererbung1(int zahl). Without the integer it works peferctly but when I want to write the integer to string and paste it after the function it wont work.
this works normally
Vererbung.cpp
void Vererbung::Writer(string name)
{
ofstream text;
text.open ("test.txt", ios::trunc);
text <<"write something\n";
text <<"again2 \n";
text <<"again 3\n";
text << name;
text.close();
}
Vererbung1.cpp
include "Vererbung.h"
void Vererbung1::Writer(int zahl)
{
std::ostringstream ostr;
ostr<< zahl;
name = "\n" "Test\n""Test\n""Test\n" ostr.str();
Vererbung::Writer(name);
}
When I run it in main it says that I need a ';' before ostr.str(); how can I fix this, If I want a integer value to string in a file in it?
This problem is very unclearly stated but, if we assume that name is an std::string, then:
name = "\n" "Test\n""Test\n""Test\n" ostr.str();
This is just a bunch of string literals written next to each other, followed by an std::string expression also just randomly floating there in free space.
Your computer doesn't know what you want it to do.
Although you actually can concatenate string literals in this way (literals are something like "hello world", but not something like aStringVariable), that doesn't apply to arbitrary expressions (and you don't even need it where you've used it).
I think that what you meant was this:
name = "\nTest\nTest\nTest\n" + ostr.str();
I hope that your C++ book teaches this; if not, get a better one.
Related
I am extracting a string from a .txt file and saving it in a variable:
std::string line = "The king's name is getKingName()";
Lets assume that getKingName() is a function that returns a King class' name data member.
How can I make a call to getKingName() when the string variable looks like that?
As far as I know, C++ does not provide such kind of functionality to interpolate functions call inside a string. All you can do implement your own logic to do that.
Like,
1) define all the valid methods like this,
string getKingName(){
return "Some name";
}
string otherMethods(){
return "other values";
}
2) One helper method for mapping of function call
string whomToCall(string methodName){
switch(methodName){
case "getKingName()":
return getKingName();
break;
case "otherMethods()":
return otherMethods();
break;
default:
return "No such method exist";
}
}
3) break the line in tokens(words), read one by one and check for following condition also if
token starts with any alphabetical character and ends with "()" substring
istringstream ss(line);
do {
string token;
ss >> token;
if(isMethod(token))
cout << whomToCall(token) << " ";
else
cout << token<< " ";
} while (ss);
4) isMethod() to check if token's value can be a valid method name
bool isMethod(string token){
int n= token.length();
return isalpha(token[0]) && token[n-2]=='(' && token[n-1] == ')' ;
}
This would be the easiest solution, but I think your problem consists of several such calls?
std::string line = "The king's name is getKingName()";
if (line.find("getKingName()") != std::string::npos) {
King name = getKingName();
}
Amended
This answer is a little off subject. I will leave it up, because others might find it relevant, but I agree with other answers, a simple map->function will work better for your case.
This is not supported by C++. C++ is not an interpreted language. If you you want to do things like this, why not use an interpreted language, which do these sorts of things by default. Languages like lua are designed to call C/C++ functions with an interpreted language, with a small overhead.
However, if you really need to do this, it is possible, depending on your operating system. For example,
On windows start with dbghelp. You will need to build a pdb, (e.g. build with symbols).
On linux, you will also need to build symbols (-g), and use something like dlsym see here for a discussion.
That said, there are lots of gotchas doing it this way. Optimization can get in the way (best to disable them). Also best to avoid dynamic linking (prefer static). You will also need to cope with C++ name mangling (the name of the function is not the name of your function in C++). see https://blog.oakbits.com/how-to-mangle-and-demangle-a-c-method-name.html.
Can I manually type in the file name inside of the terminal?
For example, the program will ask me which file I want to open up. I'll manually type in "test.txt" and it'll open up test.txt. However, my compiler is issuing the following error:
no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::string&)'
My code:
int main() {
string input;
cout << "Please enter the name of the file\n";
cout << "Input: ";
getline (cin, input);
ifstream file (input);
if (file.is_open()) {
// blah
} else {
// blah
}
}
How can I manually type in the text file name?
If you're using an older (pre-C++11) compiler, you'll have to use:
ifstream file (input.c_str());
Although the file name was a string-like thing, and C++98 had std::string, they didn't put two and two together (so to speak), so in C++98 the string-like thing to specify the name of a file you wanted to open had to be specified as a char const * (i.e., a C-style string) rather than an std::string.
I predict it's purely a matter of time before supporting only char const * and std::string as the name looks (at least close to) equally silly--once we're accustomed to using ranges, it'll be obvious that it should really just be some range of character-like things.
Ok thanks for the answer Wug! I changed my code but now it's complaining about:
no matching function for call to
std::basic_ofstream::basic_ofstream(std::basic_string)
I'm not sure it makes any difference but i'll just post all of my code it's not that much so far.
I'll try to keep it cleaner from now on.
#include <iostream>
#include <windows.h>
#include <direct.h>
#include <fstream>
using namespace std;
int main()
{ /*Introduction*/
SetConsoleTitle("Journal");
string action, prom0, filename, filepath;
filepath = "C:\\Users\\-\\Desktop\\Projects\\Journal Project\\Logs\\";
cout << "Hi and welcome to Journal! \nHere you can write down your day.\nWrite help for";
cout << "more \nType command to start: ";
/*Choose Action*/
cin >> action;
if (action == "new")
{system("cls");
/*Make new Journal file*/
cout << "Filename: ";
getline(cin, filename);
mkdir("C:\\Users\\-\\Desktop\\Projects\\Journal Project\\Logs");
ofstream journallogs(filepath + filename);
journallogs.close();
}
else {
cout << "Wrong command\n";
};
return 0;}
There are 2 things wrong. The first is what the compiler's complaining about:
ofstream journallogs("C:\\Users\\-\\Desktop\\Projects\\Journal Project\\Logs\\" + getline(cin, filename), ios::out);
std::getline(istream&, string&) returns istream&, and you can't add char * to istream. I recommend taking a look at the documentation for getline(), which might help you understand better how you're supposed to use it. Here's an example anyway:
string filepath = "C:\\Users\\-\\Desktop\\Projects\\Journal Project\\Logs\\";
string filename;
getline(cin, filename);
ofstream journallogs(filepath + filename);
The second problem is that you're reading from cin into filename before calling getline(). When you call getline(), any contents of filename are dropped, so you'll effectively trim the first word off of your filename, which probably isn't what you want. To fix that, remove the extraneous cin >> filename;
Note: indentation is important and helps you read your own code. Put forth the effort to keep your code looking nice.
First, learn this:
Start small and simple.
Add complexity a little at a time.
Test at every step.
develop new functionality in isolation.
Never add to code that doesn't work.
For the rest, I don't use Windows, so I can't be certain my code will work there, but the approach will.
You are trying to 1) get a filename from the user, 2) modify it and then 3) use it to open a file; we will develop these three things in isolation.
Getting a filename from the user. Civilized filenames do not contain whitespace, so they can be read with cin, but if you want to allow whitespace you can use getline instead. Either way, test it.
Modifying the filename. Write code that assigns a value to the filename, just as it does to the path-- do not get the filename from the user, it slows down your testing and is not proper isolation. Now try to append them. If you try filepath + filename, you may get a compiler error. Here's where you must understand the difference between std::string and char[]. A char[] is an array of char, and it (usually) contains a null-terminated sequence of characters; you must read up on arrays and pointers. It is a primitive type, and you cannot simply concatenate two of them with '+', you must use something like strcat, which is dangerous if you haven't done your homework on arrays. On the other hand, std::string is more sophisticated, and can handle '+' and many other operations. If you have a std::string x and you decide you want a char[] after all, you can get one like so: x.c_str().
Opening the file. If I remember right, the ofstream constructor can take a char[], but not a std::string. Test this with a hard-coded string (isolation!).
Once you have these three components working independently, you can hook them together.
I have a problem. I need to convert string type to unicode.
I know metod like
string.c_str();
but it doesn't work in my code.
I have function
void modify(string infstring, string* poststring)
and in it i need to display infstring in memo. Like a
Form1->Memo1->Lines->Add("some text "+infstring.c_str()+" some text");
but compiler says me "E2085 Invalid Pointer addition"
How can i solve my problem?
Form1->Memo1->Lines->Add("some text "+infstring.c_str()+" some text");
should be
Form1->Memo1->Lines->Add(("some text "+infstring+" some text").c_str());
i.e. you add the string literals to the std::string then use c_str() to get a const char* from it.
That still won't work if the Add() function takes a different type, but you haven't given enough information to know what you're asking about.
use a stringstream
#include <sstream>
std::stringstream ss;
ss << "some text" << mystring << "some text";
Form1->Memo1->Lines->Add(ss.str().c_str());
Hello for all people...
Sorry for my english, but speak spanish...
In this week, study and work for this proyect, I want create a software to make files(.us)...
Example
char name[50]; //Or string
cin>>name;
ofstream PlayerPawn("D:\\UDK\\UDK_XXX\\Development\\Src\\" + name+"\\Classes\\_PlayerPawn.us");
But the compiler has error in the Operator binary plus
Any alternative, examples or something for create the file in specific directory
Good bye and Thx!
Either side of operator+ must be a std::string1 for operator+ to concatenate strings:
string name;
cin >> name;
ofstream PlayerPawn("D:\\UDK\\UDK_XXX\\Development\\Src\\" + name + "\\Classes\\_PlayerPawn.us");
And use std::string for this stuff; with std::string there's no danger of buffer overflows that you get with char*.
1 Actually it just needs to be a class type that supports operator+, not specifically std::string, but then you have no idea what it will do.
I believe you want name to be a std::string - otherwise, name + [suffix] will try to add the suffix string to the array and will not compile. If you really want to keep the name as an array, you should use strcat to append the strings together.