1

I'm printing a text file to the console with the following code:

bool Screen::GetLevelFile(string fileName, vector<string>& vecLevel)
{
    ifstream in(fileName.c_str());

    if (!in)
    {
        cerr << "Cannot open file: " + fileName << endl;
        return false;
    }

    string str;

    while (getline(in, str))
    {
        if (str.size() > 0)
        {
            vecLevel.push_back(str);
        }
        
    }
    in.close();


    return true;
}

Assume the textfile contains something like:

 #################################################   
 #  S                                            #   
 #                                               #   
 #                                               #   
 #                                               #   
 #                                               # 
 #                                               # 
 #                                               # 
 #                                               # 
 ################################################# 

Can I somehow find the x and y of 'S' in order to then replace it with '@' without using system("cls") and without editing the text file?

Edit: How I'm printing the file

void Screen::RenderScr()
{
    SetColor(FOREGROUND_RED | FOREGROUND_INTENSITY);

    string levelFile = "lvl1.txt";
    vector<string> levelVec;
    bool result = GetLevelFile(levelFile, levelVec);

    if (result)
    {
        //system("cls");
        cout << "\n\n" << endl;
        for (string &line : levelVec)
        {   
            cout << line << endl;
        }

    }
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 4
    Not in standard C++. There are platform specific libraries that will do this (ncurses and Win32 for example) but to make a recommendation we'd need to know what platform you are working on. – john Nov 24 '20 at 18:40
  • I don't think you need a console library to do this. The input here isn't the console, it's just some text file. `str` is just a string, which means you can do things like [find](https://en.cppreference.com/w/cpp/string/basic_string/find). – Nathan Pierson Nov 24 '20 at 18:44
  • @NathanPierson You might be right but I took the reference to `cls` as meaning that the above had already been printed and the OP wants to modify the existing console display. – john Nov 24 '20 at 18:46
  • Well, there's nothing in this code that prints anything anywhere. `vecLevel` looks like an output parameter that gets printed in some other code we don't see. If the goal is to modify the contents of the level between reading them in from the text file and printing the out to the screen, that's easy enough do to during the body of `GetLevelFile`. – Nathan Pierson Nov 24 '20 at 18:49
  • yes the text has already been printed to the console. I'm on windows also. You're correct, I have another function printing the text, I'll add it to my question for clarification. – Nerf_Herder42 Nov 24 '20 at 18:49
  • @Nerf_Herder42 Win32 has a [console API](https://learn.microsoft.com/en-us/windows/console/console-functions). However I'm not sure if it is compatible with standard C++ I/O,so you may need to change all your output to use the Win32 console API. – john Nov 24 '20 at 18:53
  • You need to change your approach: keep a map in memory and render that to screen instead of trying to read what your program put on the screen earlier. – Botje Nov 24 '20 at 18:54
  • @Botje I do have the map stored in a vector but I'm not sure how to actually find the 'S'. My reason for looking the X and Y is so i can then move the '@' (player character). Any suggestions on how to do this? – Nerf_Herder42 Nov 24 '20 at 18:57
  • So traverse your `levelVec` to find an `S` in it. – KamilCuk Nov 24 '20 at 19:01
  • I'll do that for the time being then come back to my issue with coordinates. – Nerf_Herder42 Nov 24 '20 at 19:04

0 Answers0