Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   A problem with File I/O names (http://www.programmingforums.org/showthread.php?t=8771)

HaCkeR Mar 8th, 2006 3:04 PM

A problem with File I/O names
 
Ok, ive tried o sort this out but i can't.
What i want to do is to create say 10 file all called 'hello.text' but i want them to have a number after them so i would get
hello1.txt hello2.txt etc

i know i can be done using loops and stuff but i just cant work out host to use a loop and change the number any help useful. thank you

DaWei Mar 8th, 2006 3:05 PM

Use string streams or sprintf.

HaCkeR Mar 8th, 2006 3:11 PM

Thanks ill have a little read if i still dont get it ill come back to you

HaCkeR Mar 8th, 2006 3:27 PM

sprintf seems to be my best option but i have this code so far but it doesn't create the file

:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    char loc[200] = "H:\\";
    int i =1;
    sprintf (loc, "blah%d.txt", i);
    return 0;
}


DaWei Mar 8th, 2006 3:31 PM

Well, no, that just makes a name. You'll have to open the file(s) for writing to actually create it/them. To do that, declare a file object and open it for writing. Alter the name and loop around as many times as necessary. You'll probably want to create one file, write to it, or whatever your use for it is, complete your operations with it entirely, then generate the next name and do it all again (via loop). Have you used iostreams/filestreams before?

HaCkeR Mar 8th, 2006 3:42 PM

ive only used the basic input and output never tried to do this and i cant succed, ive tried to open the file but still no success

Infinite Recursion Mar 8th, 2006 3:44 PM

http://www.cplusplus.com/ref/iostream/fstream/

DaWei Mar 8th, 2006 4:51 PM

Here is a simple piece of code to study:
:

#include <iostream>    // Included for I/O stream operations
#include <fstream>      // Include for file stream operations
#include <string>      // Included for C++ string usage.
                        // They're easier to handle than char arrays

using namespace std;    // To use the entire standard namespace,
                        // thus precluding the necessity of
                        // using the scope resolution operator.
                        // Can introduce name conflicts if due
                        // care is not taken.

const unsigned MAXNAMELEN = 255;
// Function to send error message to stderr stream
int uhOh (string trouble)
{
    cerr << trouble << endl;
    return 1;
}
// Main
int main (int argc, char *argv [])
{
    string baseName = "Textfile";
    string baseExt = ".txt";
    char fileName [MAXNAMELEN];
    unsigned desiredFiles;

    // There are file streams, input file streams, and output file streams.
    // Pick an choose how you like, just read the documentation.
    fstream myFile;

    cout << "Enter number of files to create: ";
    cin >> desiredFiles;
    if (!cin.good ()) return uhOh ("Bad input");

    for (unsigned i = 0; i < desiredFiles; i++)
    {
        // Construct file name.
        // Robust code would worry about the resulting file name length.
        // On the other hand, one could use a string instead of a char array
        // and use, "fileName.c_str".  Choices abound.
        sprintf (fileName, "%s%d%s", baseName.c_str (), i, baseExt.c_str ());
        // Open a file
        myFile.open (fileName, ios::binary | ios::out);
        if (!myFile.good ()) return uhOh ("Ouput file didn't open");
        myFile << "This is file " << i << endl;
        myFile.close ();
        myFile.open (fileName, ios::binary || ios::in);
        if (!myFile.good ()) return uhOh ("Input file didn't open");
        cout << myFile.rdbuf ();
        myFile.close ();
    }
    cout << "Press ENTER to exit (don't you love to say that?)";
    cin.sync ();
    cin.get ();
    return 0;
}

The output looks like this:
Quote:

Originally Posted by Output
Enter number of files to create: 10
This is file 0
This is file 1
This is file 2
This is file 3
This is file 4
This is file 5
This is file 6
This is file 7
This is file 8
This is file 9
Press ENTER to exit (don't you love to say that?)


nnxion Mar 8th, 2006 6:43 PM

I'd use string streams if I were using C++. Why do you say that sprintf is the better option?

DaWei Mar 8th, 2006 7:19 PM

I don't say that it's better, Ruben. As a matter of fact, sprintf has been deprecated. Now ask yourself, if you were Hacker, and ALL of this was new to you, if you'd like to learn string streams at the same time as I/O and file streams, or if you'd maybe like to take one after the other. I guess I need to go back and read my posts, I don't recall saying it was the better option.


All times are GMT -5. The time now is 5:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC