合并2个文件中的内容

时间:2016-10-18 16:54:22

标签: c loops

我想要合并2个文件中的内容。但我无法做到。

我的输出似乎是

Username: admin
Password: password123

Username: admin
Password: password456

Username: admin
Password: password789

Username: admin
Password: p@ssw0rd

Username: admin123
Password: password123

但是我想这样输出

Username: admin
Password: password123

Username: admin
Password: password456

Username: admin
Password: password789

Username: admin
Password: p@ssw0rd

Username: admin123
Password: password123

Username: admin123
Password: password456

Username: admin123
Password: password789

Username: admin123
Password: p@ssw0rd

代码如下:

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

int main(void)
{
    FILE *fpUser, *fpPass;

    char user[100], pass[100];  
    fpUser = fopen("username.txt","r");
    fpPass = fopen("password.txt","r");

    if (fpUser==NULL || fpPass==NULL)
    {
             printf("Username or Password File Cannot Found\n");
             exit(0);
    } else {
        while(fgets(user , 100 , fpUser) != NULL)
        {   
            do
            {
                printf("Username: %s\n", user);
                printf("Password: %s\n", pass);
            }
            while (fgets(pass , 100 , fpPass) != NULL);     
        }

    }

    return 0;
}

2 个答案:

答案 0 :(得分:2)

你需要重新考虑你的循环。我想你想要这样的东西:

 while((fgets(user , 100 , fpUser) != NULL) &&
              (fgets(pass , 100 , fpPass) != NULL))
        {
            printf("Username: %s", user);
            printf("Password: %s\n", pass);
        }
    }

    return 0;
}

鉴于文件username.txt

admin
admin
admin123
admin123
user1
user2
user3

password.txt

password
badmin
x_234$yz00o@
n@@dles
p1ckL3z
sandW1ch3z

使用新循环程序输出:

Username: admin
Password: password

Username: admin
Password: badmin

Username: admin123
Password: x_234$yz00o@

Username: admin123
Password: n@@dles

Username: user1
Password: p1ckL3z

Username: user2
Password: sandW1ch3z

请注意,我已在第一次调用printf()时删除了换行符,因为fgets()会读取换行符并保留换行符。另请注意,程序在密码或用户名用完时会停止运行。

更新

@David C. Rankin建议代码应该处理输入中的空行。这是一个非常好的主意,在他提到它之前在我的脑海里含糊不清,但我太懒了,不能真正接受这个概念。以下是完成此操作的整个程序的修改版本。我在声明中添加了两个指向char的指针,以便从调用fgets()接收返回值。

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

int main(void) {
    FILE *fpUser, *fpPass;

    char *isUser, *isPass;
    char user[100], pass[100];
    fpUser = fopen("username.txt","r");
    fpPass = fopen("password.txt","r");

    if (fpUser==NULL || fpPass==NULL)
    {
        printf("Username or Password File Cannot Found\n");
        exit(0);
    } else do {
            while ((isUser = fgets(user, 100, fpUser)) != NULL &&
                   user[0] == '\n')
                continue;
            while ((isPass = fgets(pass, 100, fpPass)) != NULL &&
                   pass[0] == '\n')
                continue;
            if (isUser && isPass) {
                printf("Username: %s", user);
                printf("Password: %s\n", pass);
            }
        } while (isUser && isPass);

    return 0;
}

以下是文件username.txt

admin
admin




admin123
admin123
user1
user2






user3

password.txt

password
badmin

x_234$yz00o@
n@@dles
p1ckL3z


sandW1ch3z
anotherpass
longforapass

您无法在此处看到它,但password.txt文件以空行开头。这是新的输出:

Username: admin
Password: password

Username: admin
Password: badmin

Username: admin123
Password: x_234$yz00o@

Username: admin123
Password: n@@dles

Username: user1
Password: p1ckL3z

Username: user2
Password: sandW1ch3z

Username: user3
Password: anotherpass

答案 1 :(得分:0)

Postmaster,您可以关闭此案例。我找到了在内循环中使用rewind()的答案。