473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

vector<string> to array question: very new to this

4
Hello,

This is my first attempt at a c++ program, and it is a long post, please bear with me.
I'm trying to read in a text file containing a firewall log, make the information searchable by an element (protocol, remote ip etc). The file is of known max width (65) containing 8 columns of known max size. The columns are seperated by white space. However, it is of unknown length. I read that to input this file in, I should use a vector. There are several commented out lines that I used to check my progress while mucking thru my attempt.
I've managed to ask for the filename, open the file and dump it into a vector<string>. This is where I hit the sensory overload wall:

What is correct way to step-thru a vector<string>? Using the simple for loop with for(int i=0;i<vector.size();i++) works; however, I've also read about using 'vector::iterator' to initilize vector step thrus.

Is it possible to put my vector into a two dimentional char array to manipulate or search the data. Is it possible to do this searching directly on the vector, by making a struct of the knowns elements and then vector<struct name>?

As you can see, I've reached the new user overload. Pointing me in a starting direction would be great and thanks for your help.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int main () {
  8.     vector<string> AllReadyReadin;
  9.     // typedef vector<double>::size_type vector_sizeVariable;
  10.     char filename[81];
  11.     string input_line;
  12.  
  13.     cout << "Enter Filename to read: ";
  14.     cin >> filename;
  15.  
  16.     // cout << "name of file to read is: " << filename << endl;
  17.  
  18.     //open file and see if it exsists
  19.     ifstream file_toread(filename);
  20.  
  21.     if (!file_toread.is_open()) {
  22.         cout << filename << " Could not be opened. " << endl;
  23.         return -1;
  24.     }
  25.  
  26.     while (!file_toread.eof()) {
  27.  
  28.     getline(file_toread, input_line);
  29.     //temp check of file contents
  30.     //cout << input_line << endl;
  31.     AllReadyReadin.push_back(input_line);
  32.     }    
  33.     file_toread.close();
  34.  
  35.     //various ways to print out size of array
  36.  
  37.     //vector_sizeVariable size = AllReadyReadin.size();
  38.     //cout << "Read in " << size << " lines" << endl;
  39.  
  40.     // cout << "Read in " << AllReadyReadin.size() << " lines" << endl;
  41.  
  42.     //dump vector to check contents
  43.         //for(int i = 0;i < AllReadyReadin.size(); i++)
  44.     //    cout << AllReadyReadin[i] << endl;
  45.  
  46.     return 0;
  47.  
  48. }
Sep 12 '07 #1
5 2440
Ganon11
3,652 Expert 2GB
I've always just used array subscript notation and a for...loop, as you have in your code.
Sep 12 '07 #2
gpraghuram
1,275 Expert 1GB
Hello,

This is my first attempt at a c++ program, and it is a long post, please bear with me.
I'm trying to read in a text file containing a firewall log, make the information searchable by an element (protocol, remote ip etc). The file is of known max width (65) containing 8 columns of known max size. The columns are seperated by white space. However, it is of unknown length. I read that to input this file in, I should use a vector. There are several commented out lines that I used to check my progress while mucking thru my attempt.
I've managed to ask for the filename, open the file and dump it into a vector<string>. This is where I hit the sensory overload wall:

What is correct way to step-thru a vector<string>? Using the simple for loop with for(int i=0;i<vector.size();i++) works; however, I've also read about using 'vector::iterator' to initilize vector step thrus.

Is it possible to put my vector into a two dimentional char array to manipulate or search the data. Is it possible to do this searching directly on the vector, by making a struct of the knowns elements and then vector<struct name>?

