请试着写一个C语言程序:关于您感兴趣的大学相关历史沿革中的四条记录,写入到一个mycollege.txt的文件里。
时间: 2024-02-26 08:53:16 浏览: 35
好的,以下是一个简单的C语言程序,可以将四条大学历史沿革记录写入到mycollege.txt文件中:
```c
#include <stdio.h>
int main() {
// 打开文件
FILE *file = fopen("mycollege.txt", "w");
// 写入四条历史沿革记录
fprintf(file, "1. 1901年创建\n");
fprintf(file, "2. 1956年更名为XX大学\n");
fprintf(file, "3. 1978年成为全国重点大学\n");
fprintf(file, "4. 2000年获得教育部211工程重点支持\n");
// 关闭文件
fclose(file);
return 0;
}
```
这个程序中使用了标准库中的文件操作函数,首先通过`fopen`函数打开一个名为`mycollege.txt`的文件,打开方式为写入模式("w")。然后使用`fprintf`函数向文件中写入四条历史沿革记录。最后,通过`fclose`函数关闭文件。
相关问题
请试着写一个C语言程序:关于清华大学相关历史沿革中的四条记录,写入到一个mycollege.txt的文件里。
好的,以下是一个简单的C语言程序,可以将清华大学的四条历史沿革记录写入到mycollege.txt文件中:
```c
#include <stdio.h>
int main() {
// 打开文件
FILE *file = fopen("mycollege.txt", "w");
// 写入清华大学四条历史沿革记录
fprintf(file, "1. 1911年创建清华学堂\n");
fprintf(file, "2. 1928年成为国立清华大学\n");
fprintf(file, "3. 1952年成为全国重点大学\n");
fprintf(file, "4. 1999年与北京大学共建\n");
// 关闭文件
fclose(file);
return 0;
}
```
这个程序和上一个程序的区别在于,写入的是清华大学的历史沿革记录。其他部分和上一个程序相同。
请试写一个c语言程序:关于你感兴趣的大学相关历史沿革中的四条记录,写到一个mycollege.txt的文件里
好的,以下是一个简单的C语言程序,可以实现将四条历史沿革记录写入mycollege.txt文件中:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char *filename = "mycollege.txt";
char *history[] = {
"1920年,本校创立",
"1950年,本校开始招收本科生",
"1980年,本校开始独立设置研究生院",
"2000年,本校成为国家重点大学"
};
int i;
fp = fopen(filename, "w");
if (fp == NULL) {
printf("无法打开文件 %s\n", filename);
exit(1);
}
for (i = 0; i < 4; i++) {
fprintf(fp, "%s\n", history[i]);
}
fclose(fp);
printf("成功写入 %s 文件\n", filename);
return 0;
}
```
这个程序定义了一个字符串数组history,其中存储了四条历史沿革记录。程序使用fopen函数打开一个名为mycollege.txt的文件,并使用for循环将history数组中的四条记录写入文件中。最后,程序使用fclose函数关闭文件,并输出成功写入文件的消息。
请注意,此程序假设当前目录下已存在名为mycollege.txt的文件,如果该文件不存在,程序将创建一个新的文件。如果需要避免覆盖已有的文件,可以将fopen函数的第二个参数从"w"改为"a",这样程序将在文件末尾添加新的记录而不是覆盖原有的记录。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)