files with user defined names.

Mar 28, 2008 at 1:43am
hey all need some help. :)
I have already learned how to create a file, and enter data in the file. And also to READ all the contents (thanks to this site) **thumbs up to everyone**

But heres my problem : I wish to make my program create unique files. That is the user can ENTER the filename.
Any help is appreciated and thanks in advance.

tried a pointer.. to get it after a cin command..
tried reading it in then stating the the variable as the filename...
Looked at tmpnam but that definately threw me in a tizzy..
none of these work!! :(
Mar 28, 2008 at 8:13am
1
2
3
4
5
6
7
cout << "Enter filename: ";
std::string filename;
cin >> filename;
cout << endl;

// Open file
ifstream file(filename.c_str());
Mar 28, 2008 at 8:13am
it doesn't work with a pointer, you need an array:
1
2
3
4
5
char filename[256];
cin.getline(filename, 256);  // <- to allow whitespaces
std::fstream f(filename, ios::in);
// do something with the file
f.close();
Mar 28, 2008 at 6:06pm
ty ty ty.. **thumbs up** Thanks guys!! so far i have been able to name them, and also specify files to be retrieved.. due to your help.

THANKS a bunch :) ill post any other snags i happen into.

i am really trying to set up like an inventory database :)
Mar 28, 2008 at 6:41pm
oh well i forgot to post this before.. im having trouble getting to read in input that has spaces.
e.g.
Delivery address : 58 multree hill

when i use a string or char.. as soon as i press SPACE it hops out of the category and the program stops.
tried getline.. but to no avail.

Also.. i actually take in the values.. then READ them in after into the file.. maybe thats the problem.
Well heres what works... (that is when its a CHAR of 20)
**note**the var i want to read in with spaces is Delivery_Add


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// writing on a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
char sg [20];
char C_name [20];
char Delivery_Add [20];
char Country [20];
char Post_code [20];
char Date [20];
char Product [20]; 

int Invoice_num;
int Contact_num;
int Order;
int qty;
int FAX;
char TRN;
int Sell_price;
int product_cost;
int ADD;
int Edit;
int Delete;


int main () {
//menu and Welcome
cout<<" ***************************"<< endl;
cout<<" *                         *"<< endl;
cout<<" *       NEW INVOICE       *"<< endl;
cout<<" *                         *"<< endl;
cout<<" ***************************"<< endl;
//filenaming
//Note TRN is the filename
cout << "Enter TRN: ";
std::string TRN;
cin >> TRN;
cout<<endl;
//menu
cout<< "Fill in the required fields:";
cout<< endl;
cout<<endl;
cout<< "First Name :";
cin>>sg;
cout<< endl;






// Open file
ofstream myfile(TRN.c_str());
if (myfile.is_open())
  {
//this code block takes in the data..it has not been written to the file yet.                     
cout<< "Last Name :";
cin>> C_name;
cout<< endl;
cout<< "Delivery Address :";
cin>> Delivery_Add;
cout<< endl;
cout<< "Contact number :";
cin>> Contact_num;
Invoice_num + 1;
cout<< endl;
cout<< "Country   :";
cin>>Country;
cout<< endl;
cout<< "Product :";
cin>>Product;
cout<< endl;
cout<< "Quantity? :";
cin>> qty;
cout<< endl;

//this section writes the data in the file

myfile<< "First Name :";
myfile<<sg;
myfile<< endl;
myfile << endl;
myfile<< "Last Name :";
myfile<<C_name;
myfile<< endl;
myfile << endl;
myfile<< "Delivery Address :";
myfile<<Delivery_Add;
myfile<< endl;
myfile << endl;
myfile<< "Contact number :";
myfile<<Contact_num;
myfile << endl;
myfile << endl;
myfile<<"Country  :";
myfile<<Country;
myfile << endl;
myfile << endl;
myfile<< "Item :";
myfile<<Product;
myfile<< endl;
myfile << endl;
myfile<< "Amt :";
myfile<<qty;
myfile << endl;
myfile<< Invoice_num;
cout<<endl;
time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "The Invoice was created at: %s", asctime (timeinfo) );
myfile<<asctime (timeinfo);

    myfile.close();
  }
  else cout << "Unable to open file";

system("PAUSE");
  return 0;
}
Last edited on Mar 28, 2008 at 6:59pm
Topic archived. No new replies allowed.