简体   繁体   English

在C程序中将两个文件合并为一列

[英]Two files into one column in C Program

I am trying to figure it out why am I still getting error message when I did my gcc command on terminal and I have also both included for code and compiler as well. 我试图弄清楚为什么我在终端上执行gcc命令时仍然收到错误消息,并且代码和编译器也都包含了该消息。 Can anyone have any idea why or can help me for me? 谁能知道为什么或可以帮助我吗? I am really new to C Program this semester. 这个学期我真的是C程序的新手。 This is a main function that takes command line argument that opens two files and combines the two files one line at a time into one output. 这是一个主要功能,它使用命令行参数来打开两个文件,并将两个文件一次一行地组合成一个输出。 The first file are lines of text, but remove any trailing white spaces from the end of each line (newlines, tabs and spaces), and the second files are the list of numbers. 第一个文件是文本行,但是请删除每行末尾的所有空格(换行符,制表符和空格),第二个文件是数字列表。 So there should be two columns separated by a character. 因此,应该有两列用字符分隔。 I have them for example so you can visual for more clarification: 以我为例,您可以直观地进行进一步说明:

  Example for to output:
  ./p2 test/p2-testa test/p2-testb
  Test A  11
  Test B  51
  Test C  91
  Test D  26
  Test E  17
  Test F  76


/* 3 point */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

const int MAXLEN = 4096;
const int MAXLINES = 10;

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

  char buffer[MAXLEN];
  char buffer2[MAXLEN];
  FILE *fp = fopen(argv[1], "r");
  FILE *fp2 = fopen(argv[2], "r");

  if (!(fp && fp2)) {
    perror ("Not Found");
    exit (EXIT_FAILURE);
  }

  int n = 0;
  while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
    printf("%s\t%s", buffer, buffer2);
    n++;
  }

  fclose((fp) && (fp2));    
  return (0);

}

ERROR COMPILE MESSAGE (BTW: For lecture, I used labcheck by instructor): 错误编译消息(顺便说一句:对于授课,我使用了labcheck ):

p2:
p2.c: In function ‘main’:
p2.c:52:19: warning: passing argument 1 of ‘fclose’ makes pointer from integer without a cast [-Wint-conversion]
       fclose((fp) && (fp2));
              ~~~~~^~~~~~~~
In file included from p2.c:2:
/usr/include/stdio.h:199:26: note: expected ‘FILE *’ {aka ‘struct _IO_FILE *’} but argument is of type ‘int’
 extern int fclose (FILE *__stream);
                    ~~~~~~^~~~~~~~
-3.0 output of program (p2) is not correct for input '/u1/h7/CS151/.check/text/list.1 /u1/h7/CS151/.check/nums/tiny.1':
------ Yours: ------
---- Reference: ----
Line A  6
Line B  41
Line C  52
Line D  3
Line E  36
Line F  61
--------------------

I didn't really understand the warning and expected message in C program. 我不太了解C程序中的警告和预期消息。

Expression (fp) && (fp2) passed to fclose combines two pointers by operator && , which expects integral operands and interprets them as beeing either ==0 or !=0 . 传递给fclose表达式(fp) && (fp2)由运算符&&组合两个指针,该指针期望整数操作数并将其解释为==0!=0 The result is an integral value, which again is either ==0 or !=0 , but it has nothing to do any more with the pointers fclose expects. 结果是一个整数值,该值还是==0!=0 ,但它与fclose期望的指针无关。

So fclose((fp) && (fp2)) should be 所以fclose((fp) && (fp2))应该是

fclose(fp);
fclose(fp2);

your program should look exactly like this 您的程序应该看起来像这样

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

const int MAXLEN = 4096;
const int MAXLINES = 10;

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

  char buffer[MAXLEN];
  char buffer2[MAXLEN];
  FILE *fp = fopen(argv[1], "r");
  FILE *fp2 = fopen(argv[2], "r");

  if (!(fp && fp2)) {
    perror ("Not Found");
    exit (EXIT_FAILURE);
    }

     int n = 0;
       while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
           printf("%s\t%s", buffer, buffer2);
               n++;

}
      fclose(fp);
      fclose(fp2);

      return (0);

      }

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

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