从文件中,如何从C ++中的某个点读取 [英] From a file, how do I read from a certain point in C++

查看:138
本文介绍了从文件中,如何从C ++中的某个点读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么:
我需要在void main()中创建3个自动对象。
对于每个对象,我有一个txt文件中的信息。



如何获取每个对象的信息(engine,max_speed ... etc)如何跳过阅读引擎,如何在创建第二个对象时跳过整个自动。

  

auto1
engine:gasoline
max_speed:250
engine_cc:1980
avg_consumption_urban:11
avg_speed_urban:50
avg_consumption:8
avg_speed:
auto2
engine:diesel
max_speed:230
engine_cc:1600
avg_consumption_urban:9
avg_speed_urban:50
avg_consumption:6
avg_speed:80
auto3
引擎:hybrid
max_speed:190
engine_cc:1450
avg_consumption_urban:7
avg_speed_urban:50
avg_consumption: 4
avg_speed:90

到目前为止,我使用了这个代码简单版本的auto.txt读取和显示 - 见下面的简单版本的我的txt文件):



这是我的ifstream方法在Auto类



 朋友ifstream& operator>>(ifstream& in,Auto& a)
{
delete [] a.engine;
a.engine = new char [10];
在>>发动机
在>> a.max_speed;
在>> a.engine_cc;
在>> a.avg_consumption_urban;
在>> a.avg_speed_urban;
在>> a.avg_consumption;
在>> a.avg_speed;
return in;
}

这是我如何从文件中读取void main

  ifstream f(auto.txt,ios_base :: in); 
f>> auto1;
auto1.display();
f.close();

这是我从中读取的文本。这是一个简化的版本。

  gasoline 
250
1980
11
50
8
100


解决方案

是没有有效的方法来定位到给定的记录。记录是可变大小的,例如,发动机速度为100(3个字符),而另一个具有80(2个字符)。



由于每个字段位于单独的行上,因此您可以在文本开头读取每个文本行搜索engine。



这个项目闻起来可以使用数据库。



如果您必须逐行阅读,您也可以按记录读取记录。也就是说,让记录对象从文件中读取其成员。



如果在读取记录之前获得文件位置,可以将信息存储到 std :: map< unsigned int,Engine> 并使用文件指针作为地图的索引。



我说这种味道好像可以使用数据库吗?



另一个想法是读入所有记录并将它们推入 std :: vector 。创建 std :: map< unsigned int,unsigned int> 将向量索引映射到文件位置。



气味就像你应该使用数据库。



通过将引擎记录放入向量中,您可以创建其他索引表(数据库可以为您创建索引表)。例如,将引擎类型映射到向量索引的 std :: map< string,unsigned int>



查看数据库理论,特别是正常表单。当您使用数据库时,您将从这些知识中受益良多。


What I want to do: I need to create in void main() 3 auto objects. For each object I have the information in a txt file.

How do I get the information (engine, max_speed... etc) for each object. How do I skip reading "engine" and how do I skip a whole auto when creating the second object. Thanks!

The txt file: (auto.txt)

    auto1
    engine: gasoline
    max_speed: 250
    engine_cc: 1980
    avg_consumption_urban: 11
    avg_speed_urban: 50
    avg_consumption: 8
    avg_speed: 100
    auto2
    engine: diesel
    max_speed: 230
    engine_cc: 1600
    avg_consumption_urban: 9
    avg_speed_urban: 50
    avg_consumption: 6
    avg_speed: 80
    auto3
    engine: hybrid
    max_speed: 190
    engine_cc: 1450
    avg_consumption_urban: 7
    avg_speed_urban: 50
    avg_consumption: 4
    avg_speed: 90

What I have so far (I used this code using a more simple version of the auto.txt to read and display - see below for the simple version of my txt file):

This is my ifstream method in the Auto class

    friend ifstream& operator>>(ifstream& in, Auto &a)
        {
            delete[]a.engine;
            a.engine = new char[10];
            in >> a.engine;
            in >> a.max_speed;
            in >> a.engine_cc;
            in >> a.avg_consumption_urban;
            in >> a.avg_speed_urban;
            in >> a.avg_consumption;
            in >> a.avg_speed;
            return in;
        }

And this is how I read from the file in void main

    ifstream f("auto.txt", ios_base::in);
        f >> auto1;
        auto1.display();
        f.close();

This is the text I read from. This is a simplified version.

    gasoline
    250
    1980
    11
    50
    8
    100

解决方案

In the file format, there is no efficient method to position to a given record. The records are variable sized for example, one has an engine speed of 100 (3 characters) while another has 80 (2 characters).

Since each field is on a separate line, you could read each text line searching for "engine" at the start of the text.

This project smells like it could use a database.

If you are having to read line by line, you might as well read record by record. That is, have the record object read its members from the file.

If you get the file position before you read a record, you could store the information into a std::map<unsigned int, Engine> and use the file pointer as the index into the map.

Did I say this smells like a it could use a database?

Another idea is to read in all the records and shove them into a std::vector. Create a std::map<unsigned int, unsigned int> to map vector indices to file positions.

Still smells like you should use a database.

By placing the Engine records into a vector you could create other index tables (Databases can create index tables for you). For example, a std::map<string, unsigned int> to map engine types to vector indices.

Look into Database theory, especially Normal Forms. You would benefit a lot from this knowledge when you use a database.

这篇关于从文件中,如何从C ++中的某个点读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