我正在尝试将一个文件的内容复制到另一个文件,但是每当我运行代码时,除了复制文件外,其他所有功能都起作用。我认为这可能与我的while循环有关。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//You may also add more include directives as well.
// THIS VALUE CANNOT BE CHANGED.
// You should use this value when creating your buffer.
// And also to limit the amount of bytes each read CAN do.
# define BUFF_MAX 13
// ABOVE VALUE CANNOT BE CHANGED //
// creating arrays to save inputs
int main(int argc, char const *argv[]) {
char fileName[BUFF_MAX], copyTo[BUFF_MAX], Buf[BUFF_MAX];
int nameInput, copyInfo;
int count = 0;
ssize_t ret_in;
printf("Welcome to the file Copy Program by Jocelyn Guzman!\n Enter the name of file to copy from:\n ");
scanf("%s", fileName);
nameInput = open(fileName, O_RDONLY);
if (nameInput <0) {
perror("Error");
exit(1);
}
printf("Enter the name of file to copy to:\n ");
scanf("%s",copyTo);
copyInfo=open(copyTo,O_RDONLY);
if(copyInfo<0) {
perror("Error");
exit(1);
}
while ((ret_in = read(fileName,Buf,BUFF_MAX))>0){
write(copyTo,Buf,ret_in);
count += ret_in;
}
close(fileName);
close(copyTo);
printf("File Copied Successful, %d bytes copied\n");
return 0;
}
我正在尝试遵循伪代码逻辑,读取的文件将反复从输入文件读取到缓冲区中,并且文件的写入将反复从缓冲区写入输出文件中。
...
Welcome to the File Copy Program by <yourname>!
Enter the name of the file to copy from:
<type in file name & hit return>
Enter the name of the file to copy to:
<type in the file name & hit return>
File Copy Successful, <number> bytes copied
答案 0 :(得分:1)
ui.Module_1_Ch_1
ui.Module_1_Ch_2
ui.Module_2_Ch_1
ui.Module_2_Ch_2
这是您要打开的文件。但是您仍然通过for i in [1,2]:
for j in [1,2]:
if ui.Module_%i_Ch_%j.isChecked()==True:
Do Something
else:
pass
。您将如何写入以只读模式打开的文件?
List_1=[1,2,3]
List_2=[4,5,6]
print(List_%s %input())
您无法传递copyInfo=open(copyTo,O_RDONLY);
和O_RDONLY
文件名。他们需要一个打开的文件描述的句柄,以跟踪您正在读取或写入的文件中的位置,并跟踪模式(只读,读/写等)。您应该将打开文件描述的句柄传递给while ((ret_in = read(fileName,Buf,BUFF_MAX))>0){
write(copyTo,Buf,ret_in);
和read
。这就是为什么您调用write
的原因-创建一个新的打开文件描述并获取它的句柄。