agudbd 2023-09-24 09:30 采纳率: 33.3%
浏览 71
已结题

C++从txt文件中读取数据存入数组中

C++从txt文件中读取数据并存入结构体数组中

img


book.txt中的内容如下,现在的问题是如何跨过前两行把后面的内容录入的结构体数组中

大学图书馆计算机类图书采购列表
ISBN 书名 定价
9787302257646 程序设计基础 25
9787302164340 程序设计基础(第2版) 20
9787302219972 单片机技术及应用 32
9787302203513 单片机原理与应用技术 26
9787810827430 工业计算机控制技术——原理与应用 29
9787811234923 汇编语言程序设计教程 21
9787512100831 计算机操作系统 17
9787302202844 计算机操作系统基础与应用(第二版) 30
9787302265436 计算机导论实验指导 18
9787302131304 计算机数学 20
9787302162834 计算机数学 30
9787302221630 计算机数学(第二版) 25
9787811235388 计算机网络基础 32
9787302137849 计算机维护与维修 26
9787302188308 计算机系统组装与维护 26
9787512101517 计算机应用基础与信息处理案例教程 25
下面是题目给出的框架代码,我进行了一点补充但未能解决问题

#include <bits/stdc++.h>
using namespace std;

typedef struct{
    char no[20];                                   //书号
    char name[100];                             //书名
    float price;                                   //价格
}Book;

typedef struct{
    Book *elem;                                 //指向数组的指针
    int length;                                 //数组的长度
}SqList;

void InitList(SqList &L){                               
//使用动态内存分配new进行初始化
int size=100;
L.elem=new Book[size];
L.length=0;
}

void FreeList(SqList &L){
//释放内存
delete[] L.elem;
L.length=0;
}

void ReadFile(SqList &L, string filepath){
//从文件中读取图书信息,将其按顺序存入L.elem指向的数组中
ifstream inputFile(filepath);
if(!inputFile.is_open()){
    cout<<"无法打开文件"<<endl;
    return;
}

while (inputFile >> L.elem[L.length].no >> L.elem[L.length].name >> L.elem[L.length].price)
    {
        L.length++;
    }
inputFile.close();
}

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-09-24 09:54
    关注

    引用 皆我百晓生 小程序回应内容作答:

    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    typedef struct {
        char no[20];                                      // 书号
        char name[100];                                   // 书名
        float price;                                      // 价格
    } Book;
    
    typedef struct {
        Book *elem;                                       // 指向数组的指针
        int length;                                       // 数组的长度
    } SqList;
    
    void InitList(SqList &L) {
        int size = 100;
        L.elem = new Book[size];
        L.length = 0;
    }
    
    void FreeList(SqList &L) {
        delete[] L.elem;
        L.length = 0;
    }
    
    void ReadFile(SqList &L, string filepath) {
        ifstream inputFile(filepath);
        if (!inputFile.is_open()) {
            cout << "无法打开文件" << endl;
            return;
        }
    
        // 跳过前两行
        string dummyLine;
        getline(inputFile, dummyLine);
        getline(inputFile, dummyLine);
    
        while (inputFile >> L.elem[L.length].no >> L.elem[L.length].name >> L.elem[L.length].price) {
            L.length++;
        }
    
        inputFile.close();
    }
    
    int main() {
        string filepath = "book.txt";
        SqList bookList;
        InitList(bookList);
        ReadFile(bookList, filepath);
    
        for (int i = 0; i < bookList.length; i++) {
            cout << "书号: " << bookList.elem[i].no << endl;
            cout << "书名: " << bookList.elem[i].name << endl;
            cout << "价格: " << bookList.elem[i].price << endl;
            cout << endl;
        }
    
        FreeList(bookList);
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月2日
  • 已采纳回答 9月24日
  • 赞助了问题酬金15元 9月24日
  • 创建了问题 9月24日