Text File

How do I do something like this...

myfile.open ((filename)(".txt"));

or

ifstream myfile ((filename)(".txt"));

those aren't the actual codes, obviously, just asking how I can make the user make their own file name:
cin >> filename
and then make a file out of the name they made.

Please help!

Edit:

filename += ".txt"

myfile.open (filename);

this mix didn't work :'(

Edit:

Surprisingly, this cut down on those HUGE errors, but still doesn't work :'(


filename += ".txt"

myfile.open (("\"")folder("\""));
Last edited on
1
2
3
4
std::string str;
std::cin>>str;
str+=".txt";
std::ifstream file(str.c_str());
Maybe make a string variable like string filename; and get the user to input the filename into that. Then, do something like Filename.append(".txt"). That should get you a filename and extension. Then maybe use the filename variable to create your file? I haven't tried this out as I am on my iPad, but in theory, it might work. :)
agh, neither one worked >_<

Here's the full code, if it helps:

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
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;

int main () {
  string line;
  string name;
  string age;
  string time;
  string folder;
  

  cout<<"Please enter new text file name. Certain \
symbols are not accepted, check your computer's \
handbook for details(if you have already made a \
folder, press Enter)";
  cout<<endl;
  getline (cin, folder);
  cout<<endl;
  
  folder += ".txt";
  
  if (!folder.empty())
  {
  cout << "Enter your name";
  cout << endl;
  getline (cin, name);
  cout << endl;
  cout << "Enter your age";
  cout << endl;
  getline (cin, age);
  cout << endl;
  cout << "Enter the time";
  cout << endl;
  getline (cin, time);
  cout << endl;
  
  ofstream myfile;
  myfile.open (("\"") (folder) ("\""));
  myfile << name;
  myfile << endl;
  myfile << age;
  myfile << endl;
  myfile << time;
  myfile << endl;
  myfile.close();
  }
  
  else if (folder.empty())
  cout<<endl;
  
  

  
  ifstream myfile (("\"")folder("\""));
  if (myfile.is_open() && myfile.good())
  {
  cout<<"Here are your entries\n\n";                       

      getline (myfile, line);
      cout << "name:";
      cout << line;
      cout << endl;
      getline (myfile, line);
      cout << "age:";
      cout << line;
      cout << endl;
      getline (myfile, line);
      cout << "time submitted:";
      cout << line;
      cout << endl;

  myfile.close();
  }

  else
  cout << "Unable to open file"; 
  getch ();
  return 0;
}


it just won't recognize folder when it compiles, so I don't know if it's impossible for the person using the program to make a name of their own, or what, but it definitely should be possible.
Last edited on
Try std::string("\"") + folder + "\"" in the relevant locations.
LoL,

cout<<noob<<" asks, \"Where are the relevant locations?\"";
me = noob;
Last edited on
Ugh, I hate it when a problem can never be solved >_< Aggggh!

locations you have to find.

if the filename is coming in parts and you want to join all the parts, you can do like this:

1
2
3
4
5
std::string filename = "c:\\windows";
filename += "\\system32";
filename += "\\cmd.exe";

myfile.open (filename); //going to open cmd.exe 
oh, I see... Alright, so if I go like string filename = "c:\\windows\\users\\desktop" or something of equal stature, it would place the direct file to the desktop. That's interesting, I'll test it in the morning.
Alright, well I still haven't figured this problem out yet, does anybody know how to make it possible for the user to enter in a variable and then to make a text document out of the word(s) they wrote? For example:

1
2
3
string filename;
cin>>filename;
myfile.open (filename);


Because that does not work.
Use filename.c_str()

fstreams want a char* so you can't give it a normal std::string.
I do not understand, you mean change string filename into filename.c_str()?

I hate being unteachable >_< **** my brain =_=
closed account (zb0S216C)
It's quite simple: myfile.open( filename.c_str( ) );
Last edited on
WOOT! TY Everyone! A special thanks to Framework for seeing me at my level of intelligence xD

Anyways, that worked out, but the getline functions are still screwing me xD

Whenever I have a getline (cin,string); it always skips that line and goes to the next one >_<

If you know how to fix that to, I'd love you for life xD
closed account (zb0S216C)
Which if block does this problem occur?

Just to clarify, ofstream is used to write to a file. ifstream is used to read from a file.
Last edited on
TY for clarification, Framework. Here's the current code, and it's the very first if.
Note to self: I need to stop spamming out the forums xD

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
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;

int main () {
  string line;
  string name;
  string age;
  string time;
  string folder;
  

  

  cout<<"Please enter new text file name. Certain symbols are not accepted, check your computer's handbook for details(if you have already made a folder, press Enter)";
  cout<<endl;
  cin >> folder;
  cout<<endl;
  "\"" + folder;  
  folder += ".txt\"";
  cout<<endl;
  
  if (!folder.empty())
  {
  cout << "Enter your name";
  cout << endl;
  getline (cin, name);//it skips this getline.
  cout << endl;
  cout << "Enter your age";
  cout << endl;
  getline (cin, age);
  cout << endl;
  cout << "Enter the time";
  cout << endl;
  getline (cin, time);
  cout << endl;
  
  ofstream myfile;
  myfile.open (folder.c_str());
  myfile << name;
  myfile << endl;
  myfile << age;
  myfile << endl;
  myfile << time;
  myfile << endl;
  myfile.close();
  }
  
  else if (folder.empty())
  {
  cout<<"Please give the name of your old folder.";
  cout<<endl;
  cin >> folder;
  cout<<endl;
  folder += ".txt\"";
  }
  
  ifstream myfile (folder.c_str());
  if (myfile.good())
  {
  cout<<"Here are your entries\n\n";                       

      getline (myfile, line);
      cout << "name:";
      cout << line;
      cout << endl;
      getline (myfile, line);
      cout << "age:";
      cout << line;
      cout << endl;
      getline (myfile, line);
      cout << "time submitted:";
      cout << line;
      cout << endl;

  myfile.close();
  }

  else
  cout << "Unable to open file"; 
  getch ();
  return 0;
}
Last edited on
Okay, I'm gonna head to bed, sorry I can't stay on for the answer to the mysterious getline skipage >_<
closed account (zb0S216C)
You need to synchronize your input stream. This is done by calling a simple function:
1
2
3
4
5
6
7
if (!folder.empty())
{
    cin.sync( ); // Synchronize the stream.
    cout << "Enter your name";
    cout << endl;
    getline (cin, name);//it skips this getline.
    // ... 

You can read up on cin.sync( ) here: http://www.cplusplus.com/reference/iostream/istream/sync/
Last edited on
Thanks for all the help!
Topic archived. No new replies allowed.