Author |
Message |
jin
|
Posted: Sat Nov 04, 2006 5:36 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Sat Nov 04, 2006 7:11 pm Post subject: (No subject) |
|
|
You could use std::strings instead of character strings. And use std::vector instead of an array, it automatically manages it's size. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Nov 04, 2006 7:16 pm Post subject: (No subject) |
|
|
Unless of course this question is limited to C. |
|
|
|
|
![](images/spacer.gif) |
jin
|
Posted: Sat Nov 04, 2006 7:34 pm Post subject: (No subject) |
|
|
thnx for the reply but i havent yet learned about std::string and std::vector. i think i might have gotten it.
code: |
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 |
|
|
|
|
![](images/spacer.gif) |
jin
|
Posted: Sat Nov 04, 2006 9:45 pm Post subject: (No subject) |
|
|
File contents are:
abc <space> def <space><space>ghi <enter>
the output i get is:
abc
def
<empty line>
ghi
the output i need is:
abc
def
ghi
thnx. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sun Nov 05, 2006 9:51 am Post subject: (No subject) |
|
|
First off, let's try formatting this code nicely.
code: | 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:
code: | 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? |
|
|
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: Sun Nov 05, 2006 10:06 am Post subject: (No subject) |
|
|
@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. |
|
|
|
|
![](images/spacer.gif) |
jin
|
Posted: Sun Nov 05, 2006 10:45 am Post subject: (No subject) |
|
|
thnx for all your help i got it working now. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|