C程序执行到fread函数突然退出,这是为啥?

此文提出的问题及答案分析,收录在我的C语言解惑圈子的第266篇:【第266篇】【C语言基础】【变量声明时未赋初始值,然后程序莫名其妙退出,你造吗?】

同学们可以加我微信:c-poop 进圈查看学习。

有同学问了我一个奇怪的问题,说C程序运行的好好的,突然就退出了!根据这位同学的描述,我写了一个C程序来重现这个奇怪的场景,C程序代码如下:

#include<stdio.h>

typedef struct student
{
	int NO;//学号 
	char name[10];//姓名 
}STU;

int main() 
{
	FILE *pFile = NULL;
	int index;
	STU array[2] = { {1, "wang"}, {2, "zhang"} };
	STU shuzu[5];

    if( NULL == (pFile = fopen("c:\\student.txt", "wb")) )//以可写的方式读取"student.txt"文件
    {
        printf("打开学生信息文件 %s 失败!\n", "student.txt");
        return -1;
    }
    
    fwrite(&array[0], sizeof(STU), 1, pFile);//写文件 
    fwrite(&array[1], sizeof(STU), 1, pFile);//写文件 
    
    fclose(pFile);
    
    if( NULL == (pFile = fopen("c:\\student.txt", "rb")) )//以只读的方式读取"student.txt"文件
    {
        printf("打开学生信息文件 %s 失败!\n", "student.txt");
        return -1;
    }
    
	while(!feof(pFile))
	{
		printf("=========\n");
		fread(&shuzu[index], sizeof(STU), 1, pFile);
		index++;
	}
    
    fclose(pFile);
    
    return 0;
}

这个C程序很简单,先往c:\\student.txt这个文件写入两行数据,然后再从c:\\student.txt读出这两行数据,并且放在数组shuzu中。读取函数是fread,可是执行到: fread(&shuzu[index], sizeof(STU), 1, pFile); 这一行时,C程序突然退出了! 运行结果如下:

是不是很奇怪?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Linux C 语言中,使用系统函数复制目录需要使用递归的方式遍历目录树,并对每个文件进行复制。下面是一个简单的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #define BUFFER_SIZE 1024 void copy_file(const char *src, const char *dst) { char buffer[BUFFER_SIZE]; size_t nread; FILE *fp_src = fopen(src, "rb"); if (fp_src == NULL) { perror("fopen"); exit(1); } FILE *fp_dst = fopen(dst, "wb"); if (fp_dst == NULL) { perror("fopen"); exit(1); } while ((nread = fread(buffer, 1, sizeof(buffer), fp_src)) > 0) { fwrite(buffer, 1, nread, fp_dst); } fclose(fp_src); fclose(fp_dst); } void copy_dir(const char *src, const char *dst) { DIR *dir = opendir(src); if (dir == NULL) { perror("opendir"); exit(1); } char src_path[BUFFER_SIZE], dst_path[BUFFER_SIZE]; struct dirent *entry; struct stat statbuf; while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } snprintf(src_path, sizeof(src_path), "%s/%s", src, entry->d_name); snprintf(dst_path, sizeof(dst_path), "%s/%s", dst, entry->d_name); if (lstat(src_path, &statbuf) == -1) { perror("lstat"); exit(1); } if (S_ISREG(statbuf.st_mode)) { copy_file(src_path, dst_path); } else if (S_ISDIR(statbuf.st_mode)) { if (mkdir(dst_path, statbuf.st_mode & 0777) == -1 && errno != EEXIST) { perror("mkdir"); exit(1); } copy_dir(src_path, dst_path); } else { fprintf(stderr, "Unsupported file type: %s\n", src_path); exit(1); } } closedir(dir); } int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]); exit(1); } if (mkdir(argv[2], 0777) == -1 && errno != EEXIST) { perror("mkdir"); exit(1); } copy_dir(argv[1], argv[2]); return 0; } ``` 这个程序的使用方法是: ``` $ ./copydir <source> <destination> ``` 其中 `<source>` 是源目录的路径,`<destination>` 是目标目录的路径。 程序首先检查命令行参数的个数是否正确,如果不正确则输出用法信息并退出。然后创建目标目录,如果目录创建失败则输出错误信息并退出。接下来,程序使用递归的方式遍历源目录中的文件,并将它们复制到目标目录中。如果遇到普通文件,则调用 `copy_file` 函数进行复制;如果遇到子目录,则递归调用 `copy_dir` 函数进行复制。 需要注意的是,在复制目录时,目标目录必须事先存在。如果目标目录不存在,则需要先创建目标目录。此外,如果遇到目录中存在软链接等非普通文件类型,则需要根据实际情况进行处理。 如果要使用标准库来粘贴目录到其他目录,可以使用 `system` 函数调用 `cp` 命令来实现。例如: ```c #include <stdlib.h> int main() { system("cp -r /path/to/source /path/to/destination"); return 0; } ``` 这个程序的作用是将 `/path/to/source` 目录复制到 `/path/to/destination` 目录中。需要注意的是,在使用 `system` 函数调用命令时,需要特别注意命令中可能包含的特殊字符,如空格、单引号、双引号等。这些特殊字符可能会导致命令执行出错。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C语言答疑课堂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值