I heard that Code::Blocks is a good free compiler.
MareoRaft
Junior Poster in Training
79 posts since Oct 2005
Reputation Points: 10
Solved Threads: 4
Here is a function which takes in a string and a filename, and creates a text file with that string in it:
void writeFile ( string text, string filename ){
ofstream myfile;
myfile.open ( filename.c_str() );
myfile << text;
myfile.close();
}
And here is a function which converts doubles and integers into strings (i.e. converts 5 to "5"):
template <typename T>
std::string to_string(T const& value) {
stringstream sstr;
sstr << value;
return sstr.str();
}
And here is how you can use these two functions together to make a bunch of numbered files:
int main(){
int numComputers = 8;
string text = "hello i'm some file text :p";
string name;
for( int fileNum=1; fileNum<=numComputers; ++fileNum ){
name = "comp";
name += to_string( fileNum );
writeFile( text, name );
}
return 0;
}
*note: you will need the following headers:
#include <iostream>
#include <fstream>
#include <sstream>
MareoRaft
Junior Poster in Training
79 posts since Oct 2005
Reputation Points: 10
Solved Threads: 4
Thanks for all your replies. But can anyone think that these are the reply to my question? Am i helpless regarding this?
Yes, absolutely.
Let's recap. You asked a question giving an example:...say an incorrect program..:
for(int i=0;i<=12;i++)
{
ifstream comp("%i.txt");
comp.close;
}
i know the code is incorrect. But the text document to be created as such 1.txt,2.txt,...12.txt.
OK, that tells us what you are trying to do. Answer was:You can use a stringstream for this. Try something like...
[code included]
You said it didn't work. Why? Because:@ravenous Yep, am using turbo c++ 3.0 version. I know it tat its too old to do. But i hav no more way since i feel difficult to use visual studio. Am not more updated. If i get any proper tutorial i do improve.
you know it's too old. Visual Studio is too hard. If you get instruction you will improve. So you were told:I heard that Code::Blocks is a good free compiler.Personally, i'm enjoying using Qt Creator at the moment. It's free, simple and nice to work in. There are versions for Windows, Linux and mac.
There -- two new compilers. Did you try them? Were they too hard? Must be:Any one pls answer my question for creating multiple text files pls...!
Am using turbo c++ version 3.0 in windows xp platform..
If you want to improve, get a new compiler....
Then someone went old-school which will work with your compiler fine:You can always use the C version of ostringstream -> http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
and you completely blew him off. This will work inany compiler, even Turbo 1.0
So, yes, you have gotten help. You just need to use it.
By the way:
Maybe you could post the code that you already have, so that people can help you find the problems with it more easily. If it's more than ~75 lines, then you should try and extract just the part that you think is causing you the problem.
Did you bother to post any code at all? No. Even though we've posted code for you, you can't be bothered to post yours. Looks to us like you want us to write it for you.
And finally, why are you so arrogant to write:I think u r clear.
I think its clear for u.
I think u understood my point.
Yes, we are clear, and we understood. Maybe you should understand.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
But am helpless without codes i said. And am ready to post my codes, but the problem is that its incomplete and i created now so far that i can understand.... I just waited for the code that suits my requirements.
What this really tells us is "I'm not going to post my code, I need you to write the code for me." Read the Rules.
I thought that the members would be totally confused by my dirty coding.
So how do you clean dirty code? By not asking how and letting someone else write it...Since I need to create some text files, I tried my codes. But they only encountered in error.
How do you clean up an error? Post the code and EXPLAIN what it does and also EXPLAIN the error.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
this will create 12 "computer" objects and open 12 .txt files:
ifstream computers[12];
for(int i=1; i<13; i++)
{
string file("comp");
file += itoa(i);
file += ".txt";
computers[i].open(file.c_str());
if(!computers[i].is_open())
{
cout << "\a\nError, file #" << i << " unable to be opened!";
cout << "\nFile may be been moved, renamed, or deleted. \n\n";
cout << "\nPress [Enter] to continue...";
cin.get();
}
file.clear();
}
all you have to do is load each computer object with data.
Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118