-3

I have to do a task for university in C. The program creates images in PGM format.The program compiles successfully, however, I can't run the program correctly. At the beginning of the main function the if operator needs 2 argc, but every time I try to run the program I just have one argc and I get the result

"Error: missing or bad parameters, usage a3 [imagefile]"

The code is:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>

const double PI = 3.1415926535897932;

int** loadImage (const char *filename, int* width, int* height);
bool saveImage (const char *filename, int **pixels,
                const unsigned int width, const unsigned int height);

/* *** task (a) *** */

int normalize(int value) {
  return 0;    // Dummy return
}

/* *** task (b) *** */

int** createImage(unsigned int width, unsigned int height, int value){
  
  return NULL;    // Dummy return
}

/* *** task (c) *** */

void freeImage(int** pixels, unsigned int width, unsigned int height){
  
}

/* *** task (d) *** */

int** inverter (int **pixels, int width, int height)
{
  
  return NULL;    // Dummy return
}

/* *** task (e) *** */

int** laplace (int **pixels, int width, int height)
{
  
  return NULL;    // Dummy return
}

/* *** task (f) *** */

int** rotate (int** pixels, int width, int height, double angle, int background)
{

  return NULL;    // Dummy return
}

int main (int argc, char **argv)
{
  if (argc == 2)
  {
    int width=0, height=0;
    int **image = loadImage (argv[1], &width, &height);
    if (image != NULL)
    {
      int **image1 = inverter (image, width, height);
      int **image2 = laplace (image, width, height);
      int **image3 = rotate (image, width, height, 150, 128);

      saveImage ("image0.pgm", image,  width, height);
      saveImage ("image1.pgm", image1, width, height);
      saveImage ("image2.pgm", image2, width, height);
      saveImage ("image3.pgm", image3, width, height);

      freeImage (image, width, height);
      freeImage (image1, width, height);
      freeImage (image2, width, height);
      freeImage (image3, width, height);
    }
  }
  else {
    printf ("Error: missing or bad parameters, usage a3 [imagefile]\n");
  }
  return 0;
}

/* *** Bild aus Datei laden *** */
int** loadImage (const char *filename, int* width, int* height)
{
  const char *delimiter = " \n\r\t";
  FILE *file = fopen (filename, "rt");
  int** pixels;
  bool isValid = true;

  if (file != NULL)
  {
    char line [80];
    int n = 0, params [3];

    if (fgets (line, 80, file) == NULL) isValid = false;
    if (strlen (line) != 3 || strncmp (line, "P2", 2)) isValid = false;
    while (isValid && fgets (line, 80, file) != NULL)
    {
      if (strlen (line) <= 71)
      {
        if (line[0] == '#') continue;
        char *p = strtok (line, delimiter);
        while (isValid && p != NULL)
        {
          if (n < 3)
          {
            int a = atoi (p);
            if (a <= 0) {isValid = false;}
            else {params[n++] = a;}
          }
          else
          {
            if (n == 3)
            {
              if (params[2] != 255) {isValid = false;}
              else {
                pixels = createImage (params[0], params[1], 0);
                *width = params[0];
                *height = params[1];
              }
            }
            if (n-3 < *width * *height)
            {
              int x = (n-3) % *width;
              int y = (n-3) / *width;
              int value = atoi (p);
              pixels[y][x] = normalize(value);
              n++;
            }
            else {isValid = false;}
          }
          p = strtok (NULL, delimiter);
        }
      }
      else { isValid = false;}
    }
    fclose (file);
    if (n-3 != *width * *height) {isValid = false;}
    if (!isValid) freeImage (pixels, *width, *height);
  }
  return isValid ? pixels : NULL;
}

/* *** Bild in Datei speichern *** */

