Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:44:10+00:00 2026-06-04T04:44:10+00:00

My code is this. I am reading a file, comparing the word entered by

  • 0

My code is this. I am reading a file, comparing the word entered by user from data in file and then want to print the next two words from file. How can I do that?

        #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;
}

I can find the word in file same as what user entered but unable to print the next two words from file. File name is data.txt.

Code formatted to be legible (use vim and =% on the open brace of the main() function, mostly).

#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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:44:12+00:00Added an answer on June 4, 2026 at 4:44 am

    sample of find the word and next two words get

    #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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: This code actually codes from a tutorial book I'm reading at the moment,
I copy and paste code from this URL for creating and reading/writing a proc
This is what I'm trying to do: I'm reading a file in from the
I have this code in a Java EE Application for reading the Properties file.
I am reading this sample code from FUSE: http://fuse.sourceforge.net/helloworld.html And I am having trouble
I'm trying use this code to get image resolution from file bool GetImageSizeEx(const char
I'm reading this code sample : And since I don't know C#, I decided
i was reading this code, where setRegions is called after RootViewController is released :
I am reading On lisp and encountered this code (I simplified it a bit).
I was reading a blog where the writer said this Code doesn’t exist unless

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.