0

I'm trying to make a calculator which when run opens a txt file form a certain path, the user inputs the data and when the user closes said txt file the rest of the program runs. I have 0 ideea how to open files in c++ or how to detect whether said file is running or not.

#include <fstream>
//1.card's attacks stat
//2.the rest of the multipliers (the defensive calculations are much easier then the attack stat calculations so this is possible)
using namespace std;
double stats[16];
int main()
{
    //in theory what this does is opens the txt file, allows the user to enter the data and then closes.
    system("D:\atk stat\def stat v2\template.txt");
    ifstream fin("template.txt");
    //from this point on it's just the calculator stuff
    int n,i,finstat;
    double x;
    finstat=1;
    //reads the data
    n=0;
    while(fin>>x)
    {
        n++;
        if(x==0)
            stats[n]=1;
        else
            stats[n]=x;
    }
    //transforms all the buffs into multipliers
    for(i=2; i<=n; i++)
        if(stats[i]!=1)
            stats[i]=(stats[i]/100)+1;
    //multiplies everything to the cards DEF stat
    for(i=1; i<=n; i++)
    {
        finstat=finstat*stats[i];
    }
    //this just prints out the DEF stat
    cout<<finstat<<endl;
    return 0;
}

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Gomotianu
  • 1
  • 1
  • 1
    "Blocking" is the term you're looking for. You want your program to spawn a process and *block* until the subprocess exits. – user229044 Jun 24 '22 at 19:05
  • That's cool, but i have trouble with actually opening the file. I get "The filename, directory name, or volume label syntax is incorrect.". I think this is because the path has spaces, but i'm unsure. – Gomotianu Jun 24 '22 at 19:08
  • 1
    `system("D:\atk stat\def stat v2\template.txt");` the \ will need to be escaped or you need to use a raw string literal. I would expect your compiler to warn about this. – drescherjm Jun 24 '22 at 19:10
  • I'm using Windows 10 – Gomotianu Jun 24 '22 at 19:10
  • 3
    [How to wait for ShellExecute to run?](https://stackoverflow.com/a/17638969) – 001 Jun 24 '22 at 19:17
  • 4
    `system` doesn't do what you think it does. – john Jun 24 '22 at 19:18
  • `"D:\atk stat\def stat v2\template.txt"` -- If you don't fix this issue, then everything is moot. That string is not what you think it is. You have `D`, followed by `:`, followed by the ASCII BEL, followed by `t`, followed by `k`. – PaulMcKenzie Jun 24 '22 at 19:19
  • Within a string literal `\x` is interpreted [by the compiler] as special (e.g. `\n` is a newline or 0x0A). So, to prevent that interpretation, we need to "escape" the `\x` by replacing it with `\\x`. We need: `"D:\\atk stat\\def stat v2\\template.txt"` – Craig Estey Jun 24 '22 at 19:24
  • @CraigEstey now i get " 'D:\atk' is not recognized as an internal or external command, operable program or batch file." – Gomotianu Jun 24 '22 at 19:27
  • Now you are having problems with the spaces in your filename. Add `\"` at the beginning and end of the filename so the shell will see quotes. – Ben Voigt Jun 24 '22 at 19:30
  • After you've fixed the backslashes in the string, note that `system(string)` tries to invoke/execute the program. You [probably] can't execute a `.txt` file. I'd remove that. I presume you _just_ want to open the file. So, just put the full string into `ifstream fin("template.txt");` instead of just the file tail. – Craig Estey Jun 24 '22 at 19:30
  • @Gomotianu The function you are looking for (on Windows) is `ShellExecute` not `system`. The function will open the text file in the users preferred text editor. Once you have that figured out you can look at the answer suggested by Johnny Mopp for how to make your program wait until the text editor has been closed. This is all more complicated than you realised. – john Jun 24 '22 at 19:34
  • @john thx , i'm going to try my best.... – Gomotianu Jun 24 '22 at 19:35
  • You can't rely on ShellExecuteEx to always give you a process handle... – Anders Jun 24 '22 at 20:37

0 Answers0