用C语言代码实现读取非文本文件,返回文件长度
时间: 2023-07-18 07:33:43 浏览: 42
可以使用C语言的标准库函数fseek和ftell来实现读取非文本文件并返回文件长度。
示例代码如下:
```c
#include <stdio.h>
int main() {
FILE *fp;
long int len;
// 打开文件
fp = fopen("filename.bin", "rb");
if (fp == NULL) {
printf("无法打开文件\n");
return 1;
}
// 定位到文件末尾
fseek(fp, 0, SEEK_END);
// 获取文件长度
len = ftell(fp);
// 关闭文件
fclose(fp);
printf("文件长度为 %ld 字节\n", len);
return 0;
}
```
在打开文件时,需要指定文件的打开模式为"rb",表示以二进制方式打开文件。使用fseek将文件指针定位到文件末尾,再使用ftell获取文件长度。最后记得关闭文件。
相关问题
c语言中获取文件长度并读取文件
在C语言中获取文件长度并读取文件,可以使用以下步骤:
1. 打开文件:使用fopen()函数打开文件,并返回文件指针。
2. 定位到文件末尾:使用fseek()函数将文件指针移动到文件末尾,以便获取文件长度。
3. 获取文件长度:使用ftell()函数获取文件指针的当前位置,即文件长度。
4. 将文件指针移回文件开头:使用rewind()函数将文件指针移回文件开头。
5. 读取文件内容:使用fread()函数读取文件内容,并将其存储到一个缓冲区中。
6. 关闭文件:使用fclose()函数关闭文件。
下面是一个示例代码,演示了如何获取文件长度并读取文件:
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
long file_len;
char *buffer;
// 打开文件
fp = fopen("test.txt", "rb");
if (fp == NULL) {
printf("Failed to open file.\n");
exit(1);
}
// 定位到文件末尾,获取文件长度
fseek(fp, 0, SEEK_END);
file_len = ftell(fp);
// 将文件指针移回文件开头
rewind(fp);
// 分配缓冲区
buffer = (char*) malloc(file_len + 1);
if (buffer == NULL) {
printf("Failed to allocate memory.\n");
exit(1);
}
// 读取文件内容到缓冲区中
fread(buffer, file_len, 1, fp);
buffer[file_len] = '\0';
// 输出文件内容
printf("%s", buffer);
// 关闭文件
fclose(fp);
return 0;
}
```
注意,上述代码中的文件打开模式为"rb",表示以二进制模式打开文件。如果要以文本模式打开文件,则应该使用"r"模式。
使用c语言写一串代码实现商品库存管理系统,要求用到文本文件
好的,以下是一个简单的商品库存管理系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ITEMS 100 // 最大商品数量
#define MAX_NAME_LEN 50 // 商品名称最大长度
typedef struct {
int id;
char name[MAX_NAME_LEN];
int quantity;
} Item;
int read_items(Item items[]) {
FILE *fp;
fp = fopen("items.txt", "r");
if (fp == NULL) {
printf("Failed to open items.txt\n");
return 0;
}
int i = 0;
while (!feof(fp) && i < MAX_ITEMS) {
int id, quantity;
char name[MAX_NAME_LEN];
fscanf(fp, "%d %s %d", &id, name, &quantity);
items[i].id = id;
strcpy(items[i].name, name);
items[i].quantity = quantity;
i++;
}
fclose(fp);
return i;
}
void write_items(Item items[], int num_items) {
FILE *fp;
fp = fopen("items.txt", "w");
if (fp == NULL) {
printf("Failed to open items.txt\n");
return;
}
for (int i = 0; i < num_items; i++) {
fprintf(fp, "%d %s %d\n", items[i].id, items[i].name, items[i].quantity);
}
fclose(fp);
}
void print_items(Item items[], int num_items) {
printf("ID\tName\tQuantity\n");
for (int i = 0; i < num_items; i++) {
printf("%d\t%s\t%d\n", items[i].id, items[i].name, items[i].quantity);
}
}
int main() {
Item items[MAX_ITEMS];
int num_items = read_items(items);
while (1) {
printf("1. Add item\n");
printf("2. Remove item\n");
printf("3. Update quantity\n");
printf("4. Print items\n");
printf("5. Exit\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1: {
if (num_items == MAX_ITEMS) {
printf("Maximum number of items reached\n");
} else {
Item item;
printf("Enter item ID: ");
scanf("%d", &item.id);
printf("Enter item name: ");
scanf("%s", item.name);
printf("Enter item quantity: ");
scanf("%d", &item.quantity);
items[num_items] = item;
num_items++;
write_items(items, num_items);
printf("Item added successfully\n");
}
break;
}
case 2: {
int id;
printf("Enter item ID to remove: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_items; i++) {
if (items[i].id == id) {
for (int j = i; j < num_items - 1; j++) {
items[j] = items[j + 1];
}
num_items--;
write_items(items, num_items);
printf("Item removed successfully\n");
found = 1;
break;
}
}
if (!found) {
printf("Item not found\n");
}
break;
}
case 3: {
int id, quantity;
printf("Enter item ID to update quantity: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_items; i++) {
if (items[i].id == id) {
printf("Enter new quantity: ");
scanf("%d", &quantity);
items[i].quantity = quantity;
write_items(items, num_items);
printf("Quantity updated successfully\n");
found = 1;
break;
}
}
if (!found) {
printf("Item not found\n");
}
break;
}
case 4: {
print_items(items, num_items);
break;
}
case 5: {
printf("Exiting...\n");
return 0;
}
default: {
printf("Invalid choice\n");
break;
}
}
}
return 0;
}
```
这个程序使用了一个`Item`结构体来存储每个商品的ID、名称和数量。它从文本文件`items.txt`中读取商品信息,并在修改后将其写回文件。可以通过菜单实现添加、删除、更新和打印商品的功能。