简体   繁体   English

如何将用户输入与存储在文件中的字符串进行比较?

[英]How can I compare user input to string stored into file?

I gotta write a file-handing code which will compare the string received from the user to a string of char stored into a file in C.我必须编写一个文件处理代码,它将从用户收到的字符串与存储在 C 文件中的 char 字符串进行比较。 I've trying since yesterday.我从昨天开始尝试。 I have no clue on how to do that.我不知道该怎么做。 I tried many things, but nothing seems to work.我尝试了很多东西,但似乎没有任何效果。

//
//  2.c
//  IFTM Exercises
//
//  Created by Lelre Ferreira on 10/27/19.
//  Copyright © 2019 Lelre Ferreira. All rights reserved.
//

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

int main (int argc, const char * argv[]){

    FILE *file = fopen("file.txt", "w");
    char text[] = "hi hi hi hi hi hi"; //string to be allocated into the file
    char c, userInput[] = "hi"; // simulates the user input
    int i = 0, count = 0;



    if (file == NULL) {
        printf("Failure to create file.\n");
        exit(1);
    } else {
        for (i = 0; i < strlen(text); i++) {
            printf("Inserting: [%c]\n", text[i]);
            fputc(text[i], file);
        }
        printf("All done.\n");
    }
    fclose(file);


    fopen("file.txt", "r");
    while ((c = fgetc(file) != EOF)){
        fscanf(file,   "%s",   text);
        if (strcmp(userInput, text) == 0) {
            count++;
        }
    }
    fclose(file);



    printf("Many times present: %d\n", count);
    return 0;
}

My problem is... the space between every word of the string in the file, because I have to check if there's another word starting, for instance... (Hi Hi)... I thought about using something like this in my code but I did not work as well.我的问题是......文件中字符串的每个单词之间的空格,因为我必须检查是否有另一个单词开头,例如......(嗨嗨)......我想过在我的代码,但我没有工作。

while ((c = fgetc(file) != EOF)){ // While the end of the file does not happen keep running.
    if ((c = fgetc(file) != ' ')) { //If an blank space is found... I does not make any sense actually. I'm desesperated.
        strcmp(userInput, text){
            count++
        }
    }
}
while ((c = fgetc(file) != EOF)){ 
    if ((c = fgetc(file) != ' ')) { 
        strcmp(userInput, text){
            count++
        }
    }
}

There are three major problems here.这里存在三个主要问题。

First, c = fgetc(file) != EOF is the same as c = (fgetc(file) != EOF) which obviously is not what you want.首先, c = fgetc(file) != EOFc = (fgetc(file) != EOF)相同,这显然不是你想要的。 It should be (c = fgetc(file)) != EOF它应该是(c = fgetc(file)) != EOF

Second, the above statement has (provided you did not get an EOF) actually read a character.其次,上面的语句(假设你没有得到 EOF)实际上读取了一个字符。 So the if statement should be if(c != ' ')所以 if 语句应该是if(c != ' ')

Third, c needs to be declared as an int三、 c需要声明为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修改代码以接受字符串作为用户输入并使用 strcmp 与文本文件的内容进行比较然后删除该行? - How can I modify the code to accept string as user input and use strcmp to compare with the contents of the text file then delete that line? 无法正确比较文件中的字符串和用户输入 - Can't compare string in file and user input properly 如何将用户输入字符串与C文件中的字符串进行比较 - How to compare user input string with string in a file in C 如何通过管道输入用户输入的字符串? - How can I pipe a string that the user input? 如何将用户给定的输入(字符串)与Ubuntu中的/ etc / passwd文件进行比较 - how to compare user given input(string) to /etc/passwd file in ubuntu in c 如何在C编程中将用户输入与字符串进行while循环比较 - how to compare user input to a string for a while loop in c programming 如何将字符串(由用户输入)与文件中一行的第一个单词进行比较? - How would I compare a string (entered by the user) to the first word of a line in a file? 如何在C中使用strcmp比较用户输入和文本文件? - How to compare user input with text file using strcmp in C? 如何将getchar()输入与getchar()中的先前输入进行比较? - How can I compare the getchar() input with the previous input in getchar()? 如何将字符串与用户输入进行比较? - How to compare strings with user input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM