Problem "the file is already being used by another process"

Asked

Viewed 781 times

0

I’m trying to make a code to call windows function ping and use output stream to windows file, and while using the code that the program calls on windows console right, but by the program I have a log saying "the file is already being used by another process"

class Archive{
    string name;
public:
    string getName(){
        return this->name;
    }
    fstream archive;
    Archive(string name){
        this->name = name;
        archive.open(name.c_str(), ios::out | ios::app);
    }
};


void ping (Archive &A, string IP){
    string command = "ping -t -a  ";

    command.insert(command.size(), IP);
    command.insert(command.size(), "  > ");
    command.insert(command.size(), A.getName());

    A.archive << system(command.c_str());
    getchar();
}

int main()
{
    Archive A("Ping.txt");
    ping(A, "8.8.8.8");

}


Why is this mistake happening? How do I fix it? and how I would do better the sequence I did there

    command.insert(command.size(), IP);
    command.insert(command.size(), "  > ");
    command.insert(command.size(), A.getName());```?

1 answer

0

You are not saying a way in the instant of creation. When called by the console the program has as context the path in which it can create the file. However when the double click occurs the program does not have this context.

Solution:

Archive A("./Ping.txt");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.