英文:
C program hangs after creating file
问题
以下是您要翻译的内容:
创建一个名为'split'的C程序,该程序接受一个文件和一个数字。程序将把输入文件分成两个文件,一个称为'part1.txt',其中包含前n个字节,另一个称为'part2.txt',其中包含其余的字节。如果输入文件包含的字节少于n个,则不会创建'part2.txt'文件。
这是我的程序。当我执行它时,它会创建'part1.txt'文件(里面没有任何内容),然后程序卡住了。
我已经查看了一天,但找不到问题。有帮助吗?
我已经使用以下方式编译了它:
gcc -o split split.c
当我执行它时,我输入:
./split text.txt 10
其中'text.txt'是一个包含我随机按键输入的单词的文本文件。
以下是程序代码部分,无需翻译:
int splitter;
int fd, fd1, fd2;
char buffer[5000];
int main(int argc, char** argv){
if(argc<2){
  printf("插入2个参数。");
  exit(1);
}
splitter = atoi(argv[2]);
if (fd=open(argv[1], O_RDONLY) < 0){
  perror("错误\n");
  exit(1);
} else {
  if (fd1=open("part1.txt", O_RDWR | O_CREAT, S_IRWXU) <0){
    perror("错误");
    exit(1);
  }
  if(read(fd,buffer,splitter) == splitter){
    write(fd1,buffer,splitter);
    if (fd2=open("part2.txt", O_RDWR | O_CREAT, S_IRWXU)<0){
      perror("错误");
      exit(1);
    };
    while (read(fd,buffer,1) ==1){
      write(fd2,buffer,1);
    }
    close(fd1);
    close(fd2);
  } else {
    while (read(fd,buffer,1) ==1){
      write(fd1,buffer,1);
    }
    close(fd1);
  }
  close(fd);
}
英文:
I'm doing an exercise for University, which asks:
Create a C program called 'split', which accepts a file, and a number. The program will divide the input file in two files, one called 'part1.txt' which will contain the first n bytes, and one called 'part2.txt' which will contain the other bytes.
If the input file contains less than n bytes, the file 'part2.txt' will not be created.
This is my program. What happens when I execute it, is that it creates the part1.txt file (without anything written in it) and the program hangs.
I've been looking at this for a day, but can't spot the problem. Any help?
I've compiled using:
gcc -o split split.c
When i execute it, i write:
./split text.txt 10
Where 'text.txt' is a text file containing words i accurately typed by pressing random buttons on my keyboard.
int splitter;
int fd, fd1, fd2;
char buffer[5000];
int main(int argc, char** argv){
if(argc<2){
  printf("Insert 2 arguments.");
  exit(1);
}
splitter = atoi(argv[2]);
if (fd=open(argv[1], O_RDONLY) < 0){
  perror("Error\n");
  exit(1);
} else {
  if (fd1=open("part1.txt", O_RDWR | O_CREAT, S_IRWXU) <0){
    perror("Error");
    exit(1);
  }
  if(read(fd,buffer,splitter) == splitter){
    write(fd1,buffer,splitter);
    if (fd2=open("part2.txt", O_RDWR | O_CREAT, S_IRWXU)<0){
      perror("Errore");
      exit(1);
    };
    while (read(fd,buffer,1) ==1){
      write(fd2,buffer,1);
    }
    close(fd1);
    close(fd2);
  } else {
    while (read(fd,buffer,1) ==1){
      write(fd1,buffer,1);
    }
    close(fd1);
  }
  close(fd);
}
答案1
得分: 3
C中的关系运算符位于优先级表的第6级,远高于赋值运算符。
这个语句:
if (fd=open(argv[1], O_RDONLY) < 0)
等同于:
if (fd = (open(argv[1], O_RDONLY) < 0))
<运算符返回1或0,换句话说,返回true或false,然后被赋给了fd。
将其更改为:
if ((fd = open(argv[1], O_RDONLY)) < 0)
在后续的if语句中也存在相同的问题。
来自《C专家编程》(Expert C Programming):
> 一些权威建议在C中只需记住两个优先级级别:乘法和除法优先于加法和减法。
> 其他一切都应该放在括号中。我们认为这是很好的建议。
英文:
The relational operators reside at the 6th level of C's precedence table, much higher than the assignment operators.
This statement:
if (fd=open(argv[1], O_RDONLY) < 0)
is equivalent to:
if (fd = (open(argv[1], O_RDONLY) < 0))
The < operator returns either 1 or 0, or in other words, true or false, which gets assigned to fd.
Change it to:
if ((fd = open(argv[1], O_RDONLY)) < 0)
You have the same issue in the subsequent if statement.
From Expert C Programming:
> Some authorities recommend that there are only two precedence levels
> to remember in C: multiplication and division come before addition and
> subtraction.
Everything else should be in parentheses. We think that's
> excellent advice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论