要求用C语言编写:1.先编写一个带结构的文件,存入电脑,要求第一行从左到右的内容依次是:姓名、地址、学号,每排每列对齐,没有对齐的地方用空格补齐。2.从电脑中能够读入这个带结构的文件,并进行删增改后,重新存入电脑,3.这个文件要有纵横结构,每横表示一个记录4. 编出来的代码要能够在c的运行界面进行后续的插入、删除和修改
时间: 2024-03-10 18:51:07 浏览: 31
好的,我可以帮你解答这个问题!
首先,我们可以定义一个结构体来代表每一条记录:
```c
typedef struct {
char name[20];
char address[50];
char student_id[10];
} Record;
```
然后,我们可以定义一个函数来从文件中读取记录:
```c
int read_records(const char* filename, Record** records, int* num_records) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
return -1; // 打开文件失败
}
// 先统计记录数
int num_records_ = 0;
char ch;
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n') {
num_records_++;
}
}
// 分配内存
Record* records_ = (Record*) malloc(num_records_ * sizeof(Record));
if (records_ == NULL) {
return -2; // 分配内存失败
}
// 重新定位文件指针到开头
fseek(fp, 0, SEEK_SET);
// 读取记录
for (int i = 0; i < num_records_; i++) {
fscanf(fp, "%s %s %s", records_[i].name, records_[i].address, records_[i].student_id);
}
// 关闭文件
fclose(fp);
// 返回结果
*records = records_;
*num_records = num_records_;
return 0;
}
```
这个函数会返回一个整数值,表示读取文件的状态。如果返回值为0,则表示读取成功,记录会被存储在 `records` 指向的指针中,记录数会存储在 `num_records` 指向的整数中。如果返回值为负数,则表示读取失败。
接下来,我们可以定义一个函数来向文件中插入一条记录:
```c
int insert_record(const char* filename, const Record* record) {
FILE* fp = fopen(filename, "a");
if (fp == NULL) {
return -1; // 打开文件失败
}
fprintf(fp, "%-20s %-50s %-10s\n", record->name, record->address, record->student_id);
fclose(fp);
return 0;
}
```
这个函数会返回一个整数值,表示插入记录的状态。如果返回值为0,则表示插入成功。如果返回值为负数,则表示插入失败。
接下来,我们可以定义一个函数来修改一条记录:
```c
int update_record(const char* filename, int index, const Record* record) {
FILE* fp = fopen(filename, "r+");
if (fp == NULL) {
return -1; // 打开文件失败
}
// 定位到要修改的记录
fseek(fp, index * sizeof(Record), SEEK_SET);
// 写入新的记录
fprintf(fp, "%-20s %-50s %-10s\n", record->name, record->address, record->student_id);
fclose(fp);
return 0;
}
```
这个函数会返回一个整数值,表示修改记录的状态。如果返回值为0,则表示修改成功。如果返回值为负数,则表示修改失败。
最后,我们可以定义一个函数来删除一条记录:
```c
int delete_record(const char* filename, int index) {
FILE* fp = fopen(filename, "r+");
if (fp == NULL) {
return -1; // 打开文件失败
}
// 定位到要删除的记录
fseek(fp, index * sizeof(Record), SEEK_SET);
// 将后面的记录往前移动
Record record;
while (fread(&record, sizeof(Record), 1, fp) == 1) {
fseek(fp, -2 * sizeof(Record), SEEK_CUR);
fwrite(&record, sizeof(Record), 1, fp);
fseek(fp, sizeof(Record), SEEK_CUR);
}
// 截断文件
int num_records = ftell(fp) / sizeof(Record);
ftruncate(fileno(fp), (num_records - 1) * sizeof(Record));
fclose(fp);
return 0;
}
```
这个函数会返回一个整数值,表示删除记录的状态。如果返回值为0,则表示删除成功。如果返回值为负数,则表示删除失败。
这样,我们就完成了这个问题的解答。你现在可以在 C 语言中使用这些函数来实现一个带有增删改查功能的记录管理系统了。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)