Thread: Assigning string tokens to an array

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    1

    Assigning string tokens to an array

    Good day,


    I need to dynamically build a control file for a SQL loader but when tokenizing line values read from a file i am unable to work/assign the last value to a variable even though i can print the actual token value while in the loop.


    Below is the code snippet i have and would appreciate all the help i can get:

    Code:
    //headElement declared as headElement[42]
    //getCol function is used to decode column keys present in line being read
    istringstream ss(line);
    
    
    wfile << "LOAD DATA " << endl <<
     "INFILE * " << endl <<
    "APPEND INTO TABLE cdruser.x " << endl <<
     "FIELDS TERMINATED BY ',' " << endl <<
    "(" << endl;
    
    
    while (getline(ss, token, ','))
    {
    //cout << arrayCountHeader << " " <<token<<endl;
    headElement[arrayCountHeader] = getCol(token);
    cout<<token<< " col " <<getCol(t1)<<endl;
    if (arrayCountHeader == (sizeof(headElement) / sizeof(headElement[0])) - 1)
    {
    wfile << headElement[arrayCountHeader] <<",FILENAME) BEGINDATA " << endl;
    wfile.close();
    }
    
    
    wfile.open("test.txt", std::ios_base::app | std::ios_base::out);
    wfile << headElement[arrayCountHeader] << "," << endl;
    wfile.close();
    
    
    arrayCountHeader ++;
    
    
    }
    I've attached a sample file as well.

    Regards,
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the first thing is learn how to indent code.
    Code:
    //headElement declared as headElement[42]
    //getCol function is used to decode column keys present in line being read
      istringstream ss(line);
    
      wfile << "LOAD DATA " 
            << endl 
            << "INFILE * " 
            << endl 
            << "APPEND INTO TABLE cdruser.x " 
            << endl 
            << "FIELDS TERMINATED BY ',' " 
            << endl 
            << "(" 
            << endl;
    
      while (getline(ss, token, ',')) {
    //cout << arrayCountHeader << " " <<token<<endl;
        headElement[arrayCountHeader] = getCol(token);
        cout << token << " col " << getCol(t1) << endl;
        if (arrayCountHeader == (sizeof(headElement) / sizeof(headElement[0])) - 1) {
          wfile << headElement[arrayCountHeader] << ",FILENAME) BEGINDATA " << endl;
          wfile.close();
        }
    
        wfile.open("test.txt", std::ios_base::app | std::ios_base::out);
        wfile << headElement[arrayCountHeader] << "," << endl;
        wfile.close();
        arrayCountHeader++;
      }
    Next, there is nothing in this code to stop arrayCountHeader going out of bounds.

    Also, it's very unclear what the lifetime of wfile is. In two places that I can see, it isn't obvious that wfile is even open.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strtok-assigning tokens to structure
    By anya in forum C Programming
    Replies: 9
    Last Post: 12-13-2012, 03:14 AM
  2. Replies: 9
    Last Post: 09-19-2007, 02:26 AM
  3. string tokens
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-14-2004, 10:54 AM
  4. string tokens
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-16-2001, 12:30 PM
  5. Splitting a string into tokens
    By unregistered in forum C Programming
    Replies: 4
    Last Post: 11-06-2001, 02:50 PM