0

I have a C code that opens and reads a text file with numbers then computes the area of a rectangle using those numbers. My Code is:

#include <stdio.h>
int main()
{
    FILE *ifile;
    float length, width;
    float maxarea = 0, maxlen, maxwidth;
    ifile = fopen("rectangles.txt", "r");
    while (feof(ifile) <= 0)
    {
        fscanf(ifile, "%f %f", &length, &width);
        if (length * width > maxarea)
        {
            maxarea = length * width;
            maxlen = length;
            maxwidth = width;
        }
    }
    printf("Maximum area is %f for rectangle with length %f and width %f",
        maxarea, maxlen, maxwidth);
    fclose(ifile);
    return(0);
}

When I debug it this shows up: enter image description here

When I retry it it shows this error: enter image description here

With the same code when I run it on a Linux Terminal it works and gives the right output. (recLarge is the executable file) enter image description here

How can I get the same output on Visual Studio 2017?

Serene
  • 59
  • 2
  • 8

2 Answers2

1

Edit your project settings and add any command-line options to Command Arguments:

Project Properties

Pay attention to the Working Directory setting.

Obviously, your main function is not currently set up for this. You need to add support for arguments:

int main(int argc, char **argv)
{
    if (argc <= 1)
    {
        fprintf(stderr, "Not enough arguments\n");
        return EXIT_FAILURE;
    }

    char *filename = argv[1];
    FILE *ifile = fopen(filename,"r");
    if (!file)
    {
        perror("Cannot open file");
        return EXIT_FAILURE;
    }

    // ...

    return EXIT_SUCCESS;
}
paddy
  • 60,864
  • 6
  • 61
  • 103
  • that was a corrected code provided by my professor tho. Is it impossible to compile it with the original code ? – Serene Mar 26 '19 at 01:26
  • No, you can use the other code. It's becoming difficult to interpret your question though. Maybe you need to be specific about exactly what it is you're trying to achieve. In the question that I was answering, it appeared that you were asking how to supply command-line arguments to a program that is run from the Visual Studio IDE with (or without) the debugger attached. – paddy Mar 26 '19 at 01:28
  • yes that's exactly it!! I know how to get the output from a linux terminal(I am using MobaXTerm) but don't know how to make it work on Visual Studio. I can't find any command line on Visual Studio. – Serene Mar 26 '19 at 01:48
  • 1
    You are not helping to clarify your position. _What specifically_ is "exactly it"? Did I interpret your question correctly? In that case, I answered it. If something about _your current program_ does not work for you, please clarify. You want to debug your program by running it from Visual Studio, so in that case, you can affect any command line options or starting directory from the Project Properties as I described. If you want to run it manually from the command line, then do that from the command prompt. Beyond this, I struggle to offer advice because your requirements are vague. – paddy Mar 26 '19 at 02:47
  • Since I couldn't explain it well I edited my original question and added screenshots. Does it maybe make my question a little more clear?? – Serene Mar 26 '19 at 03:46
  • Okay, so you're getting a NULL pointer exception because your `fopen` call failed and you did no error checking before using it. And so I go back to my statement about _working directory_. Your program assumes that `rectangles.txt` _exists_ in the working directory, and that no other application is blocking it from being opened with read access. One or more of these assumptions are false. – paddy Mar 26 '19 at 03:49
0

I needed to create the text file "rectangles.txt" under resources as a text file. The main C code will be under source. The dubugging for the code needs to have rectangles.txt as the command argument. Compile as should be Compile as C code(/TC).

enter image description here

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Serene
  • 59
  • 2
  • 8