簡體   English   中英

打印文件c編碼的下一行

[英]printing the next line from file c coding

我的代碼是這個。 我正在讀取文件,比較用戶從文件中的數據輸入的單詞,然后想從文件中打印接下來的兩個單詞。 我怎樣才能做到這一點?

        #include <stdio.h>
        #include <string.h>
        int main()
        {
           //Open the file for reading
                    FILE *in = fopen("data.txt", "r");
           char str[]="file1.txt";
           //fgets buffer
           char buffer[100];

         //Pieces of string tokenized
        char * stringPiece;

         //int for comparing strings
           int compare=2;


        //While loop. Getting lines from file
        while ( fgets(buffer, sizeof(buffer), in) != NULL ){
             fgets(buffer, 100, in);
            // printf("%s\n", buffer);

         stringPiece = strtok (buffer,",");
             while (stringPiece != NULL){


          printf("%s\n",stringPiece);
          compare=strcmp(stringPiece,str);

          if (compare==0){printf("HELP");}

          //printf("%s\n",stringPiece);
          stringPiece = strtok (NULL, " ");
      }
   }
         //Close file
                fclose(in);

                       return 0;
}

我可以在文件中找到與用戶輸入的單詞相同的單詞,但無法從文件中打印接下來的兩個單詞。 文件名是data.txt。

代碼格式清晰( main()main()函數的大括號上使用vim=% )。

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

int main(void)
{
    //Open the file for reading
    FILE *in = fopen("data.txt", "r");
    char str[]="file1.txt";
    //fgets buffer
    char buffer[100];

    //Pieces of string tokenized
    char * stringPiece;

    //int for comparing strings
    int compare=2;

    //While loop. Getting lines from file
    while ( fgets(buffer, sizeof(buffer), in) != NULL ){
        fgets(buffer, 100, in);
        // printf("%s\n", buffer);

        stringPiece = strtok (buffer,",");
        while (stringPiece != NULL){

            printf("%s\n",stringPiece);
            compare=strcmp(stringPiece,str);

            if (compare==0){printf("HELP");}

            //printf("%s\n",stringPiece);
            stringPiece = strtok (NULL, " ");
        }
    }
    //Close file
    fclose(in);

    return 0;
}

查找單詞的樣本,接下來的兩個單詞得到

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

int main() {
    char userInputWord[]="file1.txt";
    char readFromFileData[]="c:,file1.txt,date,size";
    char *tokenPtr,*nextFirstWord,*nextSecondWord;

    tokenPtr=strtok(readFromFileData, ",");
    for(;tokenPtr != NULL;tokenPtr=strtok(NULL, ",")){
        if(0!=strcmp(tokenPtr, userInputWord))
            continue;//skip word
        //find it!
        printf("%s\n",tokenPtr);
        nextFirstWord = strtok(NULL, ",");
        nextSecondWord = strtok(NULL, ",");
        if(nextFirstWord)
            printf("first word is \"%s\"\n", nextFirstWord);
        else
            fprintf(stderr,"first word is nothing!\n");
        if(nextSecondWord)
            printf("second word is \"%s\"\n", nextSecondWord);
        else
            fprintf(stderr,"second word is nothing!\n");
    }
    return 0;
}

暫無
暫無

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

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