Hi all,

I am writing a program in C++ to generate input files for a two programs for testing purpose. I can generate data files individually by specifying the name.

What I want is to be able to run the program once and it generates a number of data files e.g. test1.txt, test2.txt and so on. In general the main problem is how to print the counter variable as part of the file name.

Please if anyone know about this then let me know.


Many thanks,

Muz

Recommended Answers

All 58 Replies

Its easy --

#include <string>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    string filename, temp;
    for(int i = 0; i < 5; i++)
    {
        filename = "test";
        stringstream str;
        str << i;
        filename += str.str();
        filename += ".txt";
        cout << filename << "\n";\
    }
}

Here's another example. Almost the same as Ancient Dragon's.

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <fstream>

using namespace std;

int main()
{
   ofstream outputFile;
   string filePrefix = "file";
   string fileSuffix;
   string fileName;
   stringstream convert;

   for (int i = 1; i < 5; i++)
   {
	 //Convert integer to string and append to filename
     convert << i;
     fileSuffix = convert.str();
     fileName = filePrefix + fileSuffix;

     //Open file using *.c_str()
     outputFile.open(fileName.c_str());

     outputFile << "Hello There" << endl;
     outputFile.close();

     //Reset convert
     convert.str("");
   }
   return EXIT_SUCCESS;
}

// one way to do it

char filename array[9] > char filename[9];
int variable count equal to 0 >int count =0:

for this simple example increment int variable count one > var+=1;

..................01234
string copy TestX.txt to file name array variable

set file name array element 4 equal to (count+ "ASii offset " 48)

lets see what we got, print out file name variable > cout << filename << endl;

output ans = test1.txt

//you said, the main problem is how to print the counter variable as part of the file name.

//there are limits to this approach, you can only have 10 files, (0 to 9)!

hope that helps!
whole program below...dos window of course

#include <conio.h>
#include <string.h>
#include <iostream.h>

int main()
{
clrscr();

char filename[9];
int count = 0;
count+=1;
strcpy(filename, "testX.txt");
 filename[4]= (count + 48 );
cout << filename << endl;

//**********************end***********************
cout << "hit any key to exit." ;
getch();
} // end program

jazzman: what is that code going to do when count is two or more digits? If you want to use character arrays then use sprintf() to format the string. sprintf(filename,"test%d.txt", count);

#include <conio.h>
#include <string.h>
#include <iostream.h>
#include <stdio.h>

// sprintf is very nice !!!  thanks

int main()
{
clrscr();

char filename[9];
int count = 0;
for(count=0; count<100; count++)
 {
sprintf(filename,"test%d.txt", count);
cout << filename <<   "  ";
}
//**********************end***********************
cout << "hit any key to exit." ;
getch();
} // end program

First of all thank you all for you kind sugesstions and ideas.
Due to some reasons some of the programs dont work I am using Borland Compiler Version 3.1 and all i want is below.

To write data into a number of files e.g. say 3.
I want to name the files with the counter variable as part of the file name as text file assume the follwing code:

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    char FirstName[30], LastName[30];
    int Age;
    char FileName[20];

    cout << "Enter First Name: ";
    cin >> FirstName;
    cout << "Enter Last Name:  ";
    cin >> LastName;
    cout << "Enter Age:        ";
    cin >> Age;

    cout << "\nEnter the name of the file you want to create: ";
    cin >> FileName;
    ofstream Students(FileName, ios::out);
    Students << FirstName << "\n" << LastName << "\n" << Age;

    cout << "\n\n";
    return 0;
}

Now what i want actually is to be able to create 3 or more files with name e.g. test1.txt test2.txt and so on containing data of different students specified by the user.

Hope it can explain what I want to achieve I hope to hear from you guys soon.

Regards,

Muzhe

use sprintf() to format the name, i.e.

char filename[255];
sprintf(filename, "test%d.txt", loop_counter);

Its the same concept that I posted yesterday in post #2 to this thread.

use sprintf() to format the name, i.e.

char filename[255];
sprintf(filename, "test%d.txt", loop_counter);

Its the same concept that I posted yesterday in post #2 to this thread.

Hi,

The compiler gives an error message " sprintf(..........) should have a prototype.

Any idea what that is?

and can you kind recommend me some lattest C++ free compiler as some stuff dont work with Borland 3.1.

use sprintf() to format the name, i.e.

char filename[255];
sprintf(filename, "test%d.txt", loop_counter);

Its the same concept that I posted yesterday in post #2 to this thread.

Hi,

Thanks for that I managed to make it work.

However the all data files are save as testX.txt i.e. not with the name specified by user from prompt.

Also if you can tell me a way of printing three integers as part of the file name for instance test10.21.22.

Once again thanks the the help and advise.


Regards,

Muzhe

Hi all,

I managed to the generate output files with a the counter as part of the the filename. However what about if I want 3 varliable say A=10 B=32, C=21 to printed as part of the file name e.g. test10.32.21

Same syntax as stated before, just add more %d parameters in the format string, such as sprintf(filename, "test%d.%d.%d.%d", counter, A, B, C); There is no limit to the number of parameters you can pass to sprintf(), just make sure the type and number of parameters correspond to the format string.

Same syntax as stated before, just add more %d parameters in the format string, such as sprintf(filename, "test%d.%d.%d.%d", counter, A, B, C); There is no limit to the number of parameters you can pass to sprintf(), just make sure the type and number of parameters correspond to the format string.

Thanks for that. But one more question why can't I save the files with user specified name why is it always whats specified in the " " e.g. test

If its not possible then whats the purpose of having filename in the sprint statement.

Please comment on it.

Many many thanks for that.

Regards,

Muzhe

Of course its possible -- but that was not your previous question. Its all in the format specification string - you should read about it and become familiar with it so that you don't have to post all those questions. Not that I mind answering them, but it is to your advantage .

cin >> FileName;
sprintf(filename, "%s%d.%d.%d.%d", FileName, counter, A, B, C);

Of course its possible -- but that was not your previous question. Its all in the format specification string - you should read about it and become familiar with it so that you don't have to post all those questions. Not that I mind answering them, but it is to your advantage .

cin >> FileName;
sprintf(filename, "%s%d.%d.%d.%d", FileName, counter, A, B, C);

Hey

I tried but it didn't work, can u specify why please


Cheers,

>>I tried but it didn't work, can u specify why please

Post your code because I can't see your monitor.

cin >> FileName;
sprintf(filename, "%s%d.%d.%d.%d", FileName, counter, A, B, C);

The above code needs a small modification.

cin >> FileName;
sprintf(filename, "%s%d.%d.%d.%d%s", FileName, counter, A, B, C,".txt");

This works also.

sprintf(filename, "%s%d.txt", FileName, 1);

cin >> FileName;
sprintf(filename, "%s%d.%d.%d.%d", FileName, counter, A, B, C);


cin >> FileName;
sprintf(filename, "%s%d.%d.%d.%d%s", FileName, counter, A, B, C,".txt");

sprintf(filename, "%s%d.txt", FileName, 1);

Can you please explain the purpose of each argument and also tell me why are there two file names i.e. filename and the FileName??

Many thanks

We don't know the purpose of each argument other than that was what you wanted. In the format string, %s stands for a string, and %d is for an integer. There are a lot of other formats too but you need to read about them either in a text book or online.

As for FileName and filename, the first one is for user input and the second one is what the program will construct. Can't be done with just one variable. Well, that's not quite correct, but its much easier to use two character arrays instead of one.

Can you please explain the purpose of each argument and also tell me why are there two file names i.e. filename and the FileName??

Many thanks

Hi

Sorry to bother but none of the statements are working. Can you please sugesst alternatives.

Thanks

Hi

Sorry to bother but none of the statements are working. Can you please sugesst alternatives.

Thanks

Post the code you have tried.

#include <fstream.h>
#include <iostream.h>
#include <stdio.h>
int main()
{
    char FirstName[30], LastName[30];
    int Age;
    char FileName[255];
    char Filename;
     cout << "\nEnter the name of the file you want to create: ";
    cin >> FileName;
    int a=10,b=12,c=13;
    sprintf(FileName,"s%i.%i.%i", FileName,a,b,c, ".txt");
    cout << "Enter First Name: ";
    cin >> FirstName;
    cout << "Enter Last Name:  ";
    cin >> LastName;
    cout << "Enter Age:        ";
    cin >> Age;
    ofstream Students(FileName, ios::out);
    Students << FirstName << "\n" << LastName << "\n" << Age;

    cout << "\n\n";
   return 0;
}

