my program wont read vector array

This is a discussion on my program wont read vector array within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that would put all values not already in the array into the array. However, when ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    24

    Question my program wont read vector array

    I am writing a program that would put all values not already in the array into the array. However, when I call array after all values have been inserted, the array does not cout any values.
    What is possibly wrong?

    Note: the program below extracts values from lines of data (values are separated by commas) and places all new nonnumerical values into arrays - each array for each nonnumerical column.

    Code:
    #include <string>
    #include<iostream.h>
    #include<fstream.h>
    #include<ctype.h>
    #include"lvp/vector.h"
    int main()
    {
            const int BUFF_SIZE=100000;
            int a;
            ifstream infile;
            ofstream outfile;
            char buff[BUFF_SIZE];
            vector<string>col1;
            vector<string>col2;
            vector<string>col3;
            vector<string>col41;
    
    // open the files
            infile.open("/misc/tmp/kddcup.data_10_percent");
            outfile.open("output.txt");
    // make sure the files are open
            if(!infile.is_open()){
                    cerr << "error opening input file";
                    return 1;
            }
            if(!outfile.is_open()){
                    cerr << "error opening output file";
                    return 1;
            }
    // loop until the end of the input file
            for(a=0; !infile.eof();){
    // read in one line
                    char *p;
                    char word[20] = "";
                    infile.getline(buff,200);
                    p = strtok(buff,",");
                    for(int i=0; i<42; i++)
                    {
                            if(i==1)
                            {
                                    strcpy(word,p);
                                    bool found = false;
                                    for(int k=0; k<col1.length() && !found; k++)
                                    {
                                            if ( col1[k] == word ) found = true;
                                    }
                                    if ( !found )
                                    {
                                            int pos1 = col1.length();
                                            col1.resize(pos1+1);
                                            col1[pos1]=word;
                                    }
                            }
                            if(i==2)
                            {
                                    strcpy(word,p);
                                    bool found = false;
                                    for(int l=0; l<col2.length() && !found; l++)
                                    {
                                            if ( col2[l] == word ) found = true;
                                    }
                                    if ( !found )
                                    {
                                            int pos2 = col2.length();
                                            col2.resize(pos2+1);
                                            col2[pos2]=word;
                                    }
                            }
                            if(i==3)
                            {
                                    strcpy(word,p);
                                    bool found = false;
                                    for(int m=0; m<col3.length() && !found; m++)
                                    {
                                            if ( col3[m] == word ) found = true;
                                    }
                                    if ( !found )
                                    {
                                            int pos3 = col3.length();
                                            col3.resize(pos3+1);
                                            col3[pos3]=word;
                                    }
                            }
                            if(i==41)
                            {
                                    strcpy(word,p);
                                    bool found = false;
                                    for(int n=0; n<col41.length() && !found; n++)
                                    {
                                            if ( col41[n] == word ) found = true;
                                    }
                                    if ( !found )
                                    {
                                            int pos41 = col41.length();
                                            col41.resize(pos41+1);
                                            col41[pos41]=word;
                                    }
                            }
                            p = strtok(NULL,",");
                    }
                    a++;
            }
          for(int w=0; w<col2.length(); w++)
                  cout<<col2.length()<<endl;
    
    // close the files
            infile.close();
            outfile.close();
    // pause so you can see the output on the screen
            cout << " **** All done! ****\n";
            cin.get();
            return 0;
    }
    for(int w=0; w<col2.length(); w++)
    cout<<col2.length()<<endl;
    at the end of code does not return any values.
    Instead there is the segmentation fault.

    What should I do?

    thanks
    Last edited by MKashlev; 08-10-2002 at 11:53 AM.
    Dmitry Kashlev

  2. #2
    kuphryn
    Guest
    I did not read over your code, but include this.

    -----
    using std::string
    -----

    Kuphryn

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Source File
    Code:
    #include "helloClass.h"
    #include <iostream>
    #include <cctype>
    #include <string>
    #include <vector>
    #include <fstream>
    
    using std::cout;
    using std::vector;
    using std::string;
    using std::cin;
    
    int main()
    {
      string s;
      ifstream infile;
      infile.open("textfile");
      if ( ! infile.is_open() ) exit(1);
    
      while ( getline( infile, s) )
      {
        vector<string> v = Split(s);
        for ( vector<string>::size_type i = 0; i < v.size(); ++i)
          cout << v[i] << endl;
      }
      infile.close();
      return 0;
    }
    
    vector<string> Split (const string& s)
    {
      vector<string> ret;
      string::size_type i = 0;
    
      while ( i != s.size() )
      {
        while ( i != s.size() && ispunct(s[i]) ) ++i;
    
        string::size_type j = i;
    
        while ( j != s.size() && !ispunct(s[j]) ) ++j;
    
        if ( i != j )
        {
          ret.push_back( s.substr(i, j - i ) );
          i = j;
        }
      }
      return ret;
    }

    Header file
    Code:
    #ifndef _WoahXX
    #define _WoahXX
    #include <vector>
    #include <string>
    
    vector<string> Split(const string& s);
    
    
    #endif

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    24

    Arrow

    your code was really helpful with ridding from segmentation faults, but it extracted all values rather than nonnumerical values from data (I need only 2nd, 3rd, 4th, and 42nd columns). Values from each of these 4 columns should be stored in 4 different arrays. these values must be different from each other, and same values are not recorded into array. See my code in the first message.
    Example of one line in data is:
    0,tcp,http,SF,181,5450,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,8,8,0.00 ,0.00,0.00,0.00,1.00,0.00,0.00,9,9,1.00,0.00,0.11, 0.00,0.00,0.00,0.00,0.00,normal.

    I am very inexperienced with STL and :: types.

    Thanks,

    ~D
    Dmitry Kashlev

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    If you just want the alphabetic words, than add the isalpha check to the code. I have to leave for work right now!

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    24
    thanks...
    Dmitry Kashlev

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I read my program on gcc but have to run on bcc...
    By philae in forum C Programming
    Replies: 4
    Last Post: 03-06-2006, 08:28 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Program to help you read faster
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-19-2003, 03:06 PM
  4. Read a file into a loop program
    By TimeClock in forum C Programming
    Replies: 5
    Last Post: 07-17-2003, 07:29 PM
  5. read data
    By in_need in forum C Programming
    Replies: 2
    Last Post: 10-30-2001, 08:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21