简体   繁体   English

无法将文本文件中的行读入字符串

[英]Unable to read line from text file into string

I have a text file that looks something like this 我有一个看起来像这样的文本文件

user1|password1 user1 |密码1
user2|password2 user2 |密码2

I have a string, say "user1|password1". 我有一个字符串,说“ user1 | password1”。 I need to compare each line of the text file with this string. 我需要将文本文件的每一行与此字符串进行比较。

    FILE *fp;
    fp = fopen("user_info_file.txt", "r");
    while(fgets(str, 10, fp)!=NULL){
    n = write(newsockfd, "STR:", 4);
    n = write(newsockfd, str, 15);}

And then go on to strcmp. 然后继续进行strcmp。 I use the write function because I'm having a server write this to a client. 我使用写功能,因为我正在服务器上将其写到客户端。

I've just put 15 as a random value (it is within bounds) to see if any text is being read. 我只是将15作为随机值(在范围内)以查看是否正在读取任何文本。 But when it prints str, well str doesn't contain anything. 但是,当它显示str时,str不会包含任何内容。 It just prints as "STR:" and then I get my prompt back on the next line. 它只是打印为“ STR:”,然后在下一行返回提示。

Please tell me how to read the data in user_info_file.txt (at least the first line) into str. 请告诉我如何将user_info_file.txt中的数据(至少第一行)读入str。 I've spent hours on this but I just can't seem to get it. 我已经花了几个小时,但是我似乎还是不明白。 Newbie here, please bear with me. 新手在这里,请多多包涵。

EDIT: Basically I'm creating an IM application. 编辑:基本上我正在创建一个IM应用程序。 User registers by entering their username and password, which is sent to the server and the server stores it in user_info_file in the form I've written above. 用户通过输入其用户名和密码进行注册,该用户名和密码将被发送到服务器,服务器将其以我上面编写的形式存储在user_info_file中。 After a user registers, they need to be able to login. 用户注册后,他们需要能够登录。 They enter username and password, which again I send to the server. 他们输入用户名和密码,然后我再次将其发送到服务器。 I then need the server to compare this string with the existing entries in user_info_file to validate the login. 然后,我需要服务器将此字符串与user_info_file中的现有条目进行比较,以验证登录名。 So I need the server to check every line of user_info_file and compare it to what the user enters. 因此,我需要服务器检查user_info_file的每一行并将其与用户输入的内容进行比较。

So I have 所以我有

char* str = new char[256];
    FILE *fp;
    fp = fopen("user_info_file.txt", "r");
    while(fgets(str, 10, fp)!=NULL){
    n = write(newsockfd, "STR:", 4);
    n = write(newsockfd, str, 15);}
    if(strcmp(buffer, str) == 0)
        n = write(newsockfd, "Logged in", 9);
    else
        n = write(newsockfd, "No such username", 16);

Basically I am trying to read each line of user_info_file into str. 基本上,我试图将user_info_file的每一行读入str。 Then I compare str after every read line with buffer. 然后,我将每条读取行后的str与缓冲区进行比较。 buffer stores the login info the client has sent. 缓冲区存储客户端已发送的登录信息。 If they are a match, then the login should be successful. 如果匹配,则登录应该成功。

But, when I write str, well it doesn't print anything. 但是,当我编写str时,它什么也不会打印。 15 is the number of characters from str it should write, correct? 15应该写的str中的字符数对吗? So even if the number was, say, 5, it should print at least "user1" or "user2" or the first 5 characters of SOME line of user_info_file. 因此,即使数字为5,它也应至少打印“ user1”或“ user2”或user_info_file的SOME行的前5个字符。 But it doesn't look like anything is getting read from the file into str at all. 但这看起来根本没有东西从文件读入str。

I hope I've made it clear. 我希望我已经说清楚了。 What can I do? 我能做什么? I'm at my wit's end. 我机智的尽头。

I don`t think that is is convenient in your case to read from file char by char using fgets function. 我认为使用fgets函数逐字符读取文件char并不方便。 I propose you to use fscanf , which offers you more oppotunities : like reading values of different formats at the same time. 我建议您使用fscanf ,它为您提供了更多机会 :就像同时读取不同格式的值一样。 Here I got some code snipet, to demonstrate how you can obtain desired information from file using fscanf: 在这里,我得到了一些代码片段,以演示如何使用fscanf从文件中获取所需的信息:

int main(void)
{
    char str[256];
    char checkLogin[256];
    char checkPassword[256];
    FILE *fp = fopen("user_info_file.txt", "r");
    /*while(fgets(str, 10, fp)!=NULL){
    n = write(newsockfd, "STR:", 4);
    n = write(newsockfd, str, 15);}*/
    int scanSucceed = 1;
    do
    {
        scanSucceed = fscanf(fp,"%s|%s ", checkLogin, checkPassword); 
        if (!strcmp(checkLogin, buffer))
        {
        // in case if password for that login match
            if (!strcmp(checkPassword, password))
            {
                ValidLogin = true;
                scanSucceed = -1; // exit from while loop
            }
        }
    }
    while(scanSucceed != -1); // while string was read
}

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

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