
-----------------------------------
jin
Sat Nov 04, 2006 5:36 pm

cstring question
-----------------------------------
Hey i am tryin to read in a file and store the letters in a 2Ds ctstring. So that words[word number][number of letters]. In java i would have used a string tokenizer to determine when a word finishes and the next starts. Any help is appreciated.

thnx.

-----------------------------------
md
Sat Nov 04, 2006 7:11 pm


-----------------------------------
You could use std::strings instead of character strings. And use std::vector instead of an array, it automatically manages it's size.

-----------------------------------
wtd
Sat Nov 04, 2006 7:16 pm


-----------------------------------
Unless of course this question is limited to C.

-----------------------------------
jin
Sat Nov 04, 2006 7:34 pm


-----------------------------------
thnx for the reply but i havent yet learned about std::string and std::vector. i think i might have gotten it.



int y = 0;
       int x = 0;
       char letter;

       while (! inFile.eof())
      {
              inFile.get (letter);

              if (!isspace(letter))
              {
               words [x] [y] = letter;
               y++;
               }

               else
               {
               y=0;
               x++;
               }
     }



This is what i have so far and it works if there is one space between characters but if there is more. it saves the space in the variable. Any thoughts.

thnx

-----------------------------------
jin
Sat Nov 04, 2006 9:45 pm


-----------------------------------
File contents are:
abc  def  ghi 

the output i get is:
abc
def

ghi

the output i need is:
abc
def
ghi

thnx.

-----------------------------------
wtd
Sun Nov 05, 2006 9:51 am


-----------------------------------
First off, let's try formatting this code nicely.

int y = 0;
int x = 0;
char letter;

while (!inFile.eof())
{
   inFile.get(letter);

   if (!isspace(letter))
   {
      words[x][y] = letter;
      y++;
   }
   else
   {
      y = 0;
      x++;
   }
}

Now, let's step through what happens on input of:

abc def  ghi

x = 0
y = 0
letter = 'a'
'a' is not a space
words[0][0] = 'a'
y = 1

x = 0
y = 1
letter = 'b'
'a' is not a space
words[0][1] = 'b'
y = 2

x = 0 
y = 2
letter = 'c'
'c' is not a space
words[0][2] = 'c'
y = 3

x = 0
y = 3
letter = ' '
' ' is a space
y = 0
x = 1

x = 1
y = 0
letter = 'd'
'd' is not a space
words[1][0] = 'd'
y = 1

x = 1
y = 1
letter = 'e'
'e' is not a space
words[1][1] = 'e'
y = 2

x = 1
y = 2
letter = 'f'
'f' is not a space
words[1][2] = 'f'
y = 3

x = 1
y = 3
letter = ' '
' ' is a space
y = 0
x = 2

x = 2
y = 0
letter = ' '
' ' is a space
y = 0
x = 3

x = 3
y = 0
letter = 'g'
'g' is not a space
words[3][0] = 'g'
y = 1

x = 3
y = 1
letter = 'h'
'h' is not a space
words[3][1] = 'h'
y = 2

x = 3
y = 2
letter = 'i'
'i' is not a space
words[3][2] = 'i'
y = 3

Now, do you see where the error is occurring?

-----------------------------------
OneOffDriveByPoster
Sun Nov 05, 2006 10:06 am


-----------------------------------
@jin:

if using C++, look into iostream too;

if using C, look into scanf (careful what you do with it though).

In your current program:
To get rid of the extra spaces, simply read away all spaces before a word.
(A loop will do).  ungetc() can help.

-----------------------------------
jin
Sun Nov 05, 2006 10:45 am


-----------------------------------
thnx for all your help i got it working now.
