[i am new to c++] I am making a text editor that will open a file and output what the person types into a .txt file. So far i have a prog that will open a file but how do i get the input and output it into the file?
Printable View
[i am new to c++] I am making a text editor that will open a file and output what the person types into a .txt file. So far i have a prog that will open a file but how do i get the input and output it into the file?
www.cprogramming.com there is a very good tutorial on file input and output.
Okm so i got the file i/o sorted but now when it outputs the text into a .txt file it just outputs the 1st word the user inputs.
use getline()
check www.cppreference.com how to use it.
I just tried that getline() but all that i get is a bunch of numbers e.g. 0x443468.
getline(cin, stringname);
Ok after looking at the web page that is what it shows you to do...post your code.
Have you got #include <string>
i tried it with the #include <string> still didn't work(still got numbers).
Post your code....
It is really the only quick way of getting help.
Code:#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char str[100];
string s;
ofstream a_file ("LW_Text.txt", ios::app);
cout<<"Please input some text.\n";
cout<<">";
getline(cin,s);
cin.ignore();
a_file<<getline(cin,s);
a_file.close();
cin.get();
}
Try that...it works for me now.Code:#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string s; //your char string was redundant here
ofstream a_file ("LW_Text.txt", ios::app);
cout<<"Please input some text.\n";
cout<<">";
getline(cin,s);
cin.ignore();
a_file<<s; //you don't need getline here
a_file.close();
cin.get();
}
Now try making it able to read the txt file.
And possibly make the user able to choose the name of the file.
How could i make the user specify the name of the file?
By having the user enter the file name on a prompt and you saving it as a string and then opening a file based on what they wanted. Just remeber c_str().
And if they only want to enter the first part of the file name then you could use string name += ".txt"