Thread: Writing data to Multiple files

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    24

    Question Writing data to Multiple files

    I can use the following code to write the content of a string to a file,

    ofstream outfile;
    outfile.open("xyz.txt");
    outfile<<strng1<<endl;

    but what should I do to write content of different strings to different files. Multiple use of outfile is creating errors.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    More object!
    Object good!

    Code:
    string randomname()
    {
        string temp;
        for (int i = 0; i < 20; i++)
            temp += (65 + (rand() % 90));
        return temp;
    };
    
    ofstream f[256];
    for (int i = 0; i < 256; i++)
        f[i].open(randomname().c_str());
    There, we now have 256 files with random names opened. In short, just create an ofstream object for each file you need to write to.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    You may perform write I/O to a file sequentially. In other words, one or more threads can write to a file one at a time, not simultaneously.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. writing to file with multiple forked process
    By hiawatha in forum C Programming
    Replies: 7
    Last Post: 04-18-2007, 06:30 PM
  5. Replies: 26
    Last Post: 06-15-2005, 02:38 PM