how to insert int value to string object?
Aug 30, 2010 at 6:49am UTC
when I use getline(ifs,str).
I need to add a int value (int line;
)to every line read from ifs object(ifstream ifs("intput.txt" );
).
how can I Assignment "line"into "str".and put it in the front of "str";
Aug 30, 2010 at 11:55am UTC
1 2 3 4 5 6 7 8 9 10 11 12
#include <sstream>
#include <string>
int main(){
std::stringstream s;
std::string line = "line" ;
int l = 5;
s << l << line;
std::string result = s.str();
return 0;
}
Aug 31, 2010 at 1:24am UTC
I think you mistake me.
I want to copy a text file.in the new ffile. I want to show line numeber in the front of each line.
for example:
......
23 string......
24 string .........
25 string ...
.....
line is a integer not a string.
1 2 3 4 5 6 7 8 9
main{
........
ifstream ifs("intput.txt" );
int line=1;
string str;
while (getline(ifs,str))
{line++;
//how to add line to str
........
Last edited on Aug 31, 2010 at 1:26am UTC
Aug 31, 2010 at 7:54am UTC
I don't see how am I wrong. Maybe if I put this into function, it will be more clear.
1 2 3 4 5
string append(int i, string str){
stringstream s;
s << i << " " << str;
return s.str();
}
Then in main
1 2 3 4
while (getline(ifs,str)){//str == "hello world"
line++;//line == 1
str = append(line, str);//str == "1 hello world"
}
Topic archived. No new replies allowed.