【问题标题】:Copying usernames and password from a file onto another in C在C中将用户名和密码从文件复制到另一个文件
【发布时间】:2018-07-18 04:24:50
【问题描述】:

我必须编写一个 c 程序,从两个不同的文件中复制用户名和密码,并将它们匹配到第三个文件中。我真的不知道如何解决这个问题。 username.txt 和 password.txt 中都有 20 个元素。我知道我可以使用 fread 和 fwrite 从文本文件中一次读取信息块。我尝试运行该程序,但它给了我无法打开文件的错误。我该如何解决这个问题?

#include <stdio.h>
#include <stdlib.h>
#define BUFF_SIZE 1000

int main()
{
    FILE *usernames, *passwords, *merged_file;
    char a, c;
    passwords = fopen("/Users/mcicco/Desktop/passwords.txt", "r");
    usernames = fopen("/Users/mcicco/Desktop/usernames.txt ", "r");
    merged_file = fopen("/Users/mcicco/Desktop/merged.txt" , "w");

    if (usernames == NULL || passwords ==NULL)
    {
        printf("Cannot open file \n");
        return (-1);
    }

    c = fgetc(usernames);
    a = fgetc(passwords);
    while (c != EOF && a!= EOF)
    {
        fputc(c, merged_file);
        c = fgetc(usernames);
        fputc(a, merged_file);
        a=fgetc(passwords);
    }

    fclose(usernames);
    fclose(passwords);
    fclose(merged_file);
    return 0;
}

【问题讨论】:

  • 请注意这里没有使用BUFF_SIZE,因此您可能根本没有分配缓冲区。除了微不足道的数据量之外,逐个字符地读取将非常缓慢。
  • 您可能应该从两个输入文件中读取 lines,然后将它们合并到输出文件中。
  • 简单的解释是文件名错误/拼写错误,或者文件不存在。
  • 分别对每个fopen调用进行错误检查,并使用perror而不是printf来打印错误消息。这会告诉你为什么调用失败(以及哪一个)。
  • 我发现了问题,fopen函数中有多余的空间。但是,文本文件没有被合并。

标签: c file text


【解决方案1】:

以下建议的代码:

  1. 干净编译
  2. 执行所需的功能
  3. 使用fgets()快速读取两个输入文件的每一行
  4. 使用strchr() 在输入缓冲区中查找任何换行符
  5. 使用简单的赋值将任何换行符替换为 NUL 字节
  6. 正确检查对fopen() 的调用是否存在错误
  7. 如果对 fopen() 的任何调用失败,则正确清理
  8. 使用#define BUFF_SIZE
  9. 考虑到问题的 cmets。
  10. 记录包含每个头文件的原因
  11. 遵循公理:每行只有一个语句,并且(最多)每个语句有一个变量声明。
  12. 通过分隔代码块(forifelsewhiledo...whileswitchcasedefault,通过单个空行来鼓励易于阅读和理解
  13. 假设两个输入文件包含相同的行数
  14. 假设对于包含用户名的行,另一个文件的同一行包含密码
  15. 假设输出文件的所需内容是:username password

现在,建议的代码。

#include <stdio.h>   // FILE, fopen(), fprintf(), fgets(), perror()
#include <stdlib.h>  // exit(), EXIT_FAILURE
#include <string.h>  // strchr()

#define BUFF_SIZE 1000

int main( void )
{
    FILE *usernames;
    FILE *passwords;
    FILE *merged_file;

    if( NULL == (passwords = fopen("/Users/mcicco/Desktop/passwords.txt", "r") ) )
    {
        perror( "fopen to read passwords.txt failed" );
        exit( EXIT_FAILURE );
    }

    if( NULL == (usernames = fopen("/Users/mcicco/Desktop/usernames.txt", "r") ) )
    {
        perror( "fopen to read usernames.txt failed" );
        fclose( passwords );
        exit( EXIT_FAILURE );
    }

    if( NULL == (merged_file = fopen("/Users/mcicco/Desktop/merged.txt" , "w") ) )
    {
        perror( "fopen to write merged.txt failed" );
        fclose( passwords );
        fclose( usernames );
        exit( EXIT_FAILURE );
    }

    char passwordsBuffer[ BUFF_SIZE ];
    char usernamesBuffer[ BUFF_SIZE ];

    while( fgets( passwordsBuffer, sizeof passwordsBuffer, passwords ) &&
           fgets( usernamesBuffer, sizeof usernamesBuffer, usernames ) )
    {
        char *newline;
        if( NULL != (newline = strchr( passwordsBuffer, '\n' ) ) )
        {
            *newline = '\0';
        }

        if( NULL != (newline = strchr( usernamesBuffer, '\n' ) ) )
        {
            *newline = '\0';
        }

        fprintf( merged_file, "%s %s\n", usernamesBuffer, passwordsBuffer );
    }

    fclose(usernames);
    fclose(passwords);
    fclose(merged_file);
    return 0;
}

【讨论】:

    【解决方案2】:

    一次读取一个字符太慢了。一次读一行,一次写一两行:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        FILE *passwords = fopen("/Users/mcicco/Desktop/passwords.txt", "r");
        FILE *usernames = fopen("/Users/mcicco/Desktop/usernames.txt ", "r");
        FILE *merged_file = fopen("/Users/mcicco/Desktop/merged.txt" , "w");
        if (usernames == NULL || passwords == NULL || merged_file == NULL)
        {
            printf("Cannot open file \n");
            return (-1);
        }
    
        char username[1024];
        char password[1024];
        while(1)
        {
            if(!fgets(username, sizeof(username), usernames))
                break;
            if(!fgets(password, sizeof(password), passwords))
                break;
            username[strcspn(username, "\n")] = 0;
            password[strcspn(password, "\n")] = 0;
            fprintf(merged_file, "%s\n%s\n", username, password);
        }
        fclose(usernames);
        fclose(passwords);
        fclose(merged_file);    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 2022-11-24
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多