簡體   English   中英

如何從終端獲取文件名和int並將它們傳遞給ac程序以用作參數?

[英]How do I take a file name and an int from the terminal and pass them into a c program to be used as parameters?

我需要編寫一個程序,該程序從終端讀取給定的文本文件,並計算在文本文件中從命令行給定的數字出現的次數。

示例test.txt文件:

"here is your class CSC 1010"

運行程序:

$./countD test.txt 1
'1' appeared 2 times

試試這個代碼:

  1. 它通過int main(int
    argc,char* argv[])
    從main獲取命令行參數
    int main(int
    argc,char* argv[])
    int main(int
    argc,char* argv[])
    然后打開通過命令行參數獲取的文件。
  2. “ argc”給出了所需的參數數量,如果不符合要求,則給出錯誤。
  3. atoi將字符串形式的數字轉換為整數。
  4. getc()用於遍歷文件。
  5. fscanf從文件中獲取整數並將其存儲在num中。
  6. while循環用於獲取每個數字的位數並計算一個位數。

     #include<stdio.h> #include<stdlib.h> int main(int argc,char* argv[]) { int c=0; if(argc != 3) { printf("usage: ./a.out file number\\n"); return 1; } char* file = argv[1]; int n = atoi(argv[2]); FILE *inptr = fopen(file,"read"); if(inptr == NULL) { printf("error\\n"); return 2; } int num=0; int ch = getc(inptr); //to get to the end of file while(ch != EOF) { fscanf(inptr,"%d",&num);//scanning numbers one by one while(num!=0) //as it was given by you in your example { //you require the appearances of digit 1 int d = num%10; //1 can also appear multiple times in a if(d == n) //single number and hence i had to extract { //the digits of the number also c++; } num/=10; } ch = getc(inptr); } printf("'%d' appeared %d times\\n",n,c); fclose(inptr); return 0; 

    }

main功能的適當原型為:

int main (int argc, char **argv)

(您還將看到它用等效的char *argv[]編寫)

要么

int main (void)

第一種情況提供了一個整數參數計數argc ,其中包含傳遞給您程序的參數數量。 參數是指向argv中保存的字符串的索引和指針數組,其中最后一個之后的下一個條目是前哨 NULL 程序名稱始終是列表中的第一個參數(索引zero ),其次是在命令行上傳遞給程序的所有命令。

mainint main (void) )的第二種情況明確指出,沒有參數傳遞給程序。

在您的情況下,您只想采用兩個參數,(1)要讀取的文件名和(2)要搜索的字符,都可以很容易地處理。 第一個參數可以用於打開文件,第二個參數可以用作搜索字符以檢查文件中的每個字符。 您可以使用三元運算符為兩者提供默認值,例如

    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
    int c, srchc = argc > 2 ? *argv[2] : '1', n = 0;

上面, argv[1]用作文件名(或者默認為從stdin讀取, argv[2]的第一個字符是您的搜索字符。

接下來,您需要做的只是驗證文件是否已打開以進行讀取,例如

     if (!fp) {  /* validate file open for reading */
        fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
        return 1;
    }

然后循環遍歷文件中的每個字符,並記錄您的srchc與當前字符匹配的次數,例如

    while ((c = fgetc (fp)) != EOF)  /* read from file until EOF found */
        if (c == srchc)     /* compare srchc against each char in file */
            n++;

綜上所述,您可以執行以下操作:

#include <stdio.h>

int main (int argc, char **argv) {

    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
    int c, srchc = argc > 2 ? *argv[2] : '1', n = 0;

    if (!fp) {  /* validate file open for reading */
        fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
        return 1;
    }

    while ((c = fgetc (fp)) != EOF)  /* read from file until EOF found */
        if (c == srchc)     /* compare srchc against each char in file */
            n++;

    printf ("'%c' appeared %d times\n", srchc, n);

    return 0;
}

輸入文件示例

$ cat dat/cscfile.txt
here is your class CSC 1010

使用/輸出示例

$ ./bin/charinfile dat/cscfile.txt 1
'1' appeared 2 times

仔細檢查一下,如果您還有其他問題,請與我聯系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM