954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Create, edit, modify multiple text files using c++

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.

Riteman
Light Poster
32 posts since Jun 2011
Reputation Points: 6
Solved Threads: 0
 

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 ;)

yoni0505
Light Poster
28 posts since Jan 2010
Reputation Points: 10
Solved Threads: 2
 

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!

Riteman
Light Poster
32 posts since Jun 2011
Reputation Points: 6
Solved Threads: 0
 

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;
}
ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

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... :(

Riteman
Light Poster
32 posts since Jun 2011
Reputation Points: 6
Solved Threads: 0
 

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
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

@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.

Riteman
Light Poster
32 posts since Jun 2011
Reputation Points: 6
Solved Threads: 0
 

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
 

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.

ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

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

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

Riteman
Light Poster
32 posts since Jun 2011
Reputation Points: 6
Solved Threads: 0
 

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
 

Er, I thought we'd suggested:Get a newer compiler that complies with the current C++ standards
Use stringstream to make numbered file names as required

ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

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

WhiZTiM
Newbie Poster
18 posts since Dec 2010
Reputation Points: 7
Solved Threads: 3
 

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

WhiZTiM
Newbie Poster
18 posts since Dec 2010
Reputation Points: 7
Solved Threads: 3
 

You can always use the C version of ostringstream -> http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

m4ster_r0shi
Posting Whiz in Training
229 posts since Mar 2010
Reputation Points: 154
Solved Threads: 31
 

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

Riteman
Light Poster
32 posts since Jun 2011
Reputation Points: 6
Solved Threads: 0
 
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
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

@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. :(

Riteman
Light Poster
32 posts since Jun 2011
Reputation Points: 6
Solved Threads: 0
 
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
Moderator
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
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: