You are here:

C++/Output file name in C++



I have wrtiting a c++ program below:

#include <fstream.h>
#include <iostream.h>
#include <stdio.h>
//using namespace std;

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=10,b=12,c=13;
   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(, ios::out);
   Students << FirstName << "\n" << LastName << "\n" << Age;

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

This program creates an output file with the name specified by the user form prompt e.g. test.txt. I am trying to print the values of variables a,b,c as part of the file name e.g. test12.13.14.txt.

Can you please help.

Many thanks

ANSWER: Hi Muzhe, thank you for the question.

I recommend using the std::ostringstream class over sprintf as it is object oriented, and much safer. It works just like cout. You need to include the header sstream to use it.

char filename[255];
int a = 10, b = 12, c = 13;
cout << "enter the file name"
cin.get(filename, 254);

std::ostringstream oss;
oss << filename << a << b << c;
std::string str = oss.str(); // Gets the string value

cout << str;

You can also place the dots in the ostringstream as you please to get the formatting you want.

oss << filename << "." << a << "." << b << "." << c;

And so on. That should have you pointing in the right direction.

I hope this information was helpful.

- Eddie

---------- FOLLOW-UP ----------


Thanks for your answers. I got one more question can you plese tell me how to increment a number in fraction. For example i've one number a=.01 how do i increment this number with .01 for a number of times. All I want to run a loop which will the increment and print the numbers from .01 i.e. .01, .02, .03 and so on.

ANSWER: Hello Muzhe, thank you for the question.

You can accomplish this with the float data type just like you would an integer.

float a = 0.01f;

for(int i = 0; i < 10; i++)
a += 0.01f
cout << "The value is: " << a << std::endl;

This should accomplish what you want.

I hope this information was helpful.

- Eddie

---------- FOLLOW-UP ----------


Can you please specify a method which will write data into files as above but in the Visual C++ 2005 environment. There are so many commands that doesnt work with VS 2005.

Hello again Muzhe, thank you for the question.

I apologize for not responding sooner; I didn't have internet access for a couple of days.

The above code should compile and run as anticipated in VS2005, provided you take the .h off of the include file names.

#include <fstream>
#include <iostream>
#include <cstdio>
#include <conio>

Please let me know if this works for you.

I hope this information was helpful.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 All rights reserved.