Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,187 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,117 people online right now. Registration is fast and FREE... Join Now!




Using variables in filenames

 
Reply to this topicStart new topic

Using variables in filenames

Trake
2 Aug, 2007 - 12:57 PM
Post #1

D.I.C Head
**

Joined: 29 Jun, 2007
Posts: 60


My Contributions
Hi all,

Im trying to write a program that automatically creates files with names like: example1.txt; example2.txt etc. up to a point determined by the code.
so far I have the following:
CODE
#include <iostream>
#include <fstream>
using namespace std;

int fileCount;
int fileTotal = 10;

int main () {

  for (fileCount = 0; fileCount < fileTotal; fileCount++){
  ofstream myfile;
  myfile.open ("example" + fileCount + ".txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
}

  return 0;
}


But this wont even compile as fileCount is an int, I tried changing this to char or string but this wont work.
Thanks in advance for the help.

Trake
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Using Variables In Filenames
2 Aug, 2007 - 04:06 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, so I guess you just need to convert an int into a string.
HERE is an example where such function is implemented.
Hope it helps.
just the function:
CODE
template<typename T> string toString(const T& x) {
   ostringstream oss;
   oss << x;
   return oss.str();
}

but you have to
CODE
#include <sstream>
for this I believe.
User is offlineProfile CardPM
+Quote Post

Trake
RE: Using Variables In Filenames
3 Aug, 2007 - 04:37 AM
Post #3

D.I.C Head
**

Joined: 29 Jun, 2007
Posts: 60


My Contributions
Thanks for the quick reply it looks about right, only I want to change a char to an int not a string. I thought maybe I could do something like:
CODE
char fileStart[8] {e, x, a, m, p, l, e}
char fileExtension[4]{., t, x, t}
char fileName[12]


And then change fileCount to a char and then make
CODE
fileStart[7] = fileCount
followed by
CODE
fileName[0] = fileStart[0]; fileName[1] = fileStart[1];
etc.
is this possible?
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Using Variables In Filenames
3 Aug, 2007 - 12:51 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(Trake @ 3 Aug, 2007 - 05:37 AM) *

Thanks for the quick reply it looks about right, only I want to change a char to an int not a string. I thought maybe I could do something like:
CODE
char fileStart[8] {e, x, a, m, p, l, e}
char fileExtension[4]{., t, x, t}
char fileName[12]


And then change fileCount to a char and then make
CODE
fileStart[7] = fileCount
followed by
CODE
fileName[0] = fileStart[0]; fileName[1] = fileStart[1];
etc.
is this possible?


I don't think that's possible because you are trying to convert an int into char with the assignment operator.
I ask you why char and not string?

This post has been edited by PennyBoki: 3 Aug, 2007 - 12:52 PM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Using Variables In Filenames
3 Aug, 2007 - 12:56 PM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,451



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
I would use sprintf & dump everything into a buffer variable.
User is online!Profile CardPM
+Quote Post

Trake
RE: Using Variables In Filenames
3 Aug, 2007 - 01:57 PM
Post #6

D.I.C Head
**

Joined: 29 Jun, 2007
Posts: 60


My Contributions
I said char simply as I was trying to keep the code as simple as I can and as streamlined so that I can use it to create a lot of files ie about 1000 or more.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Using Variables In Filenames
3 Aug, 2007 - 02:30 PM
Post #7

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(Trake @ 3 Aug, 2007 - 02:57 PM) *

I said char simply as I was trying to keep the code as simple as I can and as streamlined so that I can use it to create a lot of files ie about 1000 or more.

And I said string
for this function here
QUOTE
myfile.open ("example" + fileCount + ".txt");

where instead of fileCount which is an int there would be some variable that's a string in order to use that open() function.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:18AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month