1
#include <iostream>
#include <fstream>

using namespace std;

int main()
{

    fstream archivo("saludos.txt",ios::in|ios::out); // Abrir y  Leer
    char caract;
    int cont=0;
    while (!archivo.eof())
    {
        archivo.seekg(cont,ios::beg);
        caract=archivo.get();
        if (caract=='A')
        {
            archivo.seekp(cont,ios::beg);
            archivo<< 'O';
        }
        cont++;
    }
    archivo.close ();
}

i'm using this code but when i build and run it, nothing happens in the file saludos.txt There isn't even a response on the console application. anybody know why? i'm using codeblocks and i also have #include fstream

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 5
    First of all you haven't checked if the file opened successfully. Secondly, [don't use `eof` in a loop condition](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). Thirdly, you don't check for errors from the seek operations. Lastly, use a *debugger* to step through your code statement by statement to see what happens. – Some programmer dude Apr 27 '21 at 19:08

1 Answers1

0

I tried your code in my environment and it works just fine, it replace all 'A' characters with '0'.

I think your problem is with Codeblocks, if you create a new project and want to use file with the relative path in your project, you have to go to Project -> Properties -> Build targets and change the "Executing Working Dir" to your project's folder.
Or you cant try with a absolute file name with full system path first.

  • what's the correct way to add the full system path to my code. I tried changing the "Executing Working Dir" to the folder but it didn't work... – ROBERTO VARGAS MONJE Apr 27 '21 at 19:32
  • it will create the .exe file at your project folder. you have to rebuild to get it. and with a simple code with Codeblocks, you cant just simply create a new .cpp file : File > New > File > select C/C++ source – Thọ Đỗ Xuân Apr 27 '21 at 20:01
  • 1
    somehow the code works now, I guess I had it saved in the wrong folder :v – ROBERTO VARGAS MONJE Apr 28 '21 at 13:33
  • Codeblock build your code to an execute file (.exe). When you change build directory target and build 1 more time, an exe file will be created at your target folder you set and that’s why it works now. And if you think this is helpful, you could accept my answer :) – Thọ Đỗ Xuân Apr 28 '21 at 14:20