Thread: txt file from ofstream. Increment the files name?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    txt file from ofstream. Increment the files name?

    Hi all

    I'm trying to generate some text files and have the name of each file created increment or be random (doesn't matter which), but am having difficult as the way I know of creating txt files is by placing the name of the file between double quotes. I have tried replacing the name part with rand(), and also by placing a variable where the name appears and then incrementing the value of the variable on each iteration of the loop (variablename++).

    Both failed.

    Here is my code, pretty standard:

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    
    void main()
    {
    	for(int a =0;a<99;a++)
    	{
    
    		ofstream out("WantToIncrementThis");
    		if(!out)
    		{
    			cout << "cannot open file";
    		}
    		out << rand() << rand() << rand()<< rand() << rand() << rand()<< rand() << rand() << rand();
    
    		out.close();
    	}
    	system ("pause");
    }
    So I just wondering how I should go about this.

    Many thanks for any help1!

    Swerve

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You'll have to generate a string with the number in it - for example using stringstream or sprintf.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "Print" a string (using stringstreams, for instance), and then use string.c_str() in the ofstream constructor.

    There's also tmpnam in ... cstdio, I guess.

  4. #4
    deletedforumuser
    Guest
    Example:

    Code:
    	char Filename[256] = "File";
    
    	for(int i = 0; i < 10; i++)
    	{
    		sprintf_s(Filename,"File%d",i+1);
    		cout << Filename;
    	}

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Or if you want to be standard C...

    Example:
    Code:
    	char Filename[256];
    
    	for(int i = 0; i < 10; i++)
    	{
    		std::snrintf(Filename, sizeof(Filename), "File&#37;02d",i+1);
    		std::printf("%s\n", Filename);
    	}
    The extra arguments I put in there are because I like my files to be listed numerically in order.

    There is a more C++ approach to this using stringstreams.

    Example:
    Code:
    #include <sstream>
    
    //... later in your code
    
    std::stringstream Filename;
    
    for(int i = 0; i < 10; ++i)
    {
      Filename.clear();
      Filename << "File";
      Filename.fill('0');
      Filename.width(2);
      Filename << i;
      std::cout << Filename.str().c_str() << std::endl;
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sf.net/void_main
    Void main is non-standard and should not be used.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

Tags for this Thread