user editing output

Mar 17, 2012 at 6:31pm
im trying to allow users to openly edit the output of the program, for example.
i would output the contents of a file onto the consul, and then the user can edit those contents and then save the changes back into the file. how do i do that?
Mar 17, 2012 at 7:00pm
Do you mean the actual C++ source or contents of a text file?
Mar 17, 2012 at 7:39pm
the actual text in the file. so if the file had "HELLO" the user could delete the "ELLO" and replace it with "I" so the file now holds "HI".
btw it will be an html file not a text file.
Last edited on Mar 17, 2012 at 7:45pm
Mar 17, 2012 at 8:37pm
http://cplusplus.com/doc/tutorial/files/

much easier if it is a text file, I'm sure there are c++ libraries out there that could help with xml files if you prefer that, I have no idea about html.
Mar 18, 2012 at 4:17pm
http://cplusplus.com/doc/tutorial/files/

i know how to open and use files, i have written this up so far
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
#include <iostream>
#include <fstream>
#include <string> 

class savefile{
private:
    std::string filename;
    std::ofstream savefile; 
public:
   
    void savethefile(std::string fileinfo[]){
        std::string hold;
        std::cout << "Please enter the file name: ";
        std::cin >> filename;
        hold = filename;
        filename = "/Users/home/Desktop/dreamweaver/dreamweaver/"; 
        filename += hold; 
        filename += ".html";
        std::cout << "file location is: " << filename; 
        savefile.open(filename.c_str());
        for(int i = 0; i < fileinfo.length(); i++){
            savefile << fileinfo[i]; 
        }
        savefile.close(); 
        std::cout << "\nFile has been saved.";  
    }
    
};

int main(){
    savefile active_file;
    return 0;
}

just allows me to save a variable filename. i need to know to the have the text within the file be outputted by the program, allow the user to DIRECTLY edit the text, and save the changes preferably as they are made, but after save is fine.
Mar 18, 2012 at 4:44pm
Hmm.. here's a piece of code you may find useful. Its rather limited in the sense that you can only write and move backwards using backspace (it won't delete the keys, but you can do it yourself). This code outputs whatever is in the console in exact way. The APIs used can be consulted from MSDN. happy coding.

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
#include <iostream>
#include <windows.h>
#include <cctype>
#include <conio.h>
#include <cstdio>
using namespace std;

int main()
{
    cout << "This is test from file.. Modify it. Use backspace to return backwards";
    while (true)
    {
        if (kbhit())
        {
            char c = getche();
            if (c == '\r')
            {
                ungetch(c);
                break;
            }
        }
    }
    //Now read from the console
    CHAR Buffer[1024];
    memset(Buffer,0,1024);
    cout << endl<<endl;
    cout << "You entered \n";

    DWORD a;
    ReadConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),Buffer,(DWORD) 55,(COORD){0,0},&a);
    cout << Buffer;

}
Mar 18, 2012 at 4:58pm
¿You want to do a text editor?
You load your file in a buffer, show the buffer and edit the buffer.
At the end, you save the buffer.

To move around you may want to use ncurses
Or you may go the ed way.
Topic archived. No new replies allowed.