比较 char 文件描述符 c
comparing char off file descriptors c
我需要比较文件描述符中的 2 个字符。
我的文件 sample.txt 包括 "first"
我的文件 sample2.txt 包括 "firbbbbbbst"
我需要 return "not similar",但它总是 returns "similar" 并且从未真正将行读取到缓冲区。
int main(int argc, char **argv)
{
ssize_t ret_in, ret_out;
char buffer[BUF_SIZE];
char buffer2[BUF_SIZE];
int where=0;
int fileDes1;
int fileDes2;
fileDes1 = open("home/aviad/Desktop/comparing/in1/sample.txt", O_RDONLY);
fileDes2 = open("home/aviad/Desktop/comparing/in2/sample2.txt", O_RDONLY);
while((ret_in = fread (buffer, BUF_SIZE, 1, fileDes1)) > 0)
{
ret_out= fread (buffer, BUF_SIZE, 1, fileDes2);
if (buffer[where] == buffer2[where])
{
printf("%c", buffer[where]);
where++;
}
else {
printf("nor similar\n");
break;
}
}
printf("similar\n");
printf("%c", buffer[2]); //garbage
printf("%c", buffer2[2]); //garbage
return 0;
}
您可能需要这个:
免责声明:未经测试的代码可能无法运行甚至无法编译,但它至少应该给您一个想法。总之还有进步空间
int main(int argc, char **argv)
{
ssize_t ret_in, ret_out;
char buffer[BUF_SIZE];
char buffer2[BUF_SIZE];
int where=0;
int fileDes1;
int fileDes2;
int notsimilar = 0;
int i = 0;
fileDes1 = open("home/aviad/Desktop/comparing/in1/sample.txt", O_RDONLY);
fileDes2 = open("home/aviad/Desktop/comparing/in2/sample2.txt", O_RDONLY);
while((ret_in = fread (buffer, BUF_SIZE, 1, fileDes1)) > 0)
{
ret_out= fread (buffer, BUF_SIZE, 1, fileDes2);
if (ret_out != ret_in) // file sizes different -> not similar
{
notsimilar = 1;
break;
}
else
{
for (i = 0; i < ret_out)
{
if (buffer[i] == buffer2[i])
{
printf("%c", buffer[i]);
where++;
}
else
{
notsimilar = 1;
}
}
}
}
if (notsimilar)
printf("similar\n");
else
printf("similar\n");
printf("%c", buffer[i]);
printf("%c", buffer2[i]);
return 0;
}
您还应该测试文件是否真的可以打开。您只是假设这两个文件都存在。
我需要比较文件描述符中的 2 个字符。
我的文件 sample.txt 包括 "first" 我的文件 sample2.txt 包括 "firbbbbbbst"
我需要 return "not similar",但它总是 returns "similar" 并且从未真正将行读取到缓冲区。
int main(int argc, char **argv)
{
ssize_t ret_in, ret_out;
char buffer[BUF_SIZE];
char buffer2[BUF_SIZE];
int where=0;
int fileDes1;
int fileDes2;
fileDes1 = open("home/aviad/Desktop/comparing/in1/sample.txt", O_RDONLY);
fileDes2 = open("home/aviad/Desktop/comparing/in2/sample2.txt", O_RDONLY);
while((ret_in = fread (buffer, BUF_SIZE, 1, fileDes1)) > 0)
{
ret_out= fread (buffer, BUF_SIZE, 1, fileDes2);
if (buffer[where] == buffer2[where])
{
printf("%c", buffer[where]);
where++;
}
else {
printf("nor similar\n");
break;
}
}
printf("similar\n");
printf("%c", buffer[2]); //garbage
printf("%c", buffer2[2]); //garbage
return 0;
}
您可能需要这个:
免责声明:未经测试的代码可能无法运行甚至无法编译,但它至少应该给您一个想法。总之还有进步空间
int main(int argc, char **argv)
{
ssize_t ret_in, ret_out;
char buffer[BUF_SIZE];
char buffer2[BUF_SIZE];
int where=0;
int fileDes1;
int fileDes2;
int notsimilar = 0;
int i = 0;
fileDes1 = open("home/aviad/Desktop/comparing/in1/sample.txt", O_RDONLY);
fileDes2 = open("home/aviad/Desktop/comparing/in2/sample2.txt", O_RDONLY);
while((ret_in = fread (buffer, BUF_SIZE, 1, fileDes1)) > 0)
{
ret_out= fread (buffer, BUF_SIZE, 1, fileDes2);
if (ret_out != ret_in) // file sizes different -> not similar
{
notsimilar = 1;
break;
}
else
{
for (i = 0; i < ret_out)
{
if (buffer[i] == buffer2[i])
{
printf("%c", buffer[i]);
where++;
}
else
{
notsimilar = 1;
}
}
}
}
if (notsimilar)
printf("similar\n");
else
printf("similar\n");
printf("%c", buffer[i]);
printf("%c", buffer2[i]);
return 0;
}
您还应该测试文件是否真的可以打开。您只是假设这两个文件都存在。