Read a File - Line by Line - moving up/down

closed account (D3R4216C)
I am currently working on a text editor and i only want to display one line/row at a time, not the whole file. I als want to skip forwards and backness through from line to line in a textfile. Can someone please help me

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
char file_name[25];
string line;

cout << "\n\nName of File:" << endl;

cin >> file_name;

fstream sourcefile;

sourcefile.open(file_name, ios:: in | ios:: out | ios:: app);

if (!sourcefile)
{
cerr << "Could not open the specified File!\n" << endl;
}

else
{
cout << "File was opened successfully!\n" << endl;

while ( getline (sourcefile,line) )
{
cout << line << '\n';
}

sourcefile.close();
}
}
Last edited on
I would just read the entire file line by line into a vector. Do your editing/displaying using the vector (or other container). When finished, save the contents of the vector to the file.

closed account (D3R4216C)
I am trying to write a text editor with the capability to read a File Line by line. My aim is to read the file into a vector and then later in the Program i want to go up and down in the File, by reading just one line, the current one in the Vector.
I am currently new to C++ and I have been struggling quite a while with this task, would appreciate any form of help

#include <vector>
#include <iostream>
#include <iterator>
#include <fstream>
#include <algorithm>

using namespace std;

int main()

{
vector<string> zeilen_vektor;
zeilen_vektor.reserve(200);
char datei_name[25];
string zeile;

//Hauptmenü
cout << "\n\nGeben sie den Name der Datei ein:" << endl;

cin >> datei_name;

//Anfang der Dateioperationen
fstream quelldatei (datei_name);

if (quelldatei.is_open())
{
while (quelldatei >> zeile)
{
zeilen_vektor.push_back(zeile); // Datei wird ausgelesen in den
// Vektor hinein
}

quelldatei << zeilen_vektor.size() << ""; // Vektor speichern (optl)

copy(zeilen_vektor.begin(), zeilen_vektor.end(), ostream_iterator<string>(quelldatei, "")); // richtiges abspeichern

quelldatei.close(); // Datei wird geschlossen

//Lesen

quelldatei.open(datei_name, ios:: in); // Datei wird zum zum lesen geö
if (!quelldatei)
{
cerr << "Die Datei konnte nicht geöffnet werden!"; // Fehler Kontrolle
return (2);
}

zeilen_vektor.clear(); //Vector wird gelöscht


istream_iterator<string> begin(quelldatei); // Afng Iterator auf Datei
istream_iterator<string> end; // Ende des Iterators

copy(begin, end, back_inserter(zeilen_vektor)); // Datei erneut auslesen

copy(zeilen_vektor.begin(), zeilen_vektor.end(), ostream_iterator<string>(cout << ""));

}

else cout << "Unable to open file";

return 0;
}
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cctype>

std::vector<std::string> get_lines( std::string file_name )
{
    std::vector<std::string> lines ;

    std::ifstream file(file_name) ;
    std::string a_line ;
    while( std::getline( file, a_line ) ) lines.push_back(a_line) ;

    return lines ;
}

int main()
{
    const std::string file_name = __FILE__ ; // this file
    std::vector<std::string> lines = get_lines(file_name) ;

    if( !lines.empty() )
    {
        std::size_t current_line = 0 ;
        std::cout << "enter N for the next line, P for the previous line, Q to quit:\n\n" ;
        char command ;

        do
        {
            std::cout << current_line+1 << ". " << lines[current_line] << '\n' ;
            std::cin >> command ;
            command = std::tolower(command) ;
            std::cout << "\ncommand: '" << command << "'\n" ;
            switch(command)
            {
                case 'n' :
                    if( current_line < ( lines.size() - 1 ) ) ++current_line ;
                    else std::cout << "there are no more lines\n" ;
                    break ;

                case 'p' :
                    if( current_line > 0 ) --current_line ;
                    else std::cout << "there is no previous line\n" ;
                    break ;
            }
        }
        while( command != 'q' ) ;
    }
}

http://coliru.stacked-crooked.com/a/d8f97e40506e5c2b
Last edited on
closed account (D3R4216C)
Thank you, was very helpful! Is there a reason why some programmers type "std::" in front of every instruction and some just write "using namespace std" ?
> Is there a reason why some programmers type "std::" in front of every instruction

See: http://www.cplusplus.com/forum/general/72248/#msg385442
Topic archived. No new replies allowed.