![]() |
Combinding variables...
I'm trying to open 5 files name 1.txt, 2.txt, 3.txt,... and check and see if string "chris" exists in it (the latter isn't scripted yet). I'm stuck on how to open the files.
So I have variable X and it's incremenating until it reaches 5. :
int x = 1; |
:
#include <iostream> |
>file.open(x + ".txt");
That doesn't do what you think it does. You need to convert x to a string object first, then append ".txt" to that string. Finally, because open takes a c-style string, you need to use the c_str member function: :
string filename = string_convert ( x );:
template <typename T> |
You've to make your integer before you can use it with a string. And .open() only take C-string, so you've to use the .c_str().
:
#include <iostream>Edit: Damn, slow again. Very slow! :o |
Is my way incorrect or are those different ways to do it?
|
Quote:
|
Please point out the operator overloading in post #2. Your code would appear to be trying to use the insertion operator as an extraction operator, but I don't see where that's accomplished or why a love for overloading would lead one to do such a thing.
|
When using the ">>" operator with iostreams, it breaks up the white-spaced words in the file. I made a mistake instead of using ">>" I used "<<". So I have it where he could find the word "chris" in each file with those 2 loops.
So this is what I meant to do :
ifstream in("text.txt"); |
I think you also failed to take into consideration the fact that C/C++ is more strongly typed than other languages you might be familiar with. One doesn't concatenate a numeric value to a textual entity. I doubt that's a typo. Regardless, I'd suggest that you might validate your code, if you can find the time. The OP's time, wasted in being mislead, is no doubt less valuable, but still....
|
Quote:
I even learnt something in this thread. :) |
| All times are GMT -5. The time now is 12:42 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC