473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

spaces and strings

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
5 6345
horace1
1,510 Expert 1GB
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
654321
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
horace1
1,510 Expert 1GB
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
654321
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
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

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

Similar topics

13
by: p s | last post by:
hi all i have a vb6 project, one of the functions is to read in a text file and place it into an array problem is, when people use the TAB key in the original file that is read, i just get the...
9
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test ...
5
by: qwweeeit | last post by:
Hi all, I need to limit as much as possible the lenght of a source line, stripping white spaces (except indentation). For example: .. . max_move and AC_RowStack.acceptsCards ( self,...
4
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely....
7
by: Eric Johannsen | last post by:
Hi, My C# code is calling VB6 code, which expects all (fixed-length) strings to be padded with spaces. The strings are contained with a struct, something like this (attributes to simulate...
2
by: BobM | last post by:
I have two strings that I would like to compare. To guarantee the compare is not affected by trailing spaces, I would like to remove them. Will this do: MyTextBox.text.ToString().Trim()?
17
by: tommy | last post by:
Hi all, I' m adding strings to some fields in my table via Access. The strings sometimes have trailing spaces and I really need to have it that way, but Access truncates trailing spaces. How can...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
40
by: raphfrk | last post by:
I have a program which reads in 3 filenames from the command line prog filename1 filename2 filename3 However, it doesn't work when one of the filenames has spaces in it (due to a directory...
0
by: MLH | last post by:
In an earlier post today, it was mentioned that dbl-spaces caused alignment-justification problems when printing labels. I've never noticed this in reports except for those meant for the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.