46558

read an input file using vectors

Question:

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.

Answer1:

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 <em>after</em> 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 <em>anything</em>? This has be fairly baffled.

Answer2:

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(); }

Answer3:

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; }

<strong>This implementation is using arrays but it didn't work,although there isn't any compling error.</strong>

Recommend

  • Cannot get my python script to run on startup/from a shortcut on raspberry pi
  • PHP contact form sending email every time the page is refreshed
  • Sharepoint 2010 migration to sharepoint online using sharegate
  • stm32f0 uart programming
  • Create Prolog Vocabulary
  • python: subprocess calling a script which runs a background process hanging
  • GATE - How to create a new annotation SET?
  • PHP - filesize() returns an empty string
  • Load LibSVM's Model File in Matlab
  • Can the NTFS $MFT file have child records?
  • Unable to insert array into database
  • Uploader produces errors regarding the column in which it should store the path
  • Jquery Ajax call not executed in for loop
  • XSLT removing arbitrary duplicate sibling elements
  • find a file in a nested directory structure
  • How can I create interrupts in C for Arduino?
  • How can the java 'class' literal return different instances of the Class object for the sa
  • In BASH convert a string with . in float
  • Fail SonarQube quality gate when coverage decreases
  • Insert Columns between columns in excel
  • Is there a way to link a linux's thread TID and a pthread_t “thread ID”
  • Loop through each key and value of php multidimensional array
  • why my app based on boost::asio didn't accept new connection
  • NSMutableArray instance used in a block
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • To display the title for the current loaction in map in iphone
  • Comma separated Values
  • Java static initializers and reflection
  • LevelDB C iterator
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?