Ask how to use a string read from a file as an if statement

Asked 2 years ago, Updated 2 years ago, 124 views

I'm going to write a code that separates the string I received from the txt file into if statements.

Yes)

This is how you create a variable and store the input string.

    char VecFun[10];

    fpInput = fopen("input.txt", "r");
    fgets(VecFun, sizeof(VecFun), fpInput);
    fclose(fpInput); 

And I want to distinguish between Add and Dot.

    if (VecFun == "Add") {
        Vector vectorO = vectorA.Add(vectorB);
        fpOutput = fopen("output.txt", "w");
        fprintf(fpOutput, "%.f %.f %.f", vectorO.x, vectorO.y, vectorO.z);
        fclose(fpOutput);
    }
    else if (VecFun == "Dot") {
        fpOutput = fopen("output.txt", "w");
        fprintf(fpOutput, "%.f", vectorA.DotProduct(vectorB));
        fclose(fpOutput);
    }
    else {
        printf("Invalid format". (%s)\n", VecFun);
    }

But the if statement doesn't work as I thought. Debugging with printf("%s", VecFun);

Here's the result. How do you get an if statement to work successfully?

c++ file-io

2022-09-20 10:53

1 Answers

Compare using strcmp function.


2022-09-20 10:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.