console text-editor

I have just made a console type text editor but I can't figure out how to save the text file and if I do make it then will it be text?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string file_name,text;
fstream file;
flile.open(file_name+".txt")
cout<<"enter file name here:";
cin>>file_name;
cout<<"enter text here:";
cin>>text;
file<<text;
return 0;
}
Last edited on
I would make a customized stack that allows popping off the front and pushing to the back.

Then store single characters into the stack. That will you will be able to use backspace and whatnot.

Then when you are reading to write the file simply write the stack to the output file.
The code cin >> mystring; only inputs a single "word". Anything else you type is not read.

Use getline( cin, mystring ); to read everything until the user presses ENTER.

See also "Press ENTER twice to finish"
http://www.cplusplus.com/forum/beginner/2409/#msg9254

Hope this helps.
Guys keep it simple I'm only a beginner.
Last edited on
Before going on with your program, you might consider to read this http://cplusplus.com/doc/tutorial/basic_io/...

When opening the file via fstream you can choose between binary and text-format... http://cplusplus.com/doc/tutorial/files/
Hi Clover,

1
2
fstream file;
flile.open(file_name+".txt") 


This code is not going to work a better alternative is -

1
2
3
4
5
6
7
8
9
10
11
12
13
string name; 
    string ext=".txt"; 
    string f_name;


cout << "enter file name: ";
getline (cin, name);
f_name=name+ext; // creating the name of the file

ofstream myfile(f_name.c_str()); // creating file

myfile<< " This is the file"; // this message will copy into the file

and the file will be there with desired name.
include cstdlib also. There will be better way then this also but it will also do.

Regards
the funny thins is: if the user now enters "myfile.txt"... the file ends being "myfile.txt.txt"...
Incubbus + 1

If you want to handle it properly, just appending ".txt" won't do.

Does the file have to be a .txt file? If not, you could do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
...
getfilename(f_name);
...
void getfilename(std::string& fname)
{
	/*
	 * Check for a file extension (iterate over the string and find the first
	 * '.'. We don't check fname[0] for a '.' because hidden files often
	 * begin with one).
	 */
	for (size_t i = fname.length(); i > 0; --i) {
		if (fname[i] == '.')
			break;
	}
	
	if (i >= 1) /* Then there were no '.'s in the string */
		fname += ".txt"; /* Append a default file extension */
	
	/*
	 * Note: We don't have to return anything because fname was passed by
	 * reference.
	 */
}
It is even more easy:

1
2
3
4
5
6
void getfilename(std::string& fname)
{
    if(fname.find(".", fname.length()-5) != fname.npos)
        return;
    fname.append(".txt");
}


(assuming that the ending starts 4 positions from the back, like ".txt" is 4 chars long)
Last edited on
What about when the filename is ".hidden_file"?
I suggest to assume that the user inputs the complete file name including the file extension.
if the filename is
1
2
3
".hidden_file"
 012345678
     s <- there we start so we dont care about the leading 

or just search for ".txt"... but that lacks the check for other extensions...
(i know this is not the most beautiful solution, but Afaik the usual files wont named like "..txt" or?^^... "if it is ".a.txt" then it´s fine - i hope :D)...

to blackcoders suggestion: clover leaf, this is fine, because You are able to open files without extensions, too...
Last edited on
Why start four characters from the start? For all we know, the system the program is running on allows filenames of length 0-n and the same for file extensions. The last '.' is the only one relevant to the file extension, hence why my loop went backwards.

You could do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Find the last '.'. If it is at the beginning or end of the string,
 * we append ".txt". Otherwise, we know a file extension already exists.
 * Examples:
 *
 * ".hiddenfile" -- attach .txt
 * "somefile." -- attach .txt
 * "somefile.ext" -- ignore
 */
void getfilename(std::string& fname)
{
	size_t pos = fname.find_last_of(".");
	
	if (pos == 0 || pos == (fname.length() - 1))
		return;
	else
		fname += ".txt";
}
My apologies... I didn´t think about all the possibilities...

To be concerning the good old flag system: the leading dot can only be added by an preceding "\\?\" in the file path, right?...

edit: but somehow I can´t get it to work with fstream >.<...
Last edited on
I don't know about on windows but on UNIXes a '.' at the start of a file- or directory-name indicates that the file is hidden.
ah ok... did only think about the big W...^^...
clover leaf wrote:
Guys keep it simple I'm only a beginner.
What we showed you was as simple as it gets. (The stuff posted after that is a bit more advanced...)

Also, for file extensions, something that is very simplified:

1
2
3
4
5
6
7
8
9
10
11
12
13
string ExtractFilename( const string& path, char delimiter = '/' )
  {
  return path.substr( path.find_last_of( delimiter ) + 1 );
  }

string ExtractFileExtension( const string& path, char delimiter = '/' )
  {
  string filename = ExtractFilename( path, delimiters );
  string::size_type n = filename.find_last_of( '.' );
  if (n != string::npos)
    return filename.substr( n );
  return string();
  }

Make sure your paths are of the form: "D:/foo/bar/baz.txt". You can do that with a simple search and replace, something like:

 
#include <algorithm> 
1
2
3
4
5
6
string NormalizePath( const string& path )
  {
  string result( path );
  replace( path.begin(), path.end(), '\\', '/' );
  return result;
  }

Hope this helps.
Topic archived. No new replies allowed.