简体   繁体   中英

How to search for specific lines that starts with a string in a file

I need help with this little program I am trying to make.

There is .txt file:

namas house
katinas cat
suo dog
skaicius number

I want to find a line which begins with specific word and then prints second word of that line. For example, user enters word katinas . Program looks through file, finds line that begins with katinas and finally prints.

What I have so far:

int main()
{
    char word;

    printf("Enter your word: ");
    scanf("%s", &word);

    FILE *fp;
    fp = fopen("data.txt", "r+");
    char buffer[256];
    while (fgets(buffer, sizeof buffer, fp) != NULL && atoi(buffer) != word)
    ;
    if (feof(fp))
    {
        printf(&buffer);
    }
    fclose(fp);

    return 0;
}

Thank you.

There were several mistakes in the code as pointed out elsewhere. This answer will find whole words so for example "suo" but not "su".

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

int main()
{
    char word[256];                 // adequate string space (not single char)
    char buffer[256];
    char *sptr;
    FILE *fp;
    int found = 0;

    printf("Enter your word: ");
    scanf("%s", word);              // requires a string pointer (note no &)

    fp = fopen("data.txt", "r");    // removed "+"
    if (fp) {
        while (fgets(buffer, sizeof buffer, fp) != NULL) {
            sptr = strtok(buffer, " \t\r\n");
            if (sptr && strcmp(sptr, word) == 0) {
                sptr = strtok(NULL, " \t\r\n");
                if (sptr) {
                    printf("%s\n", sptr);
                    found = 1;
                    break;
                }
            }
        }
        fclose(fp);
    }
    if (!found)
        printf ("%s not found\n", word);
    return 0;
}

You could use a simple logic, just like using a 'for' to search for the word and right after you find the specific word that you want, continue with the 'for' until find a blank space (use ' ' to determinete the end and the beginning of a word) then print out all the letters until find the next blank ' '. This should work just fine.

  1. char word; is a single char which can't store lines ; should be an array in order to read a line.

  2. atoi(buffer) != word you are comparing an int (returned by atoi() ) with a pointer(array name gets converted into a pointer here). I really don't see the purpose of this.

  3. You don't need to check if end of file has been reached. You just loop through the file line-by-line and check for your string.

  4. Calling printf without format specifier is not safe and you are also passing char (*)[256] whereas printf expects a const char* .

  5. You should also error checking on fopen() .

  6. Use a standard prototype for main() such as: int main(void)

After correcting all these, the code would be simplified to:

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

int main(void)
{
    char word[256] = {0};
    char buffer[256] = {0};
    FILE *fp;

    printf("Enter your word: ");
    scanf("%s", word);

    fp = fopen("data.txt", "r+");
    if (!fp) { /* error */}

    while ( fgets(buffer, sizeof buffer, fp) )
    { 
       if( strstr(buffer, word) == buffer )
       printf("Found: %s\n", buffer); 
    }

    return 0;
}

There's no library function to find if a string "starts with" in C. I am using strstr to achieve that. If there's a match and it's equal to the starting address of the buffer , then it starts with word . Because strstr returns to a pointer to the first match.

#include <stdio.h>

int main(void){
    char buffer[256];
    char word[256], second_word[256];
    char format[256];
    FILE *fp;

    printf("Enter your word: ");
    scanf("%255[A-Za-z]", word);
    sprintf(format, "%s %%255s", word);
    fp = fopen("data.txt", "r");
    while (fgets(buffer, sizeof buffer, fp) != NULL) {
        if (sscanf(buffer, format, second_word) == 1) {
            printf("%s\n", second_word);
            break;//continue;?
        }
    }
    fclose(fp);
    return 0;
}

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