简体   繁体   中英

How to store a word from file in a separate variable?

In this code, I want to select a word and save it in a separate variable. Same with all the words in a file. I have given the option (which I commented) to select any word from the file and when a user will select that word, it should be stored in a variable. How to do this?

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

int main(){
  FILE * fr = fopen("file.txt", "r");
  char string[100];

  if((fr = fopen("file.txt", "r")) == NULL){
    printf("Error! Opening file");
    exit(1);
  }
  while (fgets(string, 100, fr) != NULL){
    printf("%s", string);
    // printf("Write word to extract: ");
    // scanf("%s", ch);
    fclose(fr);
  }

I don't see the use of this as the user inputs the same word. But here you go.

Using scanf() for user input of word to look for. Using fgets() to get row from file into string. Using strtok() to step through string using the delimiters given in #define DELIMITERS .

The word scanned from the file is stored in saved_word .

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

#define DELIMITERS " ,.-_!?"
#define STRING_LEN 100

int main(){
    FILE * fr = NULL;
    char string[STRING_LEN]; // Buffer for row in file
    char input[STRING_LEN]; // Buffer for input word
    char saved_word[STRING_LEN]; // To save the word in

    memset(string, 0, sizeof string);
    memset(input, 0, sizeof input);
    memset(saved_word, 0, sizeof saved_word);

    fr = fopen("file.txt", "r");

    if (fr == NULL)
    {
        printf("Error! Opening file");
        exit(1);
    }

    char* word = NULL;

    /**
     * User input of word to look for within file
     */
    printf("string: %s\n", string);
    printf("Enter what word you want to find: ");
    scanf("%s", input);
    printf("\n");

    printf("Start scanning file.\n");
    while (fgets(string, STRING_LEN-1, fr) != NULL)
    {
        printf("Scanned row.\n");

        /**
         * Use strtok() to scan through the row, stored in string
         * Manual says to only have string input parameter on first call
         */
        word = strtok(string, DELIMITERS);
        
        int diff;
        while (word != NULL)
        {
            diff = strcmp(input, word);
            if (diff == 0)
            {
                // Matching words! 
                printf("Found the word: %s\n", word);
                strcpy(saved_word, word);
            }
            word = strtok(NULL, DELIMITERS);
        }
        
    }

    fclose(fr);
  }

I am not sure I understand what you wrote

Maybe you can read a word from the user and "extract" it from the file, a search.

For an user to be able to select a word from the file such word must be read into a variable. And for the program to be able to compare the words in the file against a word in a variable you must read the words in the file to another variable.

In your code you are opening and closing the file multiple times. Check it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM