簡體   English   中英

如何讀取文件直到C中的一個字符

[英]How to read a file until a character in C

所以我有這個輸入文件:

1 2 3 4 5 6 7
3 2 4 1 6 5 7
***
1 1 2
1 1 2
***end of input***

我想掃描前兩行整數,然后用它們掃描,然后跳過*並掃描下一行整數,並對它們做一些事情(就像一個循環,直到它讀取* )。

我怎么能那樣做? 這是我的代碼:

int main(){
int i = 0, j ;
int temp[100];
char c, buf[20];
FILE *fp;
fp = fopen("input.txt", "r");
    if (fp != NULL){

            while (1 == fscanf(fp, "%d ", &temp[i])){
                i++;
            }   
                            // do something with the integers
    }
    else{
        printf("Cannot open File!\n");   
    }

return 0;
}

所以問題是,我只能掃描前兩行整數。 我也想掃描*之后的整數。

評論:

聽起來您需要 (a) 在“做某事”行之后添加代碼來處理包含星號的行(例如使用fgets()讀取它),然后在“做某事”和“咀嚼星星”代碼重復直到 EOF。 理想情況下,您應該在遇到 EOF 后關閉文件。 刪除格式字符串中%d之后的空格也可能是明智的——原因很復雜,但尾隨的空白尤其會給交互式程序帶來令人討厭的行為。

大綱:

if (fp != NULL)
{
    int c;
    do
    {
        i = 0;
        while (fscanf(fp, "%d", &temp[i]) == 1)
            i++;
        if (i > 0)  
            do_something(i, temp);
        while ((c = getc(fp)) != EOF && c != '\n')
            ;
    } while (c != EOF);
    fclose(fp);
}

我不經常使用do ... while循環,但它在這里工作正常,因為內部循環的主體不會做任何愚蠢的事情(比如假設沒有有效輸入時)。 如果有幾行連續的星星,代碼將正常工作,在它們之間什么都不做(因為i每次都為零)。

請注意,我沒有使用fgets()來讀取星號行,但可以這樣做:

if (fp != NULL)
{
    char line[4096];
    do
    {
        i = 0;
        while (fscanf(fp, "%d", &temp[i]) == 1)
            i++;
        if (i > 0)  
            do_something(i, temp);
    } while (fgets(line, sizeof(line), fp) != 0);
    fclose(fp);
}

示例代碼

無論使用上述兩種解決方案中的哪一種,代碼都以相同的方式處理示例數據:

#include <stdio.h>

static void do_something(int n, int *arr)
{
    printf("data (%d items):", n);
    for (int i = 0; i < n; i++)
        printf(" %d", arr[i]);
    putchar('\n');
}

int main(void)
{
    int i = 0;
    int temp[100];
    FILE *fp = fopen("input.txt", "r");
    if (fp != NULL)
    {
    char line[4096];
    do
    {
        i = 0;
        while (fscanf(fp, "%d", &temp[i]) == 1)
            i++;
        if (i > 0)  
            do_something(i, temp);
    } while (fgets(line, sizeof(line), fp) != 0);
    fclose(fp);
    }
    /*
    {
        int c;
        do
        {
            i = 0;
            while (fscanf(fp, "%d", &temp[i]) == 1)
                i++;
            if (i > 0)  
                do_something(i, temp);
            while ((c = getc(fp)) != EOF && c != '\n')
                ;
        } while (c != EOF);
        fclose(fp);
    }
    */
    else
    {
        printf("Cannot open File!\n");   
    }

    return 0;
}

輸出

data (14 items): 1 2 3 4 5 6 7 3 2 4 1 6 5 7
data (6 items): 1 1 2 1 1 2

也許你可以做這樣的事情(需要 ctype.h):

int ch;
/* read file char by char til its end */
while ((ch = fgetc(file)) != EOF)
{
  if (isdigit(ch))
  {
    /* char is a digit */
    /* do s.th. with it */
    printf("digit=%d\n", (char)ch);
  }
  else if (ch == '\n')
  {
    /* new line */
    /* count lines or do s.th. else */
  }
}

我不太確定,你的問題是什么。 也許這對你有點幫助。

編輯:您還可以使用簡單的 if(-else) 語句檢查每個字符,如果它是 '*'。

你做得很好,但唯一的問題是你沒有閱讀整個文件。 你可以通過做一個簡單的修改來實現這一點......

int main(){

    int i = 0, j;

    int temp[100];

    char c, buf[20];

    FILE *fp;

    fp = fopen("input.txt", "r");

    while ((c=getc(fp))!=EOF){  //here you are reading the whole file

        while (1 == fscanf(fp, "%d ", &temp[i])){
            i++;
        }   
        // do something with the integers
    }

    return 0;
}

暫無
暫無

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

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