ofstream

hi ia m trying to make a program to create text files from user input, but every time the return key is pressed it stops geting user input and writes the text to the file.i want it to just create a new line but i dont know how to, can somebody help? could you also tell me how to get input to name the file, thanks in advance.

im using;
windows xp
dev c++

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

int openfile();
int newfile();

    string   text;
    ofstream outfile("test.txt");
   
int main()
{
    char     options;
    
    do
    {
    system("cls");
    cout <<"\t\t-------------------------------------" <<endl;
    cout <<"\t\t| [o]pen file | [N]ew file | [e]xit |" <<endl;
    cout <<"\t\t-------------------------------------" <<endl;
    options =getch();
    
    switch(options)
    {
                   case 'o':
                        {
                        }
                   break;
                   case 'n':
                        {
                            newfile();
                        }
                   break;
    }
    
    
    }
    while(options != 'e');
    
    return 0;
}


int newfile()
{
    system("cls");
    cout <<"\t\t-------------------------------------" <<endl;
    cout <<"\t\t|              NEW FILE             |" <<endl;
    cout <<"\t\t-------------------------------------" <<endl;  
    
    getline(cin,text);
       
    if(! outfile)
    {
         cout <<"error writing to file" <<endl;
         return -1;
    }
    outfile << text <<endl;
    outfile.close();
    
}
Last edited on
With getline() you could specify which character will stop the input:
getline(cin,text,'\0'); should get until a '\0' char is found (which could probably never happen)

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int newfile()
{
    system("cls");
    cout <<"\t\t-------------------------------------" <<endl;
    cout <<"\t\t|              NEW FILE             |" <<endl;
    cout <<"\t\t-------------------------------------" <<endl;  
    cout <<"\tPress Ctrl+B to break input"<<endl;  
    
    getline(cin,text,'\2');
       
    if(! outfile)
    {
         cout <<"error writing to file" <<endl;
         return -1;
    }
    outfile << text <<endl;
    outfile.close();
    
}


And of course you will have to press enter after the breaking character
Last edited on
Thanks its working now :}
Last edited on
how do i make the file to be opened a variable?
e.g;
1
2
 ofstream outfile(myvariable.txt);
 getline(cin,myvariable) 

Do you mean:
1
2
3
4
string myvariable;
ofstream outfile;
getline(cin,myvariable);
outfile.open( (myvariable+".txt").c_str() );
so that:

input: C:\myfile
result: "C:\myfile.txt" is open
yes thank u :}
Topic archived. No new replies allowed.