我试图弄清楚为什么我在终端上执行gcc
命令时仍然收到错误消息,并且代码和编译器也都包含了。谁能知道为什么或可以帮助我吗?这个学期我真的是C程序的新手。这是一个主要功能,它使用命令行参数来打开两个文件,并将两个文件一次一行地组合成一个输出。第一个文件是文本行,但是请删除每行末尾的所有空格(换行符,制表符和空格),第二个文件是数字列表。因此,应该有两列用字符分隔。例如,我有它们,以便您可以直观地进行进一步说明:
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);
}
错误编译消息(顺便说一句,在授课中,我使用了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
--------------------
我不太了解C程序中的警告和预期消息。
答案 0 :(得分:4)
传递给(fp) && (fp2)
的表达式fclose
结合了运算符&&
的两个指针,该运算符期望整数操作数并将其解释为==0
或!=0
。结果是一个整数值,该值还是==0
或!=0
,但它与指针fclose
所期望的指针无关。
所以fclose((fp) && (fp2))
应该是
fclose(fp);
fclose(fp2);
答案 1 :(得分:-2)
您的程序应该看起来像这样
#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);
}