string nach const char*
-
hab mal wieder ein problem
hab folgende funktion
for (std::map<std::string, std::string>::const_iterator i = r->params_.begin(); i != r->params_.end(); i++) { body += "<br>" + i->first + " = " + i->second; forumindex = 1; if(i->first == "catID") {body += ForumCatIndex(body, i->second);} }die soll der funktion ForumCatIndex die CatID übergeben (eine zweistellige zahl)
hier die funktion(bzw einen ausschnitt davon )
std::string ForumCatIndex(std::string body, std::string catID) { char * dat_ein; char * token; char sLine[256]; char Trenner[] = "|@|"; dat_ein = "forum/cat01.txt"; ifstream Datei(dat_ein);und dort wo jetzt cat01.txt steht soll die zahl eben soll eben die zahl von der anderen funktion hin
das klappt bei mir aber nicht egal was ich versucht habe :
habe erst der funktion versucht nicht string sondern int zu übergeben klappte nicht
dann mit c-str() klappt auch nicht mit sprintf klappte nicht da char nicht zu const char konvertieren geht und die funktion ifstream geht anscheinend nur mit const char
-
Hi!
Du solltest dein Problem mal genauer schildern und mehr Quellcode posten. So kann ich nichts entdecken.
-
mehr hab ich leider nicht was damit zu tun hat
also in der map steht folgendes catID und als value z.b. 01
dies übergebe ich der funtkion hiermit
ForumCatIndex(body, i->second)
in der funktion selbst will ich die zahl hier als besipiel 01
hier reinhaben
dat_ein = "forum/cat01.txt";
so nach dem motto
dat_ein = "forum/cat" + catID + ".txt";
das klappt aber nicht und wie ich geschrieben habe hab ich schon ein paar sachen probiert aber fehlanzeige
-
std::string ForumCatIndex(std::string body, std::string catID){ std::string dat_ein; dat_ein = "forum/cat" + catID + ".txt"; return dat_ein; } int main() { ifstream Datei(ForumCatIndex( "", "01").c_str()); return 0; }Funktioniert einwandfrei.
Skippy schrieb:
das klappt aber nicht und wie ich geschrieben habe hab ich schon ein paar sachen probiert aber fehlanzeige
Hast du
#include <string>in deinem Programm? VC6 macht da ein paar wirklich lustige Fehlermeldungen wenn du <fstream> eingebunden hast und <string> fehlt.
Kurt
-
nutze dev c++
und am include liegt es nicht ist alles drinwenn ich da in der funktion so abändere
char * dat_ein; dat_ein = "forum/cat" + catID + ".txt"; ifstream Datei(dat_ein);dann kommt folgende fehlermeldung
cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `char*' in assignment
wenn ich das so abändere
std::string dat_ein; dat_ein = "forum/cat" + catID + ".txt"; ifstream Datei(dat_ein);dann folgende
no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)'
aus spaß mal die ganze funktion wie sie bis jetzt ist
std::string ForumCatIndex(std::string body, std::string catID) { std::string dat_ein; char * token; char sLine[256]; char Trenner[] = "|@|"; dat_ein = "forum/cat" + catID + ".txt"; ifstream Datei(dat_ein); body +="<center>"; body +="<table border='0' width='760' cellpadding='0' cellspacing='0'>"; body +="<tr>"; body +=" <td class='logo' id='head' align='left'>"; body +=" <h1><a href='http://skippy.dyndns.ws' title='Skippy - sagt: das macht spaß'>SKIPPSerV - Test Forum<span></span></a></h1>"; body +=" </td>"; body +="</tr>"; body +="<tr>"; body +="<td align='left'>"; body +="<div class='BigPhorumForumTitle'><strong>SKIPPSerV Beta version Forum</strong></div>"; body +="<br />"; body +="<table width='100%' cellspacing='0' cellpadding='3' border='0'>"; body +="<tr>"; body +=" <td width='60%' nowrap='nowrap' bgcolor='#f2f2f2'><FONT color='#267e9b' class='PhorumNav'> <a href='index.php?f=0'><FONT color='#267e9b' class='PhorumNav'>Forum List</font></a> | <a href='post.php?f=6'><FONT color='#267e9b' class='PhorumNav'>New Topic</font></a> | <a href='search.php?f=6'><FONT color='#267e9b' class='PhorumNav'>Search</font></a> | <a href='login.php?f=6'><FONT color='#267e9b' class='PhorumNav'>Log In</font></a> </font></td>"; body +=" <td align='right' width='40%' bgcolor='#f2f2f2'><div class=nav><FONT color='#267e9b' class='PhorumNav'> <a href='list.php?f=6&t=1117391812&a=2'><FONT color='#267e9b' class='PhorumNav'>Older Messages</font></a> </font></div></td>"; body +="</tr>"; body +="</table>"; body +="<table class='PhorumListTable' width='100%' cellspacing='0' cellpadding='0' border='0'>"; body +="<tr>"; body +=" <td class='PhorumListHeader' bgcolor='#31a2c6'><FONT color='#FFFFFF'> Topics<img src='images/trans.gif' border=0 width=1 height=24 align='absmiddle'></font></td>"; body +=" <td class='PhorumListHeader' bgcolor='#31a2c6' width='150' nowrap='nowrap'><FONT color='#FFFFFF'>Author </font></td>"; body +=" <td class='PhorumListHeader' align='center' bgcolor='#31a2c6' width='80' nowrap='nowrap'><FONT color='#FFFFFF'>Replies </font></td>"; body +=" <td class='PhorumListHeader' bgcolor='#31a2c6' width='120' nowrap='nowrap'><FONT color='#FFFFFF'>Last Post</font></td>"; body +="</tr>"; if ( !Datei ) { cout << "Kann Datei ( " << dat_ein << " ) nicht öffnen" << endl; } while(!Datei.eof()) { Datei.getline(sLine, 256); cout << "Zeile :" << sLine << endl; // Zeile auseinandernehmen. token = strtok(sLine, Trenner); body +="<tr>"; body +=" <td class='PhorumListRow' bgcolor='#FFFFFF'><FONT color='#303030'> <img src='images/trans.gif' border=0 width=1 height=21 align='absmiddle'> <a href='/Forum/?catID="; body += token;//catID der url hinzufügen body += "'>"; token = strtok(NULL, Trenner); body += token;//Titel der Kategorie body +="</a> </font><font class='PhorumNewFlag'>"; token = strtok(NULL, Trenner); body += token;//erweiterte Nachricht z.b. Wichtig, Neu, usw body +="</font></td>"; body +=" <td class='PhorumListRow' width='150' bgcolor='#FFFFFF' nowrap='nowrap'><FONT color='#303030'><strong>"; token = strtok(NULL, Trenner); body += token;//Author body += "</strong> </font></td>"; body +=" <td class='PhorumListRow' align='center' width='80' bgcolor='#FFFFFF' nowrap='nowrap'><FONT color='#303030' size=-1>not aviable </font></td>"; body +=" <td class='PhorumListRow' width='120' bgcolor='#FFFFFF' nowrap='nowrap'><FONT color='#303030' size=-1>"; token = strtok(NULL, Trenner); body += token;//Datum body +=" </font></td>"; body +="</tr>"; token = strtok(NULL, Trenner); while( token != NULL )//entfernt überflüssige token um fehler zu vermeiden { cout << "Token = " << token << endl;// für debug zwecke token = strtok(NULL, Trenner); } } return body; }
-
std::string dat_ein; dat_ein = "forum/cat" + catID + ".txt"; ifstream Datei(dat_ein.c_str());Kurt
-
std::string dat_ein; dat_ein = "forum/cat" + catID + ".txt"; ifstream Datei(dat_ein);std::string dat_ein; dat_ein = "forum/cat" + catID + ".txt"; ifstream Datei(dat_ein.c_str());ifstream will ein char*
-
@YASC danke und ich überlege die ganze zeit warum das net klappt so war ja mein erster versuch auch dies mit nen string zu machen bin aber nicht darauf gekommen dies wieder zurückzuwandeln