1

I have a C program here where a warehouse is managed. I want to use argc and argv in this code, because the program always expects the commands as call parameters, not as interactive input. And I want to be able to enter them as interactive input. How should it be changed? I am completely new to C or programming in general. sorry in the code some words are not in english

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

#define NAMELENGTH 100
#define DBSIZE 1000
#define DBNAME "database.bin"

struct Artikel
{
  char artikel[NAMELENGTH + 1];
  int anzahl;
};

struct Artikel db[DBSIZE];

void fehler(const char *text)
{
  fprintf(stderr, "Fehler: %s!\n", text);
  exit(1);
}

void check_artikel(char *artikel)
{
  if (!artikel || strlen(artikel) > NAMELENGTH)
    fehler("Artikel zu lang");
}

void db_load()
{
  FILE *input = fopen(DBNAME, "rb");
  if (!input)
    fehler("Datenbank zum Lesen öffnen fehlgeschlagen");

  size_t size = fread(db, 1, sizeof(db), input);
  if (size != sizeof(db))
    fehler("Datenbank nicht komplett geladen");

  fclose(input);
}

void db_save()
{
  FILE *output = fopen(DBNAME, "wb");
  if (!output)
    fehler("Datenbank zum Schreiben öffnen fehlgelschlagen");

  size_t size = fwrite(db, 1, sizeof(db), output);
  if (size != sizeof(db))
    fehler("Datenbank nicht komplett geschrieben");

  fclose(output);
}

void db_clear()
{
  for (int i = 0; i < DBSIZE; i++)
  {
    db[i].anzahl = 0;
  }
}

int db_size()
{
  int size = 0;
  for (int i = 0; i < DBSIZE; i++)
    size += db[i].anzahl ? 1 : 0;
  return size;
}

int db_find(char *artikel)
{
  for (int i = 0; i < DBSIZE; i++)
    if (db[i].anzahl > 0 && !strcasecmp(db[i].artikel, artikel))
      return i;

  return -1;
}

void db_add(char *artikel, int anzahl)
{
  int found = db_find(artikel);
  if (found >= 0)
  {
    db[found].anzahl += anzahl;
    return;
  }

  for (int i = 0; i < DBSIZE; i++)
    if (db[i].anzahl == 0)
    {
      strcpy(db[i].artikel, artikel);
      db[i].anzahl = anzahl;
      return;
    }

  fprintf(stderr, "Datenbank voll!\n");
}

void db_list()
{
  if (db_size() == 0)
  {
    printf("Datenbank ist leer.\n");
    return;
  }

  printf("Anzahl  :  Artikel\n");
  printf("--------:-------------------------------------------------------------\n");

  for (int i = 0; i < DBSIZE; i++)
    if (db[i].anzahl > 0)
    {
      printf(" %5d  :  %s\n", db[i].anzahl, db[i].artikel);
    }
}

int main(int argc, char **argv)
{
  if (argc < 2)
    fehler("Kein Kommando angegeben!\nGültige Kommandos: init, add ANZAHL ARTIKEL, list");

  const char *cmd = argv[1];

  if (!strcmp(cmd, "init"))
  {
    db_clear();
    db_save();
    printf("Datenbank initialisiert.\n");
  }
  else if (!strcmp(cmd, "list"))
  {
    db_load();
    db_list();
  }
  else if (!strcmp(cmd, "add"))
  {
    if (argc < 3)
      fehler("Keine Artikelanzahl angegeben");

    int anzahl = atoi(argv[2]);

    if (anzahl <= 0)
      fehler("Artikelanzahl zu klein");

    if (argc < 4)
      fehler("Kein Artikel angegeben");

    char *artikel = argv[3];

    check_artikel(artikel);

    if (argc > 4)
      fehler("Zu viele Eingaben");

    db_load();
    db_add(artikel, anzahl);
    db_save();
    printf("Artikel hinzugefügt.\n");
  }
  else
  {
    fprintf(stderr, "Unbekanntes Kommando!\n");

    return 1;
  }

  return 0;
}
dawio
  • 11
  • 1
  • 1
    If `argc < 2` there are no arguments, so ask the user to enter. Otherwise copy from them `argv`. – Weather Vane Dec 29 '20 at 15:43
  • Does this answer your question? [What does int argc, char \*argv\[\] mean?](https://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean) – Robert Harvey Dec 29 '20 at 16:04
  • A note on nameing variables and functions: Better stick to one language. And if posting to SO best use english. – alk Dec 29 '20 at 16:10
  • What have you tried so far? You could start with a [example] to replace `argc`/`argv` with interactive input from a user. Show us where you stumble. – the busybee Dec 29 '20 at 17:03
  • can someone run the code and send the link, because when i run the code i always get the same output – dawio Dec 29 '20 at 18:38

0 Answers0