0

Here is someone's program, which i need for my project to collect data. I'm unable to understand where is the command #define IMAGE argv[1] #define IFS argv[2] pointing to and what are the values being defined in the IMAGE and IFS. These two command are necessary as they are later on required to for reading a file name.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <alloc.h>

#define IMAGE argv[1]
#define IFS argv[2]

get_image(FILE *ifs_file, unsigned int *assumed_width_of_image, unsigned int
*assumed_height_of_image, unsigned int *width_of_image, unsigned int *height_of_image, char *argv[]);

unsigned int *image;

main(int argc, char *argv[])
{
unsigned char dom[4][4], d1[4][4], ran[8][8], lowest_error, sym, bestsym,         domain_whites,range_whites;
unsigned int assumed_width_of_image, assumed_height_of_image, width_of_image,
    height_of_image;
unsigned long int count, domx, domy, ranx, rany, bestranx;
time_t start_time;
FILE *ifs_file;

start_time = time(NULL);
if (argc != 3) {
    printf("\nUse the following format:\n\ncompress [image_file] [ifs_file]\n\n");
    exit
}
if ((ifs_file = fopen(IFS, "wb")) == NULL) {
    fprintf(stderr, "\nError opening file %s\n", IFS);
    exit(1);
}
get_image(ifs_file, &assumed_width_of_image, &assumed_height_of_image,   &width_of_image, &height_of_image, argv);

    get_image(FILE *ifs_file, unsigned int *assumed_width_of_image, unsigned int
   *assumed_height_of_image, unsigned int *width_of_image, unsigned int *height_of_image,    char *argv[])
   {
 FILE *image_file;
unsigned int buf[24], *header, size_of_header, extra_height;
unsigned long size_of_file, size_of_image;

if ((image_file = fopen(IMAGE, "rb")) == NULL) {
    fprintf(stderr, "\nCannot open file %s\n", IMAGE);
    exit(1);
}
if (fread(buf, sizeof(unsigned int), 24, image_file) != 24) {
    fprintf(stderr, "\nError reading file %s\n", IMAGE);
    exit(1);
}
if (buf[0] != 19778) {
    printf("%s is not a .BMP file", IMAGE);
    exit(0);
}
if (buf[23] != 2) {
    printf("%s is not a black and white image", IMAGE);
    exit(0);
}
pinvpn
  • 1
  • 4

1 Answers1

1

argc and argv refer to command line inputs. When the program is run they are specified by the user.

myprogram.exe --input1 fred

See this: What does int argc, char *argv[] mean?

Community
  • 1
  • 1
ddoor
  • 5,819
  • 9
  • 34
  • 41
  • 1
    Incidentally, using `#define` for this is absolutely awful. They could cheaply and much more safely be defined as local variables using `char* IMAGE = argv[1], IFS = argv[2]`. I would suggest to OP that you modify the code and do this. – Chris Hayes Nov 24 '13 at 07:45
  • sorry, i'm a beginner and i have no clue how to specify it.Can you please explain about how to do that? I'm running this program in eclipse and every time i run it - i get the output to make correction in the file format. – pinvpn Nov 24 '13 at 07:46
  • In eclipse they are called run arguments, check the preferences you should be able to add arguments at run time – ddoor Nov 24 '13 at 07:47
  • @pinvpn See this question: http://stackoverflow.com/questions/5217931/passing-arguments-to-a-c-program-in-eclipse – Chris Hayes Nov 24 '13 at 07:48
  • One more question - Can i run this program as .exe without using eclipse? If yes, can you help me with it? – pinvpn Nov 24 '13 at 07:48
  • I certainly hope so, otherwise computers would be very dependent on eclipse. I assume you are on Windows then? – ddoor Nov 24 '13 at 07:49
  • Right now i'm using xcode on mac. But if this is easier to run in windows on eclipse, i can run this on windows. – pinvpn Nov 24 '13 at 07:53
  • xcode has run arguments too, see preferences for the project. Or simply google it. "runtime arguments xcode" should do it – ddoor Nov 24 '13 at 07:59