c向文件里面写入数据再打印出来
时间: 2023-06-24 07:03:14 浏览: 59
下面是一个简单的例子,演示如何向文件中写入数据,并从文件中读取数据并打印出来:
```c
#include <stdio.h>
int main() {
FILE *fp;
char str[100];
// 打开文件
fp = fopen("file.txt", "w+");
// 写入数据
fprintf(fp, "Hello, world!\n");
// 将文件指针移到文件开头
rewind(fp);
// 读取数据并打印出来
fscanf(fp, "%[^\n]", str);
printf("读取的数据:%s\n", str);
// 关闭文件
fclose(fp);
return 0;
}
```
在这个例子中,我们使用了 `fopen()` 函数来打开一个文件,并使用了 `fprintf()` 函数向文件中写入数据。然后,我们使用 `rewind()` 函数将文件指针移到文件开头,使用 `fscanf()` 函数从文件中读取数据,并使用 `printf()` 函数将数据打印出来。最后,我们使用 `fclose()` 函数关闭文件。
需要注意的是,在使用 `fscanf()` 函数读取数据时,我们使用了 `%[^\n]` 格式控制符,这是为了读取一行数据,直到遇到换行符为止。
相关问题
c语言向文件中写入字符串
在C语言中,可以使用标准库函数fwrite()来向文件中写入字符串。该函数原型如下:
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
其中,ptr是指向存储字符串的缓冲区的指针,size是每个数据元素的大小(通常为1),count是要写入的数据元素数量,stream是要写入的文件指针。
要将字符串写入文件,首先需要打开要写入的文件,可以使用fopen()函数来打开文件,原型如下:
FILE *fopen(const char *filename, const char *mode);
其中,filename是要打开的文件名,mode是打开文件的模式,如果文件不存在则创建文件并打开,如果文件已经存在则打开文件并截断(即清空文件)。
使用fwrite()函数将字符串写入文件示例如下:
```
#include <stdio.h>
int main() {
const char *str = "Hello, World!";
FILE *file = fopen("output.txt", "w");
if (file != NULL) {
fwrite(str, sizeof(char), strlen(str), file);
fclose(file);
printf("字符串写入文件成功!\n");
} else {
printf("打开文件失败!\n");
}
return 0;
}
```
在上述示例中,我们定义了一个字符串"Hello, World!",然后使用fopen()函数打开一个文件output.txt,打开模式为"w",表示写入模式。如果文件打开成功,则使用fwrite()函数将字符串写入文件,并使用fclose()函数关闭文件。最后,打印出字符串写入文件成功的消息。
需要注意的是,如果文件打开失败,会返回一个空指针,因此在写入文件前需要做错误处理。此外,在使用fwrite()函数时,size参数通常为1,因为写入的是字符串,每个数据元素的大小都是1。
用c语言write,read实现在1.txt中写入helloworld,再从该文件中读取出5个字节数据,并打印出来
以下是实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
char *msg = "helloworld\n";
char buf[5];
int fd = open("1.txt", O_RDWR | O_CREAT, 0666);
if (fd < 0) {
perror("open");
exit(1);
}
if (write(fd, msg, sizeof(msg)) < 0) {
perror("write");
exit(1);
}
if (lseek(fd, 0, SEEK_SET) < 0) {
perror("lseek");
exit(1);
}
if (read(fd, buf, sizeof(buf)) < 0) {
perror("read");
exit(1);
}
printf("%s", buf);
close(fd);
return 0;
}
```
代码解析:
首先定义了一个字符串 msg,用于写入文件。然后打开文件 1.txt,如果打开失败就输出错误信息并退出程序。接着使用 write 函数将 msg 写入文件中,如果写入失败也输出错误信息并退出程序。
接下来使用 lseek 函数将文件指针移动到文件开头,因为接下来要读取文件。然后使用 read 函数将文件中的 5 个字节读入 buf 数组中,如果读取失败也输出错误信息并退出程序。
最后使用 printf 函数输出读取到的 5 个字节,并关闭文件。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)