How to put data from text files in a vector?

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?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main () {
	
  int count=0;
  string line;
  ifstream myfile ("c17.bench");

  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
    	vector <int> v;

    	v.push_back(count); 
    	for(int i=0; i<v.size();i++)
		{
			cout<<	v[i] << " " << "\t";
			cout<< line << endl;
		}
    	count++;
		
    }
        
    myfile.close();
    
    cout << "TOTAL: " << count << endl;
        
  }
    else cout << "Unable to open file"; 


  return 0;
}
Last edited on
Edit your post and put code tags around your code to preserve indentation. Like this:

[code]
your code here
[/code]
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.

Using a very simple test data file of my own:
1
2
3
This
is a
quick test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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.
Last edited on
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?
There are several different ways to separate out individual words from a string of words.

Recursively use a C string function, strtok, on each stored std::string to tokenize to individual words.
http://www.cplusplus.com/reference/cstring/strtok/

Use a std::stringstream constructed from each stored string and then extract each word from the string stream as you would from the keyboard with std::cin.
https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt

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.
I found programming very very hard though but I will try my best. Thank you my friend!
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 using namespace std;. Notice I don't have it in my example.

https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

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?
Updated data file:
1
2
3
4
5
6
7
8
# ignore me!
This
is a
# go away!
# get lost!
quick test
# nothing to see here!
more text

Crude updated source (it ain't pretty):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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
Thank you!!
Topic archived. No new replies allowed.