So i have an assignment which basically is to create a simulator for bench files which they have in a text file the stucture of a circuit with gates.
And i thought to take the file, read every line of the text and create vector and store every line in a every vector.
I've already done some code that can read the file but i dont know how to take every line and pun into a vector.
Is this code right?
Code tags would help, but I see what might be a major flaw anyway. You are not storing the file data in your vector, you are storing the number of lines read. Increasing the number as each line is read.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::ifstream myfile("test_data.txt");
if (myfile.is_open())
{
std::string line;
std::vector<std::string> v;
while (std::getline(myfile, line))
{
v.push_back(line);
}
myfile.close();
std::cout << "TOTAL: " << v.size() << '\n';
for (size_t index { }; index < v.size(); index++)
{
std::cout << "Line #" << index + 1 << " - " << v[index] << '\n';
}
}
else
{
std::cout << "Unable to open file\n";
}
}
TOTAL: 3
Line #1 - This
Line #2 - is a
Line #3 - quick test
You had the right idea, your if/else file handling was good. You just had some logic errors for reading and storing the data into the vector. Not bad if this was your first effort.
Indeed it was, thank you though!
Now my goal is to seperate the words of the lines thate are stored in the vector and ignore words that i dont want from the text file and make each word of the file to do a job(ex. "NAND" to calculate like a nand gate) .
For this to do, is with string manipulation?
I'd personally use a string stream since you are already dealing with a std::string.
One advantage strtok has over a string stream is the ability to use a list of delimiters. That makes taking a sentence with punctuation easier to split into individual words.
Once you can split a string into individual words how you deal with each word is the next step.
In my experience learning to program has a steep learning curve. Hard, hard, hard, hard, hard, hard....wow, that wasn't so bad.
Much "easy to learn" material, be it books or online tutorials, teach C++ as if it is a weird variant of C. Teach regular arrays before vectors, C strings before std::strings, rand instead of the rich ecosystem <random> offers, etc.
Keep plugging away. If programming is your "thing," it should get less hard the more you are exposed to what C++ has to offer.
One huge pet peeve of mine, instructors insisting on usingnamespace std;. Notice I don't have it in my example.
I will do my best and i hope it get easier soon cause i have a deadline haha! :P
I Have another one question though. When i want to ignore line from a text file what do i do?
Whenever it sees this character for example : '#' to ignore all the line.
Now in my code i have each line of the text file into a vector v[index] .
How to ignore a line?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::ifstream myfile("test_data.txt");
if (myfile.is_open())
{
std::string line;
std::vector<std::string> v;
while (std::getline(myfile, line))
{
std::cout << "Read: " << line << '\n';
if (line[0] == '#')
{
// go back to the start of the loop without adding to the vector
continue;
}
v.push_back(line);
}
std::cout << '\n';
myfile.close();
std::cout << "TOTAL: " << v.size() << '\n';
for (size_t index { }; index < v.size(); index++)
{
std::cout << "Line #" << index + 1 << ": " << v[index] << '\n';
}
}
else
{
std::cout << "Unable to open file\n";
}
}
Read: # ignore me!
Read: This
Read: is a
Read: # go away!
Read: # get lost!
Read: quick test
Read: # nothing to see here!
Read: more text
TOTAL: 4
Line #1: This
Line #2: is a
Line #3: quick test
Line #4: more text