With above I have managed to print the filename entered by user from prompt but the values of a, b and c are still not coming up with the file name.

Thanks alot for your help.

as ancient dragon said you need 2 char[]'s. one for the fully assembled file name and one for the user supplied file name.

char filename[30];  // used for the total filename
char FileName[20];  // used to get the filename from the user
int a,b,c;
cout << "please enter the filename: ";
cin >> FileName;
sprintf(filename /*full filename*/, "%s%d.%d.%d.%d%s" , FileName /* user supplied filename*/, counter, A, B, C, ".txt");
// the final ".txt" will make the document a text  document.

maybe this will clear it up for you.

as ancient dragon said you need 2 char[]'s. one for the fully assembled file name and one for the user supplied file name.

char filename[30];  // used for the total filename
char FileName[20];  // used to get the filename from the user
int a,b,c;
cout << "please enter the filename: ";
cin >> FileName;
sprintf(filename /*full filename*/, "%s%d.%d.%d.%d%s" , FileName /* user supplied filename*/, counter, A, B, C, ".txt");
// the final ".txt" will make the document a text  document.

maybe this will clear it up for you.

****************************************

Hi,

Sorry i still can't print the values of a, b and c as part of the filename it saves the file with the user specified name but not the integers. Can you please comment on it.
Many thanks


Sorry i still can't print the values of a, b and c as part of the filename it saves the file with the user specified name but not the integers. Can you please comment on it.
Many thanks

Stop whining about it and post the code you tried. Do you really think we can see the code on your monitor :icon_eek:

Sorry mate code is below please have a look. Plus you can give me a quick solution for just printing a specified file name with integer values i.e. of a,b and c then thats perfectly fine as well.

Many thanks for your time.

Code:

#include <fstream.h>
#include <iostream.h>
#include <stdio.h>
#include<conio.h>


int main()
{
    char FirstName[30], LastName[30];
    int Age;
    char FileName[255];
    char filename[30];
     cout << "\nEnter the name of the file you want to create: ";
    cin >> FileName;
    int a=1,b=2,c=3;
    
    //for(int i=0; i<=3; i++)
    //{
    sprintf(filename, "%s, %i., %i., %i,%s",  a,b,c, ".txt");

    cout << "Enter First Name: ";
    cin >> FirstName;
    cout << "Enter Last Name:  ";
    cin >> LastName;
    cout << "Enter Age:        ";
    cin >> Age;

   
    ofstream Students(FileName, ios::out);
    Students << FirstName << "\n" << LastName << "\n" << Age;

    cout << "\n\n";
    //}
    
    return 0;
}

line 29: you are passing FileName array, which only contains the name entered from the keyboard on line 14. You need to pass filename which is formatted on line 19.

Do you really want all those commas in the filename?

That code then should look like this:

#include <fstream.h>
#include <iostream.h>
#include <stdio.h>
#include<conio.h>


int main()
{
    char FirstName[30], LastName[30];
    int Age;
    char FileName[255];
    char filename[30];
     cout << "\nEnter the name of the file you want to create: ";
    cin >> FileName;
    int a=1,b=2,c=3;
    
    sprintf(filename, "%s, %i., %i., %i.txt",  FileName, a,b,c);

    cout << "Enter First Name: ";
    cin >> FirstName;
    cout << "Enter Last Name:  ";
    cin >> LastName;
    cout << "Enter Age:        ";
    cin >> Age;


   
    ofstream Students(filename, ios::out);
    Students << FirstName << "\n" << LastName << "\n" << Age;

    cout << "\n\n";
    //}
    
    return 0;
}

Its giving me an error with expression syntax in this line
sprintf(filename, "%s, %i., %i., %i.txt", FileName, a,b,c);

post code -- that program works ok for me using vc++ 2008 express compiler. What compiler are you using?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.