As you can see, I've reached the new user overload. Pointing me in a starting direction would be great and thanks for your help.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int main () {
  8.     vector<string> AllReadyReadin;
  9.     // typedef vector<double>::size_type vector_sizeVariable;
  10.     char filename[81];
  11.     string input_line;
  12.  
  13.     cout << "Enter Filename to read: ";
  14.     cin >> filename;
  15.  
  16.     // cout << "name of file to read is: " << filename << endl;
  17.  
  18.     //open file and see if it exsists
  19.     ifstream file_toread(filename);
  20.  
  21.     if (!file_toread.is_open()) {
  22.         cout << filename << " Could not be opened. " << endl;
  23.         return -1;
  24.     }
  25.  
  26.     while (!file_toread.eof()) {
  27.  
  28.     getline(file_toread, input_line);
  29.     //temp check of file contents
  30.     //cout << input_line << endl;
  31.     AllReadyReadin.push_back(input_line);
  32.     }    
  33.     file_toread.close();
  34.  
  35.     //various ways to print out size of array
  36.  
  37.     //vector_sizeVariable size = AllReadyReadin.size();
  38.     //cout << "Read in " << size << " lines" << endl;
  39.  
  40.     // cout << "Read in " << AllReadyReadin.size() << " lines" << endl;
  41.  
  42.     //dump vector to check contents
  43.         //for(int i = 0;i < AllReadyReadin.size(); i++)
  44.     //    cout << AllReadyReadin[i] << endl;
  45.  
  46.     return 0;
  47.  
  48. }
HI,
1)You can iterators to iterate through the vectors.
syntax is
Expand|Select|Wrap|Line Numbers
  1.   vector<string> str_vect;
  2.   vector<string>::const_iterator str_iter;
  3.   for(str_iter=str_vect.begin();str_iter!=str_vect.end();++str_iter)
  4.   {
  5.       \\*str_iter will have one stored line.
  6.   }
  7.  
2)You can aslo use vector to store the structure

Thanks
Raghuram
Sep 12 '07 #3
Etrex
4
Thank you :)

I can loop thru a vector using a normal index.
Also I can loop thru a vector using a vector<type>::const_iterator that represents an address within a vector for use with *.

Now to stuff the vector into an array for information parsing and gathering.

bob
Sep 12 '07 #4
gpraghuram
1,275 Expert 1GB
Thank you :)

I can loop thru a vector using a normal index.
Also I can loop thru a vector using a vector<type>::const_iterator that represents an address within a vector for use with *.

Now to stuff the vector into an array for information parsing and gathering.

bob
Hi,
R u asking for more help or u are describing the task you are about to do?

Thanks
Raghuram
Sep 13 '07 #5
Etrex
4
Sorry was thinking out loud with last post :).

What it boils down to is, I want to have access to individual columns contained within the file with the ability to search by protocol, local IP etc The file has the form of the following on each line, each column entry is seperated by a whitespace:

Expand|Select|Wrap|Line Numbers
  1. struct mystruct {
  2.     char date[10];
  3.     char time[11];
  4.     char direction[1];
  5.     char remoteip[15];
  6.     char remoteport[5];
  7.     char localip[15];
  8.     char localport[5];
  9.     char protocol[3];
  10. };
The file is of type string with unknown length. I started by reading the file into a vector<string> to understand the basics of reading / writing from a vector. It would be nice if while reading in the file into a vector, the columns would be parsed into individual accessable names (mystruct) or After reading in the file into a string vector, put the information into a searchable struct.

This idea of making my firewall logs searchable is the reason I took up c++. I am currently reading accelerated c++ and c++ w/o fear and I am 'getting it' so far. I read and do the exercises until my brain screams mercy. I then pop back to this program and delve into online resources to try and figure it out. I believe that once I write the firewall log program, it will be a huge boost to my 'get it' pyschy and change my confused look to a smile heh.

thanks,
bob
Sep 18 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
5
by: Gary Wessle | last post by:
whats an efficient way to copy a string to a vector<string>? how about this? #include <iostream> #include <string> #include <vector> Using namespace std;
5
by: Martin Jørgensen | last post by:
Hi, The piece of code I'm struggling with is so simple, that I hope nobody wants a complete example for answering the question: -------- string color_line; int data_type = 0; for(...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks!...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
9
by: barcaroller | last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the return value is an iterator, can I convert that iterator to a pointer? If yes, how? 2. What is the internal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.