如何在一行中向左或向右移动光标?

时间:2020-09-12 15:49:14

标签: c++

我有这段代码可以逐行移动光标。现在,我要在特定行上向左或向右移动。我怎样才能做到这一点?我尝试了seekg(int num , ios::cur),但又下了一行。

#include <fstream>
#include <limits>
#include<iostream>
using namespace std;

std::fstream& GotoLine(std::fstream& file, unsigned int num) {
    string item;
    file.seekg(std::ios::beg);
   for (int i = 0; i < num - 1; ++i) {
        file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');   
    }
   file.seekg(3, ios::cur); ///so i will be able to print blabla in line 9 at bla.txt
    return file;
}

int main() {

    fstream file("bla.txt");

    GotoLine(file, 8);

    string line8;
    file >> line8;

    cout << line8;
    cin.get();
    return 0;
}

bla.txt

1.
2.
3.
4.
5.
6.
7.
8.
9. blabla
10.

0 个答案:

没有答案