#include <stdio.h> #define N 60 int main(int argc ,char *argv[]) { if (argc< 3) { perror("参数错误\n"); return -1; } FILE *fpr ; if ((fpr=fopen (argv[1],"r") )== NULL ) { perror("fpropen"); return -1; } char buf[N]; int ret; ret=fread(buf,sizeof(char),N,fpr); while (ret!=0) { printf("%s",buf); ret=fread(buf,sizeof(char),ret,fpr); } return 0; }
时间: 2024-02-14 08:35:54 浏览: 18
这是一个使用 C 语言编写的程序,它可以读取一个指定的文件,并将文件内容输出到控制台。程序的主要流程如下:
1. 检查参数个数,如果少于 3 个,则输出错误信息并返回 -1。
2. 打开指定的文件,如果文件打开失败,则输出错误信息并返回 -1。
3. 定义一个大小为 N 的字符数组 buf,用于读取文件内容。
4. 使用 fread() 函数读取文件内容到 buf 中,每次最多读取 N 个字符。如果读取成功,则将 buf 中的内容输出到控制台。
5. 继续使用 fread() 函数读取文件内容,直到读取到文件末尾。
6. 关闭文件并返回 0。
需要注意的是,程序中使用了宏定义 #define N 60 来定义字符数组 buf 的大小。如果读取的文件内容超过了 N 个字符,则只会输出前面的 N 个字符。另外,程序中使用了命令行参数来指定要读取的文件名,第一个参数是程序名称本身,第二个参数是要读取的文件名。
相关问题
#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;
}
```