I think this community member will help me to clear my problem.

Am not a c++ professional just a beginner. Am on the way to create a software for a browsing center. Am almost struck with the problem of creating multiple text files.

To be more clear, Say, a browsing center has 12 computers, so I need to create 12 text files with name comp1, comp2, comp3, ... comp12. and then open them in need and change the data.

I tried google.. but in vain. Many example illustrates but din't shoot my requirements. I think u r clear. I need to create n no of text files in c++ ( May be using for loop) then access the required whenever needed. Thanks for the guys who solve me with coding. Am using turbo c++ in windows environment.

Member Avatar for yoni0505

Here's a tutorial about input and output to files:
http://www.cplusplus.com/doc/tutorial/files/

"Non-binary files are known as text files", it means you should NOT open the files in binary mode, so it will consider it a text file.

Now for your 12 computers problem, I would suggest using some sort of configuration option.
You can do with with a config text file, enter the name of the computer in it (comp1,comp2...), and read with with the file i/o library that I mentioned. Then you can know with which computer you are working (after one time configuration).

These are pretty much the guidelines that I would use for solving your problem ;)

hi yoni, Thanks for ur reply, But my problem is not creating a file. Am well known with the concept of creating a file at a time. But my doubt here is that creating 12 files with auto generated name. 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. I think its clear for u. Solvers.. Thanks for u in advance!

You can use a stringstream for this. Try something like

#include <iostream>
#include <string>
#include <sstream>

int main()
{
   for ( int i = 0; i < 10; ++i ){
      std::string filenameRoot("file_");
      std::stringstream ss;
      ss << filenameRoot << i << ".txt";
      std::cout << ss.str() << std::endl;
   }
   return 0;
}
commented: Still i need t to be clear +0

Hi revenous, Thanks for the code. But it seems that i get
error: unable to open file 'sstream'.
I tried to add .h. but no use :(

another error: 'std' must be a struct or class

moreover I couldn't get to the point. Anyone pls xplain me..! :'( ..Am in hurry to create it... :(

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.

From your error, it sounds like you might be using a really old compiler. Which compiler are you using?

@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. Thanks for ur information. I think u understood my point.

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.

Any one pls answer my question for creating multiple text files pls...!

Am using turbo c++ version 3.0 in windows xp platform..

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>

Er, I thought we'd suggested:

  1. Get a newer compiler that complies with the current C++ standards
  2. Use stringstream to make numbered file names as required

ravenous is right.
sstream is just good for that Job...
Or you can make a custom container class that handles creation, and loading of any File contents... 1 to 10.
There are several ways to implement the class... you could create different files by appending file Numbers(string or char) to the filenames...
load a file by a simple int value passed to the class
Just think it big... If you don't understand what I'm saying.... try learning Data Structures, Containers, and Algorithms search google.
Believe me, it is more easier when you have an advanced knowledge if you want to handle your programs tightly.
Depending on your scope, just learn C++ STL (sstream inclusive) and you are up for your Program

oops... sorry for the bad and un-punctuated sentences.... :-)

Thanks for all your replies. But can anyone think that these are the reply to my question? Am i helpless regarding this?

@MareoRaft will I get result for this in v 3.0 in turbo c++? Because it couters me error.. Thanks for ur kind neat explanation. 5n for me. Hope its near to my question. Pls reply for this soon.. thanks

commented: You must have missed all of the help above this post. -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...

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 in any 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.

@moderator WaltP :

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:

Am not arrogant...:'( 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 thought that the members would be totally confused by my dirty coding. So only i asked about the point where am struck with. In addition, Since am new to programming environment, I feel it hard 4 me to get inside t. Sorry if I have spoiled the Community rules. I just waited for the code that suits my requirements. And in the middle I visited the community with my mobile and just i searched for the code. I din't read the texts. So the sole problem is on me. Pls do forgive me.

Now am straight forward, (Not touching the nose with my hand around my neck):

Since I need to create some text files, I tried my codes. But they only encountered in error. So i asked the help of the Daniweb. Now I left with the codes other than creating multiple files. I will certainly download the new compiler as suggested by revanous "Qt Creator".

Once again sorry. :(

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.

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 Thanks, I got the ans with slight error. Modified t.. :)

......

commented: Why did you even bother to replay in this thread? -1

i just was try the site :) it is my first so am sorry if that bother you and i saw that there is an answer for the qustion so my replay didnt hurm any body
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.