【问题标题】:Unable to understand where argv[1] and arg [2] command are pointing in this program无法理解 argv[1] 和 arg [2] 命令在此程序中指向的位置
【发布时间】:2013-11-24 07:40:47
【问题描述】:

这是某人的程序,我的项目需要它来收集数据。我无法理解命令#define IMAGE argv[1]#define IFS argv[2] 指向的位置以及在IMAGE 和IFS 中定义的值是什么。这两个命令是必要的,因为它们稍后会用于读取文件名。

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

【问题讨论】:

    标签: c


    【解决方案1】:

    argc 和 argv 指的是命令行输入。当程序运行时,它们由用户指定。

    myprogram.exe --input1 fred

    看到这个:What does int argc, char *argv[] mean?

    【讨论】:

    • 顺便说一句,使用#define 是非常糟糕的。可以使用char* IMAGE = argv[1], IFS = argv[2] 将它们更便宜且更安全地定义为局部变量。我建议 OP 修改代码并执行此操作。
    • 对不起,我是一个初学者,我不知道如何指定它。你能解释一下如何做到这一点吗?我在 Eclipse 中运行这个程序,每次运行它 - 我都会得到输出以纠正文件格式。
    • 在 Eclipse 中它们被称为运行参数,请检查您应该能够在运行时添加参数的首选项
    • @pinvpn 看到这个问题:stackoverflow.com/questions/5217931/…
    • 还有一个问题 - 我可以在不使用 eclipse 的情况下将这个程序作为 .exe 运行吗?如果是,你能帮我吗?
    猜你喜欢
    • 1970-01-01
    • 2020-09-29
    • 2011-03-27
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多