bool saveImage (const char *filename, int **pixels, unsigned int width, unsigned int height)
{
  FILE *file = fopen (filename, "wt");

  if (file != NULL && pixels != NULL)
  {
    fprintf (file, "P2\n");
    fprintf (file, "%d %d\n", width, height);
    fprintf (file, "%d\n", 255);
    for (int y = 0; y < height; y++)
    {
      for (int x = 0; x < width; x++)
      {
        fprintf (file, "%d\n", pixels[y][x]);
      }
    }
    fclose (file);
    return true;
  }
  return false;
}

The professor also said we should add following argument to the task in VSCode:

"args": [
"-g",
"-std=c99",
"-Wall",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lm"
],
Darkess
  • 3
  • 1
  • What value is `argc`? And how do you call the program? (i.e. the command line?) – Paul Ogilvie May 28 '21 at 10:31
  • I printed argc at the beginning of the main function and `argc = 1`. The professor said we should compile the program with `gcc -Wall -o a3 a2.c -lm` and then I simply run the program with `.\a2.exe` – Darkess May 28 '21 at 10:32
  • Add a for loop to print the number of arguments and their value. You'll probably understand what is wrong. – fpiette May 28 '21 at 10:32
  • And how do you call the program? (i.e. the command line?) – Paul Ogilvie May 28 '21 at 10:33
  • @fpiette I did and unfortunately it is still just one argument in total. – Darkess May 28 '21 at 10:36
  • 2
    COMMAND LINE PLEASE!! – Paul Ogilvie May 28 '21 at 10:37
  • If you run your program from VSCode, you must set the arguments. I don't know VSCode but I'm sure there is a menu item to set that. What you teacher told you is arguments for the compiler, not for the program when you start it from the IDE. – fpiette May 28 '21 at 10:40
  • Something like this: To run your project with command line arguments within Visual Studio: Right-click the default project (the one to be run) in Visual Studio and select "Properties". Click on the "Debug" tab on the left. Enter your command line arguments in the textbox labeled "Command line arguments". –  May 28 '21 at 10:43
  • @PaulOgilvie sorry what excatly do mean by this. I am new to programming. Sorry for the inconvenience. – Darkess May 28 '21 at 10:45
  • You musst provide an argument for that program to run. Some content for `argv[1]`. What do you provide. How do you call the compiled program? Really just .\a2.exe? That is obviously wrong because you explicitely request to get some extra string. – Gerhardh May 28 '21 at 10:48
  • Please show us the command line that you or VSC use to run the program, this is what Paul means. And you will see that the mandatory argument is missing. – the busybee May 28 '21 at 10:49
  • @Darkess Paul Ogilvie is asking you to open a command prompt (Windows) or a terminal window (Linux) to run your program. You'll then be sure to add the required argument. By the way: do you know what an program argument is? – fpiette May 28 '21 at 10:49
  • Till now I just did what is written in the exercise. I compile the program by `gcc -Wall -o a3 a2.c -lm` in the Terminal then a file called `a3.exe` gets created and I run this by `.\a3.exe`. In class we never learned anything else to run a program and there is nothing else written in the exercise. – Darkess May 28 '21 at 10:57
  • a3.exe **requires** an argument which is the file name. To run it, enter `.\a3.exe YourFilename`, replacing YourFilename with the actual file name you want to use. – fpiette May 28 '21 at 10:59
  • @fpiette Thank you! Now the images get created, but not correctly I guess. The code of image0 (the base image I have to edit with the funcions) is just `P2 \n 0 0 \n 255` and I cannot open it with any program – Darkess May 28 '21 at 11:05
  • 1
    Found out why the picture isn't working. Thank you all! – Darkess May 28 '21 at 11:15
  • @Darkess For more questions, please post a new question. Don't ask in the comment. – fpiette May 28 '21 at 11:17
  • `tasks.json` is for compiling and `launch.json` is for running/debugging, you have to add your run arguments to `launch.json` – rioV8 May 28 '21 at 14:40

1 Answers1

0

Your program a3.exe requires an argument which is the file name. To run it from command line, enter

.\a3.exe YourFilename

replacing YourFilename with the actual file name you want to use.

fpiette
  • 11,983
  • 1
  • 24
  • 46