我正在尝试比较C中的两个文件。我正在比较每个字符,直到它到达(。之后(我不关心文件的其余部分,直到它到达一个新行。一旦新行到达后我再次比较每个字符,直到它到达(。如果之前有差异(我希望程序让我知道文件是不同的。如果它们是相同的,我希望它迭代文件直到它达到了EOF。当文件完全匹配时,程序让我知道一切都很好但是getchar似乎在最后抓住了一些奇怪的角色(Ÿ),导致它告诉我没有最终的匹配。
另外。当我确定(似乎循环没有执行并且它仍然标记后的字符(并告诉我匹配是坏的,即使我认为我应该忽略它。我希望这是明确的,我期待着你可能有任何见解。谢谢!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *masterlist;
FILE *test;
char masterprc[12];
char testprc[12];
int i;
int j;
int n = 0;
//open authritative DB for proc calls
masterlist = fopen("masterlist","r");
//open strace of Shellcode
test = fopen("testprc","r");
//insures file properly opens
if (masterlist == NULL || test == NULL)
{
printf("A file did not open\n");
exit(0);
}else { //if files both open then begin check
do { //loop iterates through the files comparing
//values
j = getc(test);
i = getc(masterlist);
if (j == i) {
printf("good\n");
//putchar (j);
} else if (j == '(') {
while (j != '\n'){
//putchar (j);
j = getc(test);
i = getc(masterlist);
}
} else if (j != i) {
printf("no good\n");
}
} while (j != EOF);
}
fclose (masterlist);
fclose (test);
}
答案 0 :(得分:0)
不是为此编写C,而是可以在shell中执行:
rpl="cut -d '(' -f 1"
diff --brief <($rpl masterlist) <($rpl testprc)
我们的想法是在任何(
之前获取文本,然后使用标准工具区分内容。
答案 1 :(得分:0)
如果您在(
之前的两条线路相同但之后不同,那么您仍在运行支票。
主要是因为} else if (j == '(') {
一旦你(
上的j
,如果i
等于(
,你就应该一直运行到行尾。
您的代码没有这样做
让我们稍微调试你的代码。我已经添加了以下条件的案例:
// -------- CASE 1 ---------------
if (j == i) {
printf("good\n");
//putchar (j);
// -------- CASE 2 ---------------
} else if (j == '(') {
while (j != '\n'){
//putchar (j);
j = getc(test);
i = getc(masterlist);
}
// -------- CASE 3 ---------------
} else if (j != i) {
printf("no good\n");
}
} while (j != EOF);
输入:
master - &gt; 123(45
测试 - &gt; 123(54
j = 1
,i = 1
- &gt;案例1 - &gt; OK
j = 2
,i = 2
- &gt;案例1 - &gt; OK
j = 3
,i = 3
- &gt;案例1 - &gt;好的
在下一次运行中,这是你的罪魁祸首:
j = (
,i = (
- &gt;你想要案例2,但案件1被执行。
你想要案例2,继续阅读,但不会发生,因为案件1被调用,因为它在检查条件的情况2之前出现。
答案 2 :(得分:0)
将奇怪的字符EOF
打印为字符。你应该检查EOF
的两个文件,也可以检查两个文件是否为左边的parantheses。