简体   繁体   中英

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_; is in gate class and void set_as_output(); is in pin class 2 represent no of pins,1 width of first pin and 16 width of second pin. 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.

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 ).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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