gpt4 book ai didi

c - 如何从 bash 终端获取输入到我的程序中

转载 作者:行者123 更新时间:2023-11-30 15:05:16 25 4
gpt4 key购买 nike

我的一项作业有以下问题:

One function (function #2) of your program is to read in the content of an instance file. To read in the file " instance10 001.txt " you will execute the command:

NameOfProgram -i instance10 001.txt

Here " -i " is the command-line option that indicates the succeeding argument is the input filename.

这是我到目前为止所做的,主要是一个骨架:

/* Assignment 1 */

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

int main(int argc, char *argv[])

{

FILE *fp;

int max_x, max_y, num_pt, rand_inst;
int *x_coordinate, *y_coordinate;

int inputfile = 0, outputfile = 0;
int i;

if (argc == 1)
{
/* to generate random instances, accepting parameters from stdin */
printf("Generating random instances...");
printf("Enter the circuit board size MAX_X MAX_Y: ");
scanf("%d %d", &max_x, &max_y);
printf("Enter the number of points NUM_PT: ");
scanf("%d", &num_pt);
printf("Enter the number of random instances to be generated: ");
scanf("%d", &rand_inst);
return 1;
}
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-i") == 0)
inputfile = i+1;
else if (strcmp (argv[i], "-o") == 0)
outputfile = i+1;
}
if (inputfile == 0)
{
/* invalid comman line options */
printf("\nIncorrect command-line\n");
printf("myprogram [-i inputfile [-o outputfile]]");
return -1;
}

**/* THIS IS WHERE I NEED HELP */**
if (inputfile == 1)
{
fp = fopen(/*Name of the input file (instance10_001.txt) */, "r")
}


if ((fp = fopen(argv[inputfile], "r")) == NULL)
{
/* open file error */
return -2;
}
while (fscanf(fp, "%d", &max_x) != 1)
{
if (ferror(fp))
{
/* read error */
fclose(fp);
return -3;
}
if (feof(fp))
{
/* no integer to read */
fclose(fp);
return -4;
}
fscanf(, "%*[^\n]"); /*skip the rest of line */
}
if (fscanf(fp, "%d", &max_y) != 1)
{
/* max_y not following max_x */
fclose(fp);
return -5;
}
while (fscanf(fp, "%d", &num_pt) != 1)
{
if(ferror(fp))
{
/* read error */
fclose(fp);
return -6;
}
if (feof(fp))
{
/* no integer to read */
fclose(fp);
return -7;
}
fscanf(fp, "%*[^\n]"); /* skip the rest of line */
}

x_coordinate = (int *)malloc(num_pt * sizeof(int));
y_coordinate = (int *)malloc(num_pt * sizeof(int));
for (i = 0; i < num_pt; i++)
{
while (fscanf(fp, "%d", &x_coordinate[i]) != 1)
{
if (ferror(fp))
{
/* read error */
fclose(fp);
return -8;
}


if (feof(fp))
{
/* no integer to read */
fclose(fp);
return -9;
}
fscanf(fp, "%*[^\n]"); /* skip the rest of line */
}
if (fscanf(fp, "%d", &y_coordinate[i]) != 1)
{
/* y_coordinate not following x_coordinate */
fclose(fp);
return -10;
}
}
fclose(fp);
if (outputfile > 0)
{
if ((fp = fopen(argv[outputfile], "w")) == NULL)
{
/* open file error */
return -2;
}
fprintf(fp, "##################################################\n");
fprintf(fp, "#%s\n", argv[inputfile]);
fprintf(fp, "#area [0, MAX_X] x [0, MAX_Y]\n");
fprintf(fp, "%d\t%d\n", max_x, max_y);
fprintf(fp, "#number of points NUM_PT\n");
fprintf(fp, "%d\n", num_pt);
fprintf(fp, "#coordinates\n");
for (i = 0; i < num_pt; i++)
{
fprintf(fp, "%d\t%d\n", x_coordinate[i], y_coordinate[i]);
}
fprintf(fp, "#end of instance\n");
fclose(fp);
}
else
{
printf("##################################################\n");
printf("#%s\n", argv[inputfile]);
printf("#area [0, MAX_X] x [0, MAX_Y]\n");
printf("%d\t%d\n", max_x, max_y);
printf("#number of points NUM_PT\n");
printf("%d\n", num_pt);
printf("#coordinates\n");
for (i = 0; i < num_pt; i++)
{
printf("%d\t%d\n", x_coordinate[i], y_coordinate[i]);
}
printf("#end of instance\n");
}
free(x_coordinate);
free(y_coordinate);

return 0;
}

我想知道如何从 bash 终端读取输入文件的名称。我应该使用 scanf 吗?

如何获取用户输入的输入文件内容?例如,如果用户使用 ./myprogram -i instance10_001.txt 从 bash 运行我的程序,我如何在程序中打开输入的文件?

PS 我正在使用 Ubuntu 终端通过 ssh 访问我的实验室计算机。

语言:c99 ;编译器:gcc

最佳答案

这看起来像是 if 语句中的一个简单错误。你说的是当且仅当 inputfile 为 1 (这意味着 -o 必须是 argv[0])时,它才会打开 inputfile。

  if (inputfile == 0)
{
/* invalid command line options */
printf("\nIncorrect command-line\n");
printf("myprogram [-i inputfile [-o outputfile]]");
return -1;
}
else /* if inputfile is not equal to 0, then this will execute. */
{
fp = fopen(argv[inputfile], "r");
}

此外,这里还有另一个问题,即您将 fp 分配给一个函数,然后重新打开 fp 中已打开的文件:

/* removed fp = fopen (a function) */
if (fp == NULL) /* You already opened the file; no need to open again until fclose */
{
/* open file error */
return -2;
}

此外,在这段代码中:

      while (fscanf(fp, "%d", &x_coordinate[i]) != 1) 
{
if (ferror(fp))
{
/* read error */
fclose(fp);
return -8;
}


if (feof(fp))
{
/* no integer to read */
fclose(fp);
return -9;
}
fscanf(fp, "%*[^\n]"); /* skip the rest of line */
}

Fscanf 返回成功填充的参数数量,在本例中始终为 1。
请注意,类似的问题可能存在于代码的其余部分中。

关于c - 如何从 bash 终端获取输入到我的程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39939921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com