2016-10-09 3 views
0

하나가 다음과 같은 질문 bash는 터미널에서 입력을 얻는 방법 :내 프로그램에 내 할당

한 기능 (기능 2) 프로그램의 인스턴스 파일의 내용을 읽어하는 것입니다 . 에서 파일을 읽으려면 " instance10 001.txt 는"당신은 명령을 실행합니다 :

NameOfProgram -i 여기 001.txt

instance10 " -i"를 명령 줄 옵션이된다 후속 인수가 입력 파일 이름임을 나타냅니다.

이것은 내가 지금까지 대부분의 골격을했을 것입니다 :

/* 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를 사용해야합니까?

사용자가 입력 파일로 입력 한 내용을 얻는 방법은 무엇입니까? 예를 들어, 사용자가 bash에서 내 프로그램을 ./myprogram -i instance10_001.txt으로 실행하는 경우, 내 프로그램에서 입력 된 파일을 열려면 어떻게해야합니까?

PS 내 SSH를 통해 랩 컴퓨터에 액세스 할 때 내 우분투 터미널을 사용하고 있습니다.

언어 : c99; 컴파일러 : gcc

답변

1

이것은 if 문에서 간단한 오류처럼 보입니다. 입력 파일이 1 인 경우에만 (즉, -o가 argv [0]이어야 함을 나타냄) inputfile을 엽니 다.

: 코드 블록에, 또한

/* 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; 
    } 

:

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에서 연 파일을 다시하는 여기에 또 다른 문제가있다

 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입니다.
비슷한 문제가 나머지 코드.

+0

첫 번째'if' 문은 사용자가'myprogram -i inputfile -o outputfile' 또는 그 반대로 올바른 명령 줄을 입력했는지 확인합니다. 사용자가'myprogram -z inputfile -o outputfile'을 입력하면 에러를 출력하고 종료합니다. –

+0

@ImtiazRaqib :'if (inputfile == 1) '을 입력하면 fopen은 inputfile == 1 인 경우에만 발생하며,이 경우 명령 행의 첫 번째 인수 여야합니다. In './a.out -i file', ./a.out은 인수 0, -i는 인수 1, file은 인수 2이므로 입력 파일은 2가됩니다. – id0

+0

나는 if 문을 변경하고 사용자가 입력 파일로 입력 한 내용을 어떻게 얻습니까? 예를 들어 사용자가'./myprogram -i instance10_001.txt'로 bash에서 내 프로그램을 실행하는 경우, 내 프로그램에서 입력 된 파일을 어떻게 열 수 있습니까? –

관련 문제