reading in an empty field

This is a discussion on reading in an empty field within the C++ Programming forums, part of the General Programming Boards category; I have the following code to read data from a comma-delimited file. This works as long as each field is ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    reading in an empty field

    I have the following code to read data from a comma-delimited file. This works as long as each field is present. However, if I get a line like 1234,fname,lname,,01,0,123456789 then the program dies on the null at the time of the push_back. How can I convert that null to a space?

    Code:
    		void initialize_vector(std::vector<record*>& vec)
    		{
    			
    			char detailline[100];
    			std::string sdetailline;
    			char * s; // element placeholder
    			char * dup_str;
    			std::string tempstring;
    			int colcount = 0;
    			int daysapart = 0;
    			
    			//assign tmp as pointer to record struct
    			record* tmp;
    			std::ifstream fin;
    			fin.open ("zz.csv", std::ios::in);    
    			if(fin.fail())
    				{
    				throw read_exception();
    				}
    			while(!fin.fail() && !fin.eof())
    				{
    				fin.getline(detailline, 100, '\n');
    				if (detailline[0] != NULL) 
    					{
    					dup_str = strdup(detailline); // make (malloc) copy of input string
    					s = dup_str; // first element is at start
    					strtok(s, ","); // first element is now NUL-terminated
    					colcount++;
    					tmp = new record;
    					tempstring = s;
    
    					tmp->corp = static_cast<std::string>(s);
    
    					while (s != NULL) 
    					{
    						s = strtok(NULL, ","); // move to next
    						if (colcount == 1)
    						{
    							tmp->lastname = static_cast<std::string>(s);
    						}
    						else if (colcount == 2)
    						{
    							tmp->firstname = static_cast<std::string>(s);
    						}
    						else if (colcount == 3)
    						{
    							tmp->lwi = static_cast<std::string>(s);
    						}
    						else if (colcount == 4)
    						{
    							tmp->grade = static_cast<std::string>(s);
    						}
    						else if (colcount == 5)
    						{
    							tmp->tanf = static_cast<std::string>(s);
    						}
    						else if (colcount == 6)
    						{
    							tmp->ssn = static_cast<std::string>(s);
    						}
    						colcount++;
    					}
    					colcount = 0;
    					//std::cout << tmp->lastname<<std::endl;
    					vec.push_back(tmp);
    					free(dup_str); // release copy - you should have made copies of any required elements before doing this !
    					}
    				}
    			fin.close();
    		}

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I'm not sure what the best solution is for your strtok and strdup functions, but I do know that using C++ stringstreams would work well. All you would do is check the return value of the stream's getline (used with ',' as the delimiter) to see if something was read in.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    thanks, I'll see what I can find.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    COuldn't turn up much but I did remember I have a split function that would work here and get me the functionality i need.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Here is a little snippet of your code re-written with C++ only. IMO it is easier, you just have to get familiar with the tools.
    Code:
    while (getline(fin, sdetailline))
    {
      int colcount = 0;
      tmp = new record;
      std::istringstream istrLine(sdetailline);
      while (getline(istrLine, tempstring, ',')
      {
        switch (colcount)
        {
          case 0:
            tmp->corp = tempstring;
            break;
          case 1:
            tmp->lastname = tempstring;
            break;
          case 2:
            tmp->firstname = tempstring;
            break;
          // ...
          case 6:
            tmp->ssn = tempstring;
            break;
        }
        colcount++;
      }
      vec.push_back(tmp);
    }

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    thanks, I'll read up on istringstream...and eat a little lunch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get field of the screen
    By lorenzi in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2009, 01:19 PM
  2. Looking for a little assistance with item class design...
    By Raigne in forum Game Programming
    Replies: 5
    Last Post: 10-22-2008, 08:55 AM
  3. Please critique and suggest improvements for parser
    By Queue in forum C Programming
    Replies: 14
    Last Post: 09-28-2006, 08:28 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Graphing a direction field and intensity map
    By InnerCityBlues in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2004, 09:59 PM

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