Linux C 读取一个文件的内容,代码
时间: 2024-05-07 07:23:10 浏览: 10
以下是一个简单的例子,演示如何在 Linux C 中读取文件的内容:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char ch;
// 打开文件
fp = fopen("test.txt", "r");
if (fp == NULL) {
printf("无法打开文件!\n");
exit(1);
}
// 读取并输出文件内容
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
// 关闭文件
fclose(fp);
return 0;
}
```
这个例子假设在当前目录下有一个名为 "test.txt" 的文件,程序会打开并读取该文件的内容,并将其输出到控制台。在实际使用中,你需要根据自己的需求进行修改。
相关问题
Linux驱动 读写一个文件 代码
您好,以下是读写一个文件的 Linux 驱动代码示例:
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/uaccess.h>
#define DEVICE_NAME "mydevice"
#define BUF_LEN 1024
MODULE_LICENSE("GPL");
static int major;
static char msg[BUF_LEN];
static char *msg_ptr;
static int device_open(struct inode *inode, struct file *file)
{
msg_ptr = msg;
try_module_get(THIS_MODULE);
return ;
}
static int device_release(struct inode *inode, struct file *file)
{
module_put(THIS_MODULE);
return ;
}
static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
{
int bytes_read = ;
if (*msg_ptr == )
return ;
while (length && *msg_ptr) {
put_user(*(msg_ptr++), buffer++);
length--;
bytes_read++;
}
return bytes_read;
}
static ssize_t device_write(struct file *filp, const char *buffer, size_t length, loff_t *offset)
{
int i;
for (i = ; i < length && i < BUF_LEN; i++)
get_user(msg[i], buffer + i);
msg_ptr = msg;
return i;
}
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release,
};
static int __init mydevice_init(void)
{
major = register_chrdev(, DEVICE_NAME, &fops);
if (major < ) {
printk(KERN_ALERT "Registering char device failed with %d\n", major);
return major;
}
printk(KERN_INFO "I was assigned major number %d. To talk to\n", major);
printk(KERN_INFO "the driver, create a dev file with\n");
printk(KERN_INFO "'mknod /dev/%s c %d '.\n", DEVICE_NAME, major);
return ;
}
static void __exit mydevice_exit(void)
{
unregister_chrdev(major, DEVICE_NAME);
printk(KERN_INFO "Goodbye, world!\n");
}
module_init(mydevice_init);
module_exit(mydevice_exit);
希望对您有所帮助。
linux c语言实现创建一个配置文件并实现读写
要实现创建一个配置文件并实现读写,可以使用C语言中的文件操作函数。具体步骤如下:
1. 使用 fopen 函数打开一个文件,如果文件不存在则创建文件。
2. 使用 fprintf 函数向文件中写入内容,格式可以自定义,如键值对、JSON等。
3. 使用 fseek 函数将文件指针移动到文件开头。
4. 使用 fgets 函数逐行读取文件内容,可以使用字符串处理函数解析出键值对等信息。
5. 使用 fclose 函数关闭文件。
下面是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CONFIG_FILE "config.txt"
#define MAX_LINE_LENGTH 1024
int main() {
// 创建配置文件并写入内容
FILE *fp = fopen(CONFIG_FILE, "w+");
if (fp == NULL) {
printf("Failed to create config file!\n");
exit(1);
}
fprintf(fp, "name=John\n");
fprintf(fp, "age=30\n");
fprintf(fp, "city=New York\n");
fseek(fp, 0, SEEK_SET);
// 读取配置文件并解析内容
char line[MAX_LINE_LENGTH];
while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) {
char *key = strtok(line, "=");
char *value = strtok(NULL, "=");
if (key != NULL && value != NULL) {
printf("Key: %s, Value: %s", key, value);
}
}
fclose(fp);
return 0;
}
```
上述代码创建了一个名为 "config.txt" 的配置文件,并向文件中写入了三个键值对。然后,程序将文件指针移动到文件开头,并逐行读取文件内容,解析出键值对并打印出来。最后,关闭文件并退出程序。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)