Link to home
Start Free TrialLog in
Avatar of tpiazza
tpiazza

asked on

Parsing a text file

new to c++

i am reading in a text file and i am trying to get it in 15 line blocks and then set that to an array which i then grab some elements from

i can open the file ok, use getline to get the line -- what i am not sure of is how to grab 15 lines in the text file and then set that to an array


Avatar of n_fortynine
n_fortynine

I assume you've known how to use getline to get a line. Then what's wrong with using a loop counting to 15 to do this 15 times (you might want to use a 2 dimensional array maybe)?
(1) An array of what? If you're using getline you're either getting characters (in a buffer of some sort: a char array?) or std::string. So, depending on how you use getline (you'll want to show us your code) and what you want an array of, the answer is different.

(2) If you KNOW there will always be 15 lines to read, you can use a simple count-controlled loop (a for loop) to read 15 lines. I guess I am not sure where that part of the assignment is hanging you up.

-bcl
Avatar of tpiazza

ASKER

here is what i have

basically i need to read through the comma delimited file line by line and find the one that has KTC as its first value  then read that whole line and output it


#include <iostream.h>
#include <string.h>    
#include <fstream.h>
#include <stdlib.h>

int main ()

{
  char buffer[1000];
  char dataline[1000];

  ifstream thefile ("c://file.txt");
 
  if (! thefile.is_open())
  { cout << "Error opening file"; exit (1); }

  while (! thefile.eof() )
  {
      thefile.getline ( buffer, 100, ',');

      if(strcmpi("KTC", buffer))  
      {
      
      thefile.getline (dataline, 500, ',');
      cout << dataline << endl;

      }
   
            
  }
  return 0;
}
Avatar of tpiazza

ASKER

in vb there is a split function -- i cant find one for c++
Why don't you use std::string? There is a substr method that allows you to extract a substring of that string.

string str = "ABCDE";
cout << str.substr(0,3) << endl;
From Koenig and Moo's _Accelerated C++_ (http://www.acceleratedcpp.com/):

using std::find_if;
using std::string;
using std::vector;

using std::isspace;

// `true' if the argument is whitespace, `false' otherwise
bool space(char c)
{
      return isspace(c);
}

// `false' if the argument is whitespace, `true' otherwise
bool not_space(char c)
{
      return !isspace(c);
}

vector<string> split(const string& str)
{
      typedef string::const_iterator iter;
      vector<string> ret;

      iter i = str.begin();
      while (i != str.end()) {

            // ignore leading blanks
            i = find_if(i, str.end(), not_space);

            // find end of next word
            iter j = find_if(i, str.end(), space);

            // copy the characters in `[i,' `j)'
            if (i != str.end())
                  ret.push_back(string(i, j));
            i = j;
      }
      return ret;
}


This actually splits on spaces (and uses std::string and iterators all over the place) but by modifying the code for the boolean functions you should be able to make it split anything you want.

The book is really, really good, too. (A readable language introduction.)

Hope this helps, -bcl
And use
getline(thefile, str, ','); instead of thefile.getline(...); because the latter does not take a string parameter.
Avatar of tpiazza

ASKER

how do i read in the stream set to an array and then grab the element

getline is giving me whatever char is at the place i specify.  i need the whole element

#include <iostream.h>
#include <string.h>    
#include <fstream.h>
#include <stdlib.h>



int main ()

{      char buffer[100];
 
  ifstream thefile ("c://file.txt");
 
  if (! thefile.is_open())
  { cout << "Error opening file"; exit (1); }

  while (! thefile.eof() )
  {
         thefile.getline ( buffer,  '/n');
      
  cout << buffer[4] << endl;
            
  }
  return 0;
}
Your filename should be "c:\\file.txt" or "c:/file.txt", not "c://file.txt". And it's '\n' not '/n'. And you are displaying the buffer's 4th character. try cout << buffer << endl;
Avatar of tpiazza

ASKER

i know im displaying the fourth character -- how do I get the forth element

each line looks like

ktc, 3,3,11,27,00,040,00,29,66,108,50,30,06,227,00*4D

when it grabs this using getline how do i set it to an array and then grab the fourth element?

well, that's simple then.
You can grab the first string "ktc" or whatever it is, using getline.
thefile.getline(buffer, <size goes here>, ','); //get up to the comma.
after that you can use >> to get the numbers.
int num;
char ch;
for(int i = 0; i < 4; ++i) {
   thefile >> num; //get the number after 4 times you will get in sequence: 3 3 11 27.
   thefile >> ch;  //get the comma. Note that >> skips spaces.
}

//now num contains 27.
Works for you?
Avatar of tpiazza

ASKER

it goes to some wierd loop and spits nothing but zero out

i deleted everythng out of the file except

ktc, 3,3,11,27,00,040,00,29,66,108,50,30,06,227,00*4D

here is what i have:

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>



int main ()

{      char buffer[500];
      int num;
      char ch;

      ifstream thefile;
      thefile.open ("c:\\file.txt", ios::in);

  if (! thefile.is_open())
  { cout << "Error opening file"; exit (1); }

  while (! thefile.eof() )
  {

         thefile.getline(buffer, ','); //get up to the comma.

      for(int i = 0; i < 4; ++i)
      {
   
            thefile >> num;
            thefile >> ch;  
      }
      
  cout << num  << endl;
 
  }

  thefile.close();
  return 0;
}
Avatar of tpiazza

ASKER

thefile.getline(buffer, <size goes here>, ',');

the size can vary from 4 to 31 characters

i assume

thefile.getline(buffer, ','); works
You misused getline. Look at the syntax I cited:
>>thefile.getline(buffer, <size goes here>, ','); //get up to the comma.
so the called should have been getline(buffer, 499, ',');
you called getline(buffer, ',');
thus ',' is interpreted as a size, and yields an ASCII value of 50 something. That's why you can't get anything else because the entire line was read into buffer (',' was no longer a delimiter)
<size> stands for the maximum characters your array can sustain/ or the maximum you WANT to get into that array.
Avatar of tpiazza

ASKER

still get nothing but a bunch of zeros

even though i changed it to

thefile.getline(buffer, 499, ',');

      
The code worked fine for me:

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main () {
   char buffer[500];
   int num;
   char ch;

   ifstream thefile;
   thefile.open ("file.txt", ios::in);     /////Notice I changed the path because I was running this on remote UNIX
                                                    ////and it does not have access to my C Drive.
                                                    ////If you want you can change it back to what you have "C:\\thefile.txt"
   if (! thefile.is_open()) {
      cout << "Error opening file";
      exit (1);
   }

   while (! thefile.eof() ) {
      thefile.getline(buffer, 499, ','); //get up to the comma.

      for(int i = 0; i < 4; ++i) {
         thefile >> num;
         thefile >> ch;
      }

      cout << num  << endl;
  }

  thefile.close();
  return 0;
}

with the content of file.txt exactly like so:
ktc, 3,3,11,27,00,040,00,29,66,108,50,30,06,227,00*4D

The output I get is:
27  //The fourth number
66  //skip 00, get the fourth number after that. which is 66
227 //skip 108, get the fourth number after that. which is 277
227 //skips 00*4D, and get to end of file. Thus num remains the same as 277.

Please check your syntax again and let me know :)
Avatar of tpiazza

ASKER

i cut and pasted your code

now i get -858993460

Avatar of tpiazza

ASKER

please delete -- someone else answered this
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
The code I posted, worked fine for me. The asker never followed up, simply just said "someone else answered this." That's reasonable enough =/, is it?.
ASKER CERTIFIED SOLUTION
Avatar of SpazMODic
SpazMODic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial