Writing to multiple text files

Hi all,

I'm a C++ student needing help with writing a program to capture product details in a text file and store the same details in 500 external files. I'm able to create a single file, but after that...

Will appreciate your help.
generate 500 file names programatically, and copy contents of old file.
eg:
1
2
3
4
5
6
7
for(int i=1;i<500;i++)
{
   String filename="path\\xyz_fileno"+i+"fileextension like .txt";
   ifstream ifs_current(filename);
//open original file copy it to current file

}

here is a link to code to copy from one file to another:
http://stackoverflow.com/questions/10543230/fastest-way-to-copy-data-from-one-file-to-another-in-c-c
If it's small text files you're talking about, then an fsteam based copy loop is prob. good enough.

But with Boost.Filesystem you can just copy the files.

boost::filesystem::copy_file( src_path, dst_path );

The Windows API also provides a copy function (CopyFile), whereas POSIX appears not to (Boost uses CopyFile on Windows but a custom copy routine on other platforms; they do not use sendfile.)

Andy

Boost.Filesystem
http://www.boost.org/doc/libs/1_54_0/libs/filesystem/doc/index.htm
Boost.Filesystem is very likely going to turn up in C++14 (it's currenly going though the acceptance process...)

CopyFile function
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363851%28v=vs.85%29.aspx
Thanks @andywestken, yes, the text files are small, so using an fstream loop.
@SirSmilesaLot, i have incorporated that into my code below, still doesn't work and I can't figure out where I'm going wrong. Some more help please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include <iostream>
#include <fstream>
using namespace std;
    
int main () 
{
     std::ofstream outfile ("ProductDetails.txt");
     outfile << "ProductDetails" << std::endl;
     outfile.close();

        {
	   Int counter;
	   String product_name;
	   String product_size;
	   Cout<<”enter the product name”<<endl;
	   Cin>>product_name;
	   Cout<<”enter the product size”<<endl;
	   Cin>>product_size;
	   Ofstream myfile;
	   For(counter=1;counter<=500;counter++)
     }
	     {
	         String filename="d\\ProductDetails"+.txt";
	         ifstream ifs_current(ProductDetails.txt);
	     }
          {
	    Filename=”file”+counter+”.txt”;
	    Myfile,open(ProductDetails.txt);
	    Myfile(<<product_name<<endl;
	    Myfile<<product_size<<endl;
	    Myfile.close();
	 }

Return 0;
}
 

Last edited on
Topic archived. No new replies allowed.