2013-11-24 20 views
0

這是某人的程序,我需要爲我的項目收集數據。我無法理解指令#define IMAGE argv[1]#define IFS argv[2]指向的位置以及IMAGE和IFS中定義的值。這兩個命令是必要的,因爲它們稍後需要讀取文件名。無法理解argv [1]和arg [2]命令在這個程序中的位置

#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); 
} 

回答

1

argc和argv指的是命令行輸入。程序運行時,它們由用戶指定。

myprogram.exe --input1 fred

看到這個:What does int argc, char *argv[] mean?

+1

順便說一句,使用'#define'這絕對是可怕的。他們可以廉價地使用'char * IMAGE = argv [1],IFS = argv [2]'將其定義爲局部變量。我會建議OP,您修改代碼並執行此操作。 –

+0

對不起,我是初學者,我不知道如何指定它。請給我解釋一下如何做到這一點?我在eclipse中運行這個程序,每次運行它時 - 我都會得到輸出以文件格式進行修正。 – pinvpn

+0

@ChrisHayes是正確的,不是一個好主意。 – FaddishWorm