文件交错,文件扫描,文件打印

时间:2014-04-04 19:13:18

标签: c linux file

编写一个程序,从中接收三个参数 命令行:week09_1 file1.txt file2.txt file3.txt

您的程序应该打开文件file1.txt和file2.txt进行阅读并创建
file file3.txt文件如下:

file3.txt的第一行是file1.txt的第一行 file3.txt的第二行是file2.txt的第一行 file3.txt的第三行是file1.txt的第二行 file3.txt的第四行是file2.txt的第二行

... 当一个输入文件到达EOF时,另一个文件中的剩余行为
应该复制到输出文件,程序终止。您
如果少于3个文件名,程序应打印相应的错误消息 在命令行上提供或无法打开文件。

截至目前,我已经能够做出提示要求我做的事情;但是,当一个文件用完要打印的行,然后它只从另一个文件打印时,打印在同一行,我想知道如何从1个文件打印时才能启动新行。

此外,我不明白我是如何在命令行参数中以提示的方式实现的。

#include <stdio.h>
char line1[256] = { };
char line2[256] = { };
char check;
int END = 0, END1 = 0, END2 = 0;

int main(int argc, char *argv[]) {
  if (argc != 4) {
    printf("Error: Wrong amount of arguments\n");
    return 0;
  }
  FILE *file1 = fopen("argv[1]", "r");
  FILE *file2 = fopen("argv[2]", "r");
  FILE *file3 = fopen("argv[3]", "w");
  if (argv == NULL) {
    printf("Error: file could not be opened.\n");
    return 0;
  }

  while (END != 2) {
    check = fgets(line1, 256, file1);
    if (check != NULL) {
      fprintf(file3, "%s", line1);
    } else if (check == NULL) {
      END1 = 1;
    }

    check = fgets(line2, 256, file2);
    if (check != NULL) {
      fprintf(file3, "%s", line2);
    } else if (check == NULL) {
      END2 = 1;
    }
    END = END1 + END2;
  }
  return 0;
}

3 个答案:

答案 0 :(得分:1)

我会让你在那里的一部分。

将此文件指定为file1.txt

File 1, Line 1
File 1, Line 2
File 1, Line 3
File 1, Line 4
File 1, Line 5
File 1, Line 6
File 1, Line 7
File 1, Line 8

此文件为file2.txt

File 2, Line 1
File 2, Line 2
File 2, Line 3
File 2, Line 4
File 2, Line 5
File 2, Line 6
File 2, Line 7
File 2, Line 8
File 2, Line 9
File 2, Line 10

您可以阅读它们并将其打印出来,如下所示:

#include <stdio.h>

int main(int argc, char *argv[]){
    char buffer[4096];
    char *ptr;
    int i=0;

    FILE *file1 = fopen("/tmp/file1.txt", "r");
    FILE *file2 = fopen("/tmp/file2.txt", "r");

    // while there is something from either file, do...
    while (!feof(file1) || !feof(file2)){
            // use modulo to switch file1, file2, etc
        if (i%2 == 0) {
            ptr=fgets(buffer, sizeof(buffer), file1);
        }   
        else {
            ptr=fgets(buffer, sizeof(buffer), file2);
        }
            // test if the last fgets actually read something by testing ptr vs NULL
        if (ptr)
            printf("%s", buffer);
        ++i;
    }   
    return 0;
}

打印:

File 1, Line 1
File 2, Line 1
File 1, Line 2
File 2, Line 2
File 1, Line 3
File 2, Line 3
File 1, Line 4
File 2, Line 4
File 1, Line 5
File 2, Line 5
File 1, Line 6
File 2, Line 6
File 1, Line 7
File 2, Line 7
File 1, Line 8
File 2, Line 8
File 2, Line 9
File 2, Line 10 

您需要将打印添加到文件3,文件上适当的fclose,读取argv以及相应的错误测试/反应。

答案 1 :(得分:1)

我认为代码更容易管理,特别是如果文件中的最后一行没有换行符,如果使用函数处理文件的一行,并重复调用它。

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

static int read_and_print_line(FILE *ifp, FILE *ofp)
{
  char buffer[64];
  int eol_needed = 0;
  while (fgets(buffer, sizeof(buffer), ifp) != 0)
  {
    fputs(buffer, ofp);
    if (strchr(buffer, '\n') != 0)
      return 0;
    /* Either more to read in current line or EOF without newline */
    eol_needed = 1;
  }
  if (eol_needed)
    putc('\n', ofp);
  return EOF;
}

int main(int argc, char **argv)
{
  if (argc != 4)
  {
    fprintf(stderr, "Usage: %s in-file1 in-file2 outfile\n", argv[0]);
    return 1;
  }
  FILE *if1 = fopen(argv[1], "r");
  FILE *if2 = fopen(argv[2], "r");
  FILE *ofp = fopen(argv[3], "w");
  if (if1 == 0)
    fprintf(stderr, "Failed to open %s for reading\n", argv[1]);
  if (if2 == 0)
    fprintf(stderr, "Failed to open %s for reading\n", argv[2]);
  if (ofp == 0)
    fprintf(stderr, "Failed to open %s for reading\n", argv[3]);
  if (if1 == 0 || if2 == 0 || ofp == 0)
    return 1;
  int r1 = 0;
  int r2 = 0;
  while ((r1 = read_and_print_line(if1, ofp)) != EOF &&
         (r2 = read_and_print_line(if2, ofp)) != EOF)
    ;

  assert(r1 == EOF || r2 == EOF);
  assert(r1 != r2);

  while (read_and_print_line(if1, ofp) != EOF)
    ;
  while (read_and_print_line(if2, ofp) != EOF)
    ;

  fclose(if1);
  fclose(if2);
  fclose(ofp);
  return 0;
}

我很满意这可以在它自己的源代码上工作,也可以在一个'行'文件上工作,最后没有换行符(echo -n Gobbledygook >file1),依此类推。缓冲区显示为64字节以帮助测试(如果您愿意,可以将其减少到32或甚至更低)。经过测试,我的编译大小为4096。

答案 2 :(得分:0)

一些UB代码需要修复

//char check;
void * check;

推荐:

char line1[256] = { 0 };
char line2[256] = { 0 };

更改错误检查。可以扩展哪个文件不好

if (file1 == NULL || file2 == NULL || file3 == NULL) {
  printf("Error: file could not be opened.\n");
  return 0;
}

但其他人有一个很好的“其余答案”。