简体   繁体   English

使用向量读取输入文件

[英]read an input file using vectors

I have written a code to read file below but its not working. 我已经编写了一个代码来读取下面的文件,但它不起作用。 Input file: 输入文件:

2 1 16
16 0 0
1 1 1234
16 0 0
1 1 2345

code is: 代码是:

std::ifstream input_file;
evl_wire wire;
int num_pins,width,cycles,no;
std::vector<int>IP;
while(input_file)
{
    input_file >> num_pins;//num_pins=2
    if (pins_.size() != num_pins) return false;
    for (size_t i = 0; i < pins_.size(); ++i)
    {
        input_file >> width;//width=1 for 1=0 ,=16 for i=2
        if (wire.width != width) return false;
        pins_[i]->set_as_output();
    }
    for (size_t i = 1; i < file_name.size(); i=i+1)
        input_file>>cycles;
    input_file>>no;
    pins_=IP;
}

where std::vector<pin *> pins_; 其中std::vector<pin *> pins_; is in gate class and void set_as_output(); 是在门类和void set_as_output(); is in pin class 2 represent no of pins,1 width of first pin and 16 width of second pin. 在引脚类2中表示没有引脚,第一引脚的宽度为1,第二引脚的宽度为16。 here from second line in file 16 is no of cycles pins must remain at 0 0,for next 1 cycle pins must be assigned 1 and 1234 as inputs. 此处从文件16中的第二行开始,周期引脚必须保持为0 0,对于下一个周期,引脚必须分配1和1234作为输入。

I don't fully understand your code, but I don't see you are opening the input file anywhere. 我不完全理解你的代码,但我没有看到你在任何地方打开输入文件。 I think it should be: 我认为它应该是:

std::ifstream input_file;
evl_wire wire;
int num_pins,width,cycles,no;
std::vector<int>IP;
input_file.open("name of the file");
if(input_file.is_open())
{
    while(input_file >> num_pins) //num_pins=2
    {
        if (pins_.size() != num_pins) return false;
        for (size_t i = 0; i < pins_.size(); ++i)
        {
            input_file >> width;//width=1 for 1=0 ,=16 for i=2
            if (wire.width != width) return false;
            pins_[i]->set_as_output();
        }
        for (size_t i = 1; i < file_name.size(); i=i+1)
            input_file>>cycles;
        input_file>>no;
        pins_=IP;
    }
    input_file.close();
}

Some parts of your code are almost certainly wrong. 代码的某些部分几乎肯定是错误的。 Other parts I'm less certain about -- they don't make much sense to me, but maybe I'm just missing something. 其他部分我不太确定 - 它们对我没有多大意义,但也许我只是遗漏了一些东西。

while(input_file)

This is almost always a mistake. 这几乎总是一个错误。 It won't sense the end of the file until after an attempt at reading from the file has failed. 尝试从文件读取失败之后 ,它才会检测到文件的结尾。 In a typical case, your loop will execute one more iteration than intended. 在典型情况下,循环将执行比预期更多的迭代。 What you probably want is something like: 你可能想要的是:

while (input_file >> num_pins)

This reads the data (or tries to, anyway) from the file, and exits the loop if that fails. 这将从文件中读取数据(或尝试,无论如何),如果失败则退出循环。

if (pins_.size() != num_pins) return false;

This is less clear. 这不太清楚。 It's not at all clear why we'd read num_pins from the file if we already know what value it needs to be (and the same seems to be true with width vs. wire.width ). 如果我们已经知道它需要什么值,那么我们从文件中读取num_pins原因一点也不清楚( widthwire.width )。

for (size_t i = 1; i < file_name.size(); i=i+1)
        input_file>>cycles;

This strikes me as the most puzzling part of all. 这让我感到非常困惑。 What does the size of the string holding the file name have to do with anything ? 保存文件名的字符串大小与任何内容什么关系 This has be fairly baffled. 这令人感到困惑。

The function I used: 我使用的功能:

bool input::validate_structural_semantics() 
    {
        evl_wire wire;
        std::ifstream input_file;std::string line;
        int x[]={1000};
        for (int line_no = 1; std::getline(input_file, line); ++line_no)
        std::string s; int i=0;
        std::istringstream iss; 
        do 
        { 
            std::string sub; 
            iss >> sub; 
            x[i]=atoi(sub.c_str());
            i++;
        }
        while (iss); 
        if (pins_.size()!=x[0]) return false;
        for (size_t i = 0; i < pins_.size(); ++i)
        {
            if (wire.width != x[i+1]) return false;
            pins_[i]->set_as_input();
        }
        for(size_t i=4;i<1000;i++)
        {
            for(size_t j=0;j<pins_.size();j++)
                pins_.assign(x[i-1],x[i+j]);
        }
        return true;
    }

This implementation is using arrays but it didn't work,although there isn't any compling error. 此实现使用数组,但它不起作用,尽管没有任何编译错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM