c语言中怎么用fwhite把一个同时含有数字和字符串的数组追加写入.txt结尾的文件里
时间: 2024-02-13 19:00:40 浏览: 30
可以按照以下步骤实现:
1. 打开文件,以追加写入的方式打开文件,如果文件不存在则创建文件:
```c
FILE *fp;
fp = fopen("file.txt", "a+");
```
2. 定义同时含有数字和字符串的数组:
```c
int arr[5] = {1, 2, 3, 4, 5};
char str[20] = "hello world";
```
3. 将数组按照需要的格式转换为字符串:
```c
char buffer[50];
sprintf(buffer, "%s %d %d %d %d %d", str, arr[0], arr[1], arr[2], arr[3], arr[4]);
```
4. 将字符串写入文件:
```c
fprintf(fp, "%s\n", buffer);
```
5. 关闭文件:
```c
fclose(fp);
```
完整的代码可以参考以下示例:
```c
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt", "a+");
int arr[5] = {1, 2, 3, 4, 5};
char str[20] = "hello world";
char buffer[50];
sprintf(buffer, "%s %d %d %d %d %d", str, arr[0], arr[1], arr[2], arr[3], arr[4]);
fprintf(fp, "%s\n", buffer);
fclose(fp);
return 0;
}
```
相关推荐
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![m](https://img-home.csdnimg.cn/images/20210720083646.png)