By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

spaces and strings

P: 3
Hello,

In my program, i have to scanf numbers and words in some file. I can't seem to get the words in 1 string though because it always stops at a white space. How can make the program ignore the white space?
Nov 7 '06 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,510
if the scanf() conversion specification contains a space it will skip white space when reading strings, e.g.
Expand|Select|Wrap|Line Numbers
  1. char word[20];
  2. scanf(" %s",word);   
  3.  
The scanf() would skip leading white space, the read characters into word[] until the next white space is found.
Nov 7 '06 #2

P: 3
yes, but how can i skip the next white space when I do not know how many white spaces there are between the words in total?
Nov 7 '06 #3

Expert 100+
P: 1,510
yes, but how can i skip the next white space when I do not know how many white spaces there are between the words in total?
if you have the statement
scanf(" %s",word);
in a loop it will automatically skip the white space between sucessive words
Nov 7 '06 #4

P: 3
Hrm. Well, what if i had a file instead. And there are numbers and setences. Once i get a number or a setence, i need to put a number into some array and the whole sentence in another array. Then put another number, and another whole sentence, and another, and so on. I'm pretty much stuck on how to put that whole sentence in another array. I tried to use
Expand|Select|Wrap|Line Numbers
  1.  
  2. input_status = fscanf("%d %s%c",&time,song,&ch);
  3.     while (ch==' ')
  4.     {
  5.         fscanf("%s%c",song2,&ch);
  6.         strcat(song,song2);
  7.     }
  8.  
  9.  
but it says my arguments are not the right type. Plus, the whole song title wouldn't look right because after many strcats, there are no spaces between the words.

I also tried to use getline to get a line at a time from the file. But i'd get the same problem, (besides the fact my terminal exploded because of buffer issues i guess), i'd have a number and words in 1 string and i'd need to seperate the number and put it in an array and put the whole sentence in some other array.
Nov 7 '06 #5

P: 4
yes, but how can i skip the next white space when I do not know how many white spaces there are between the words in total?
To get the whole sentence and ignore the white space between words I think you need something like this: (that is assuming each sentence is on a different line):

fscanf("%d %[^\n]", &time, song);

I think this extract from MDSN might help you

Reading Undelimited strings
To read strings not delimited by whitespace characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The set of characters in brackets is referred to as a control string. The corresponding input field is read up to the first character that does not appear in the control string. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.

Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.

Reading Unterminated strings
To store a string without storing a terminating null character ('\0'), use the specification %nc where n is a decimal integer. In this case, the c type character indicates that the argument is a pointer to a character array. The next n characters are read from the input stream into
Oct 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.