C语言载入txt文件
时间: 2023-07-04 19:12:17 浏览: 107
您可以使用C语言中的标准库函数fopen()和fscanf()来载入txt文件。
下面是一个简单的示例代码:
```c
#include <stdio.h>
int main() {
FILE *fp;
char filename[] = "example.txt";
int num1, num2;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file %s\n", filename);
return 1;
}
fscanf(fp, "%d %d", &num1, &num2);
printf("The first number is %d and the second number is %d\n", num1, num2);
fclose(fp);
return 0;
}
```
在这个示例中,我们打开了名为example.txt的文件,使用fscanf()函数读取了文件中的前两个整数,并将它们打印到控制台上。最后,我们使用fclose()函数关闭了文件。
请注意,如果文件打开失败,我们会收到一个错误消息并退出程序。
相关问题
c语言游戏状态的文件保存载入
在C语言中,可以使用文件I/O操作来保存和载入游戏状态。
首先,需要定义一个结构体来存储游戏状态的各个变量,例如:
```c
struct game_state {
int level;
int score;
char player_name[20];
// 其他变量
};
```
然后,可以使用fopen()函数打开一个文件,使用fprintf()函数将游戏状态写入文件,使用fscanf()函数从文件中读取游戏状态。例如:
```c
void save_game_state(struct game_state state) {
FILE *fp;
fp = fopen("game_state.txt", "w");
fprintf(fp, "%d %d %s", state.level, state.score, state.player_name);
fclose(fp);
}
struct game_state load_game_state() {
struct game_state state;
FILE *fp;
fp = fopen("game_state.txt", "r");
fscanf(fp, "%d %d %s", &state.level, &state.score, state.player_name);
fclose(fp);
return state;
}
```
注意,需要在编译时加上 -lm 参数,以链接数学库。完整的代码示例如下:
```c
#include <stdio.h>
struct game_state {
int level;
int score;
char player_name[20];
};
void save_game_state(struct game_state state) {
FILE *fp;
fp = fopen("game_state.txt", "w");
fprintf(fp, "%d %d %s", state.level, state.score, state.player_name);
fclose(fp);
}
struct game_state load_game_state() {
struct game_state state;
FILE *fp;
fp = fopen("game_state.txt", "r");
fscanf(fp, "%d %d %s", &state.level, &state.score, state.player_name);
fclose(fp);
return state;
}
int main() {
struct game_state state = {5, 100, "Alice"};
save_game_state(state);
struct game_state loaded_state = load_game_state();
printf("Loaded game state: level=%d, score=%d, player_name=%s\n",
loaded_state.level, loaded_state.score, loaded_state.player_name);
return 0;
}
```
用C语言的结构体编写汽车配件管理系统该系统主要包括管理员用户和普通用户,并且该系统可以读取文件来载入客户账号以及产品信息。给该系统的文件保存和载入编写代码。其中管理员模块功能包括(管理、录入、产品详细信息、信息提交、修改);普通用户模块功能包括( 账号注册、产品信息预览、用户端查看)。
好的,这里提供一份对汽车配件管理系统的文件保存和载入的代码实现,同时包含管理员和普通用户模块的功能。
首先,我们需要定义一些结构体来表示用户和产品信息,与之前一样,例如:
```c
struct User {
char username[20];
char password[20];
int isAdmin;
};
struct Product {
char name[50];
char type[20];
double price;
int stock;
};
```
其中,`User`结构体表示用户信息,包括用户名、密码和是否为管理员;`Product`结构体表示产品信息,包括产品名称、类型、价格和库存。
接下来,我们可以使用文件读写来保存和载入数据。
```c
void saveUsersToFile(User users[], int numUsers) {
FILE* fp = fopen("users.txt", "w");
if (fp == NULL) {
printf("Failed to open file users.txt.\n");
return;
}
for (int i = 0; i < numUsers; i++) {
fprintf(fp, "%s %s %d\n", users[i].username, users[i].password, users[i].isAdmin);
}
fclose(fp);
}
void loadUsersFromFile(User users[], int* numUsers) {
FILE* fp = fopen("users.txt", "r");
if (fp == NULL) {
printf("Failed to open file users.txt.\n");
return;
}
while (fscanf(fp, "%s %s %d", users[*numUsers].username, users[*numUsers].password, &users[*numUsers].isAdmin) != EOF) {
(*numUsers)++;
}
fclose(fp);
}
void saveProductsToFile(Product products[], int numProducts) {
FILE* fp = fopen("products.txt", "w");
if (fp == NULL) {
printf("Failed to open file products.txt.\n");
return;
}
for (int i = 0; i < numProducts; i++) {
fprintf(fp, "%s %s %lf %d\n", products[i].name, products[i].type, products[i].price, products[i].stock);
}
fclose(fp);
}
void loadProductsFromFile(Product products[], int* numProducts) {
FILE* fp = fopen("products.txt", "r");
if (fp == NULL) {
printf("Failed to open file products.txt.\n");
return;
}
while (fscanf(fp, "%s %s %lf %d", products[*numProducts].name, products[*numProducts].type, &products[*numProducts].price, &products[*numProducts].stock) != EOF) {
(*numProducts)++;
}
fclose(fp);
}
```
以上代码中,`saveUsersToFile()`和`loadUsersFromFile()`分别用于保存和载入用户信息,`saveProductsToFile()`和`loadProductsFromFile()`分别用于保存和载入产品信息。
在管理员模块中,我们可以实现一些功能来管理和修改产品信息,例如:
```c
void addProduct(Product products[], int* numProducts) {
printf("Enter product name: ");
scanf("%s", products[*numProducts].name);
printf("Enter product type: ");
scanf("%s", products[*numProducts].type);
printf("Enter product price: ");
scanf("%lf", &products[*numProducts].price);
printf("Enter product stock: ");
scanf("%d", &products[*numProducts].stock);
(*numProducts)++;
printf("Product added successfully.\n");
}
void editProduct(Product products[], int numProducts) {
char name[50];
printf("Enter product name to edit: ");
scanf("%s", name);
int index = -1;
for (int i = 0; i < numProducts; i++) {
if (strcmp(products[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Product not found.\n");
return;
}
printf("Enter new product name: ");
scanf("%s", products[index].name);
printf("Enter new product type: ");
scanf("%s", products[index].type);
printf("Enter new product price: ");
scanf("%lf", &products[index].price);
printf("Enter new product stock: ");
scanf("%d", &products[index].stock);
printf("Product edited successfully.\n");
}
void deleteProduct(Product products[], int* numProducts) {
char name[50];
printf("Enter product name to delete: ");
scanf("%s", name);
int index = -1;
for (int i = 0; i < *numProducts; i++) {
if (strcmp(products[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Product not found.\n");
return;
}
for (int i = index; i < *numProducts - 1; i++) {
products[i] = products[i + 1];
}
(*numProducts)--;
printf("Product deleted successfully.\n");
}
```
以上代码中,`addProduct()`用于添加新的产品信息,`editProduct()`用于修改现有的产品信息,`deleteProduct()`用于删除产品信息。
在普通用户模块中,我们可以实现一些功能来查看产品信息。例如:
```c
void viewProducts(Product products[], int numProducts) {
printf("Products:\n");
for (int i = 0; i < numProducts; i++) {
printf("Name: %s\n", products[i].name);
printf("Type: %s\n", products[i].type);
printf("Price: %.2lf\n", products[i].price);
printf("Stock: %d\n", products[i].stock);
printf("\n");
}
}
```
以上代码中,`viewProducts()`用于查看所有产品的详细信息。
希望以上代码对您有所帮助!
阅读全文