Computer Science Canada cstring question |
Author: | jin [ 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. |
Author: | md [ Sat Nov 04, 2006 7:11 pm ] |
Post subject: | |
You could use std::strings instead of character strings. And use std::vector instead of an array, it automatically manages it's size. |
Author: | wtd [ Sat Nov 04, 2006 7:16 pm ] |
Post subject: | |
Unless of course this question is limited to C. |
Author: | jin [ Sat Nov 04, 2006 7:34 pm ] | ||
Post subject: | |||
thnx for the reply but i havent yet learned about std::string and std::vector. i think i might have gotten it.
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 |
Author: | jin [ Sat Nov 04, 2006 9:45 pm ] |
Post 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. |
Author: | wtd [ Sun Nov 05, 2006 9:51 am ] | ||||||
Post subject: | |||||||
First off, let's try formatting this code nicely.
Now, let's step through what happens on input of:
Now, do you see where the error is occurring? |
Author: | OneOffDriveByPoster [ Sun Nov 05, 2006 10:06 am ] |
Post 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. |
Author: | jin [ Sun Nov 05, 2006 10:45 am ] |
Post subject: | |
thnx for all your help i got it working now. |