我正在尝试编写一个比较2个文件的程序,如果它们相等则返回。
我只能使用以下功能:fork
,dup
,dup2
,open
,write
,exec
和{{1} }。
当我在linux gcc上编译程序时,它返回:
bash:./ comp.out:权限被拒绝
代码:
read
如何更改权限?
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int CheckSimilar(char *path1, char *path2);
int main(int argc, char **argv) {
int returnValue = CheckSimilar(argv[1], argv[2]);
switch (returnValue){
case 1:
write(1, "1\n", 3);
break;
case 2:
write(1, "2\n", 3);
break;
case 3:
write(1, "3\n", 3);
break;
default:
break;
}
return 0;
}
/*
* This function checks if the files are similar or similar by case sensitive
* it gets 2 files, and returns: 3 if identical, 2 if identical but only if not
* case sensitive or 1 else.
*/
答案 0 :(得分:3)
命令
-c
创建目标文件,而不是可执行程序文件。对象文件是已编译的translation units(大致包含所有包含头文件的源文件),仅此而已。它是gcc
标志,告诉编译器前端程序-c
创建一个目标文件,因此要么删除$ gcc ec11.c -o comp.out
标志,要么明确链接。
所以要么
$ gcc -c ec11.c
$ gcc ec11.o -o comp.out
或的
-Wall -Wextra -pedantic
在一个不相关的说明中,我建议你在编译时添加警告标志,这样编译器会给你更多关于运行时可能引起麻烦的事情的警告(比如未定义的行为或逻辑/语义问题)。我个人至少使用旗帜int main()
{
char file1[100];
int count=0;
char c,ch,ck;
FILE *fptr;
printf("Enter the file name\n");
scanf("%s", file1);
fptr=fopen(file1, "r");
printf("Enter the character to be counted\n");
scanf(" %c", c);
ck = c;
if((int)c>=65 &&(int)c<=90)
c = (int)c+32;
while((ch = getc(fptr)))
{
if((int)ch>=65 && (int)ch<=90)
ch = (int)ch+32;
if(ch == EOF)
break;
else if(ch == c)
count+=1;
}
printf("File '%s' has %d instances of letter '%c'.",file1,count,ck);
fclose(fptr);
return 0;
}
。