Thread: File Copier

  1. #1
    Registered User Kimbokasteniv's Avatar
    Join Date
    Mar 2005
    Location
    AZ
    Posts
    4

    Question File Copier

    I'm pretty much a noob at c++, but I'm trying.
    Ok basically this progam is supposed to make a file 5 times, using a do while. However every time the loop restarts it writes the file like its supposed to and uses the same file name like expected but this is just the problem, since the file name is the same it only writes one file instead of 5, how can the file name to be used, be changed every time the loop iterates?

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char str[10];
      int x=0;
    
     do
     {
     x++;
      ofstream a_file ( "file.txt" );
      a_file<<"test";
      a_file.close();
    }
     while(x<5);
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Instead of hardcoding "file.txt" into the ofstream line, use a char string. Then you can simply change one character of the string every time through the loop.

  3. #3
    Registered User Kimbokasteniv's Avatar
    Join Date
    Mar 2005
    Location
    AZ
    Posts
    4
    I understand what you are saying but I am not sure how you would change one character or any character in the string automatically. What I tried was I left the x as an int, but in the ofstream line I used (char)x to convert it into a char (and the counter would change the name each time), however the compiler says the file name cant be a char but a const char. Is there a way of changing a const char in each iteration?

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Hello,

    Here is my solution to your problem.
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char str[] = "file .txt";
    	char str2[] = "01234";
    	int x=0;
    	do
    	{
    		str[4] = str2[x];
    		ofstream a_file ( str );
    		a_file<<"test";
    		a_file.close();
    		x++;
    	}
    	while(x < 5);
    }
    EDIT
    swoopy's solution is better because it easily allows for more values of x then just x < 10
    Last edited by MadCow257; 03-15-2005 at 08:53 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One idea is sprintf().
    Code:
    #include <cstdio>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char filename[80];
      int x=0;
    
     do
     {
      x++;
      sprintf(filename,"file%d.txt",x);
      ofstream a_file ( filename );
      a_file<<"test";
      a_file.close();
    }
     while(x<5);
    }
    Last edited by swoopy; 03-15-2005 at 08:55 PM. Reason: included cstdio header

  6. #6
    Registered User Kimbokasteniv's Avatar
    Join Date
    Mar 2005
    Location
    AZ
    Posts
    4
    Hey thanks swoopy, that was the perfect solution, and I'm going to need to spend some time looking at it to figure out how it works,
    just one more question, instead of just creating a file and copying it 5 times, how could you take an already created file and copy it 5 times?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one way.
    Code:
    #include <cstdio>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char src_filename[80] = "filename.xxx";
      char filename[80];
      int x=0;
    
      ifstream in(src_filename,ios::binary);
      if (!in.is_open())
      {
         cout << "File not found: " << src_filename << endl;
         return 1;
      }
     do
     {
      x++;
      sprintf(filename,"file%d.txt",x);
      ofstream a_file ( filename, ios::binary );
      a_file << in.rdbuf();
      a_file.close();
      in.seekg(0);
     }
     while(x<5);
      cout << "File " << src_filename << " copied." << endl;
    }
    You can also use CopyFile() if you are using windows. To use it, include <windows.h>.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    A third option is the system() command. Something like:
    Code:
    system("copy a.txt b.txt");
    or whatever the file copy command is on your computer.

  9. #9
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	int i = 0;
    	do
    	{
    		char file [200];
    		itoa(i,file,10);
    		strcat(file,".txt");
    		ofstream a_file(file, ios::app);
    		i++;
    	}
    	while (i < 5);
    
    }
    Something like this will do you fine just modify the code for your needs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM