#include <stdio.h> #define N 60 int main(int argc ,char *argv[]) { if (argc< 3) { perror("参数错误\n"); return -1; } FILE *fpr ,*fpw; if ((fpr=fopen (argv[1],"r") )== NULL ) { perror("fpropen"); return -1; } if ((fpw=fopen (argv[2],"w") )== NULL ) { perror("fpwopen"); return -1; } char buf[N]; int ret; while ((ret=fread(buf,sizeof(char),N,fpr))!=0) { fwrite(buf,sizeof(char),N,fpw); } return 0; }这个代码有错误吗
时间: 2024-03-09 20:48:38 浏览: 19
这个代码存在一个潜在的错误,即在使用fwrite()函数写入目标文件时,应该写入实际读取的字节数,而不是固定的N个字节。因此,应该将fwrite()函数的第三个参数从N改为ret,这样可以确保写入目标文件的字节数与读取的字节数相等。修改后的代码如下:
```c
#include <stdio.h>
#define N 60
int main(int argc, char *argv[]) {
if (argc < 3) {
perror("参数错误\n");
return -1;
}
FILE *fpr, *fpw;
if ((fpr = fopen(argv[1], "r")) == NULL) {
perror("fpropen");
return -1;
}
if ((fpw = fopen(argv[2], "w")) == NULL) {
perror("fpwopen");
return -1;
}
char buf[N];
int ret;
while ((ret = fread(buf, sizeof(char), N, fpr)) != 0) {
fwrite(buf, sizeof(char), ret, fpw);
}
return 0;
}
```
相关问题
#include <stdio.h> #include <string.h> #define N 20 enum color { yellow=1, red=3, green=5 }; int main(int argc, char const *argv[]) { enum color s; s=yellow; printf("%d %s\n",yellow,s); return 0; } 输出的时候错了,为什么
在 `printf` 函数中,`%d` 是用来格式化整数的占位符,而 `s` 是一个 `enum color` 类型的变量。
在 C 语言中,枚举类型并不会直接映射为整数类型,而是作为一种独立的类型存在。因此,直接将枚举类型作为参数传递给 `%d` 是不正确的。
要正确地输出枚举类型的值,可以使用强制类型转换将其转换为整数类型,然后使用 `%d` 进行格式化输出。
以下是修正后的代码:
```c
#include <stdio.h>
#include <string.h>
#define N 20
enum color
{
yellow = 1,
red = 3,
green = 5
};
int main(int argc, char const *argv[])
{
enum color s;
s = yellow;
printf("%d %d\n", yellow, (int)s);
return 0;
}
```
这样,将枚举类型的值 `(int)s` 转换为整数类型后,才能正确地使用 `%d` 进行格式化输出。输出结果应该为枚举值对应的整数值。
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <sys/ipc.h> #include <sys/shm.h> #include <string.h> #define SIZE 128 int main(int argc,char *argv[]) { key_t key = ftok(".",'s'); if(key < 0) { perror("ftok"); return -1; } int shmid = shmget(key,SIZE,IPC_CREAT | 0777); if(shmid < 0) { perror("shmget"); return -1; } printf("shmid:%d\n",shmid); char *p = shmat(shmid,NULL,0); if((char *)-1 == p) { perror("shmat"); return -1; } while(1) { puts(p); sleep(1); if(strncmp(p,"quit",4) == 0) break; } if(-1 == shmdt(p)) { perror("shmdt"); exit(-1); } if(-1 == shmctl(shmid,IPC_RMID,NULL)) { perror("shmctl"); exit(-1); } return 0; }修改代码,使其正确输出读入端的代码一次
这里是修改后的代码,可以正确输出读入端的代码一次:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#define SIZE 128
int main(int argc, char *argv[]) {
key_t key = ftok(".", 's');
if (key < 0) {
perror("ftok");
return -1;
}
int shmid = shmget(key, SIZE, IPC_CREAT | 0777);
if (shmid < 0) {
perror("shmget");
return -1;
}
printf("shmid:%d\n", shmid);
char *p = shmat(shmid, NULL, 0);
if ((char *)-1 == p) {
perror("shmat");
return -1;
}
char buf[SIZE];
while (1) {
memset(buf, 0, SIZE);
fgets(buf, SIZE, stdin);
strncpy(p, buf, strlen(buf));
if (strncmp(p, "quit", 4) == 0)
break;
}
if (-1 == shmdt(p)) {
perror("shmdt");
exit(-1);
}
if (-1 == shmctl(shmid, IPC_RMID, NULL)) {
perror("shmctl");
exit(-1);
}
return 0;
}
```