How to Read a String With Whitespaces in C

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Word Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more than! To participate y'all demand to annals. Registration is gratuitous. Click here to register now.

  • Forums
  • Digital Design and Embedded Programming
  • PC Programming and Interfacing
Y'all are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or employ an alternative browser.

Reading a string with white space in C

  • Thread starter Old Nick
  • Get-go date
Status
Non open for further replies.
  • #1
scanf read cord with whitespace

Hullo

I'k writing some data aquisition lawmaking in which the binder and files that the information are stored in are user definable without changing the code. Now I've managed to practice this, but I'm having a problem with 'white space' (space bar especially) in the user inputed names. I'm using scanf() to read in a user inputed string which treats white space in a manner that causes problems with my program. If it was only me that was goint to use the lawmaking then information technology wouldn't be a problem.
Anyway

pprintf("enter directory ->");
scanf("%s", &input2);
sprintf(dirname,"c://%southward",input2);
mkdir(dirname);
printf("enter filename base ->");
scanf("%s", &input);

these are some of the pertinent parts of the code. Anywhite space in the names causes code malfunction, Is at that place some other fashion of reading in a cord, or is there a way of ignoring/blocking white-space entry?

Cheers,

Nick

  • #2
Joined
Mar 23, 2006
Messages
11
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,281
Activity points
1,342
scanf c white infinite bar

Hello Nick,

Instead of using scanf to read input, try:

fgets(buffer, buffer_length, stdin);

This will read a line from the standard input until either a /n character is read, or buffer_length number of characters have been read. The /n character volition be copied into the buffer, which you may need to strip out.

Also, when calling the functions to admission directories or files with spaces in them, y'all may need to enclose the string in quotes to ensure it is candy correctly by the Os.

Hope this helps,
Jay

  • #3
Joined
Sep fourteen, 2007
Letters
77
Helped
xv
Reputation
xxx
Reaction score
5
Bays points
1,288
Activeness points
1,829
reading whitespace in c

The only problem existence SCANF...scanf takes input well but when space bar is striking, scanf terminates taking input...the alternative for scanf is GETS...using gets you can have any input and it only cease when enter is hit...
so uncomplicated...supplant your scanf with gets

Best Regards

  • #iv
Joined
Mar 23, 2006
Messages
eleven
Helped
three
Reputation
6
Reaction score
0
Bays points
1,281
Activeness points
1,342
cord with white space reading

gets() will work, but is dangerous because is does not perform bounds-checking. A user can type a string longer than your buffer and overflow it. fgets() is much safer.

  • #five
Joined
Sep fourteen, 2007
Messages
77
Helped
fifteen
Reputation
30
Reaction score
5
Bays points
i,288
Activeness points
1,829
read input with whitespace c

well fgets brings input from a file...
simply according to Nick the input is desired from the user/keyboard.

All-time Regards

  • #6
how to read a string with white spaces in c

I don't call back I tin apply that to capture input from the keyboard. fgets() reads from a file right? like to fscanf etc. or am I wrong?

The problem I take is that the people that use the program will be making directory and file names of their choosing, all different lengths etc. I could requite them instructions to not use spaces etc. but that looks a impact unprofessional. I used to be fairly good at C during my undergrad and masters years, but my memories have faded somewhat.

There is surely a way to read a space into a character string from a keyboard input.

  • #7
Joined
Sep 14, 2007
Messages
77
Helped
15
Reputation
30
Reaction score
5
Trophy points
1,288
Activeness points
1,829
reading string with white spaces c++ from stdin

Correct Nick.
exactly what i mentioned.

Best Regards

  • #viii
cord with spaces in c -c++ -c#

Is there some way I could encase the scanf() or whatever in a wrapper of some sort to detect if a space-bar press has occured, then I can instruct the user to input a valid proper noun.

I'yard certain I remember doing this sort of thin years ago.

Cheers

  • #9
Joined
Mar 23, 2006
Messages
11
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,281
Activity points
1,342
c - scanf does not capture whitespace

The 3rd argument to fgets() specifies where to read the input -- tin can exist a file or, as I wrote above, stdin (reading panel input from the keyboard).

Run into https://en.wikipedia.org/wiki/Fgets

  • #ten
c++ cord read white spaces?

I know that gets() should never exist used, always.

  • #11
Joined
Sep 14, 2007
Messages
77
Helped
15
Reputation
30
Reaction score
v
Trophy points
ane,288
Activity points
1,829
read a cord containing whitespace c from stdin

well past but useing scanf y'all tin have a string input with spaces but a chip lengthy and unprofessional look...
information technology will go like
char stringa, stringb,space;
space = ' ';
scanf("%s%s",stringa,stringb);
stradd(stringa,space);
stradd(stringa,stringb);

the terminal line will bring string a and b in a line.

*the above lawmaking will only enable to take a string input with ane space...
for multiple you'll need to make it more circuitous.
i would still recomment gets.

Best Regards

  • #12
c++ read string with white spaces from keyboard

I've managed to become the fget() to work - sort of- bit as was mentioned there is a carriage render at the end of the cord which is causing some problems. Is reading the cord into a new string 1 char shorter the best way to strip this off, or is in that location a more elegant way?

Cheers,

Nick

  • #13
Joined
Sep fourteen, 2007
Messages
77
Helped
15
Reputation
thirty
Reaction score
5
Trophy points
ane,288
Activity points
ane,829
getting a string with whitespace from keyboard c

in programming what i believe elegant = logic + ease
if you don't have to show you're code to some one and you're just after getting the task done, then use what always suits you all-time...

if y'all have to show the code to some one then the all-time code is shortest lawmaking.

Best regards

  • #14
reading in white space with c

They'll unfortunately require the source code.

  • #15
Joined
Sep 14, 2007
Messages
77
Helped
15
Reputation
xxx
Reaction score
5
Trophy points
1,288
Activity points
ane,829
aahhh...then you meliorate apply the shortest code but make certain its simple for ya to work with and implement what you desire.

Best Regards

  • #16
Joined
Mar 23, 2006
Messages
11
Helped
three
Reputation
six
Reaction score
0
Bays points
1,281
Activity points
1,342
Hi Nick,

You could try this:

                                int len = strlen(string); if (string[len-1]=='\n')     string[len-1]='\0'                              

That should replace a abaft space in 'string' with a null-terminator.[/code]
  • #17
cheers guys,

I'm going to have to write a loop to search through the cord for the a '/due north' since I'm not going to know the length of the string they'll exist inputing.

Thanks for your assistance.

Nick

  • #18
Howdy,

it has been said, that gets() could cause string overflow, that's true, but non in a different or more dangerous manner than fgets() or scanf() without length parameter, equally used in the original code. And so why gets() should never exist used but the other functions may?

Regards,
Frank

  • #19
Joined
Mar 23, 2006
Messages
11
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,281
Activity points
1,342
fgets() and scanf() tin both limit the length of the string being read:

fgets has a buffer length argument and scanf() tin can specify length in the format string i.e. "%10s". gets() provides no such length checking.

Why utilize dangerous functions when safe alternatives exist?

Regards,
Jay

  • #20

echo47

Advanced Member level v
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
ane,274
Reaction score
89
Bays points
1,328
Location
USA
Activity points
33,176
Status
Not open for further replies.

Similar threads

  • Forums
  • Digital Design and Embedded Programming
  • PC Programming and Interfacing

campbellvely1989.blogspot.com

Source: https://www.edaboard.com/threads/reading-a-string-with-white-space-in-c.118245/

0 Response to "How to Read a String With Whitespaces in C"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel