Home > Mobile >  Replace any line with another string
Replace any line with another string

Time:12-28

I want to replace part of a specific line. I thought I could:

  1. find the line
  2. assign this line a temporary line (like target_line)
  3. change the part that I want over temporary line
  4. delete the original line and rewrite the temporary line in my text file

My one line contains: name, surname, position, age, perform, and jersey no. of a player. I want to change the "perform" number if the user wants to.

So I wrote this code:

void TeamSetFuncChangePerform(string nickname, int jersey) {
ifstream readForChanging;

readForChanging.open("formedteam.txt", ios::app);

string target_team = nickname;
int    target_player = jersey;
int jerseyno;
string line;
size_t isFound{};
string target_line;
string perform;
int answer;


if (readForChanging.is_open()) {
    while (!(readForChanging.eof())) {
        getline(readForChanging, line);
        isFound = (line.find(target_team));
        if (isFound != string::npos) {
            isFound = 1;
            break;
        }
        else {
            isFound = 0;
        }
    }

I found the target team in file with the above code, and I tried to find the target player with the following code:

if (isFound == 1) {
    for (int i = 0; i < 11; i  ) {
        getline(readForChanging, line);
        isFound = (line.find(target_player));
        if (isFound != string::npos) {
            target_line = line;
            cout << "enter performans value between 30-100";
            cin >> perform;
            target_line[61] = perform[0];
            target_line[62] = perform[1];

            cout << "changed the performance value of player #" << target_player << endl;

If target player was found, then I am getting input for the new "perform" value. Now I have to replace the new "perform" value with the old one. I searched replace() method also, but I didn't understand how I could do it with replace(). So, the question is how can I change the specific substring and rewrite it into the file?

Here is a sample of my text file. For example, how can I change the Benjamin Blanchet's "perform" value with a new one?

name surname             position                 age       perform    jersey no
team: tr-trabzonspor
Leon Schmidt             santrafor                31        55         88        
Leon Schmidt             kanat forvet             32        81         96        
Robin Perez              kanat forvet             29        40         2         
Robin Perez              ofansif ortasaha         20        69         68        
Howl Pendragon           ofansif ortasaha         27        95         57        
Benjamin Blanchet        ortasaha                 19        78         54        
Yves Russo               ortasaha                 24        87         40        
Robin Perez              sagbek                   23        79         4         
Hans Weber               solbek                   36        57         89        
Hans Weber               defans                   18        43         21        
William Dupont           kaleci                   35        83         83        

CodePudding user response:

Consider if you want a quick&dirty solution, or a reusable solution that works on any kind of data.

I would choose the second option, split the code in sub-problems:

  1. Read the file into memory if less than 1000000 lines.
  2. Decode the header, what columns do I have and what width for each
  3. Decode the rest of the file, does each line have a correct syntax? Use the syntax of the header line. Maybe find a regular expression library (regex) for this to avoid difficult spaces/tabs, upper case/small case.
  4. Run your query on the data, replace whatever you want.
  5. Write data back to the (same) file. Only if least one is changed.

CodePudding user response:

What I want to suggest is a dirty trick that normally should be avoided. Only if your files will be really large, it may worth to look at. But there is a precondition for this trick:

Length of all lines should be similar in your file which means you may need to pad each line with additional blanks to reach a prefixed size. In addition, each part of line need to have a maximum fixed size.

So let's assume you have designed your file line to be 80 chars like this:

|----    25 chars   ----||----    25 chars   ----||-- 10 --||--  10  --||--  10 --|
Leon Schmidt             santrafor                31        55         88         \n

But if you have a file that is like this, you can manually calculate starting byte number of each line. So if you want to change 10th line, the starting byte will be (10 2)*80=960 (2 added for 2 lines of header). Then you can open your file as binary, goto 960th byte with fseek(), and update 80 byte in that line. In this case you can change file with minimum overhead.

But as I said, this is a dirty trick and it should be avoided unless it is completely necessary.

  •  Tags:  
  • c
  • Related