Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion 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! To participate you need to register. Registration is free. Click here to register now.

Reading a string with white space in C

Status
Not open for further replies.

Old Nick

Advanced Member level 1
Advanced Member level 1
Joined
Sep 14, 2007
Messages
479
Helped
68
Reputation
136
Reaction score
18
Trophy points
1,298
Activity points
4,243
scanf read string with whitespace

Hi

I'm writing some data aquisition code in which the folder and files that the data are stored in are user definable without changing the code. Now I've managed to do 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 just me that was goint to use the code then it wouldn't be a problem.
Anyway

pprintf("enter directory ->");
scanf("%s", &input2);
sprintf(dirname,"c://%s",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 there another way of reading in a string, or is there a way of ignoring/blocking white-space entry?

Cheers,

Nick
 

scanf c white space bar

Hi 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 will be copied into the buffer, which you may need to strip out.

Also, when calling the functions to access directories or files with spaces in them, you may need to enclose the string in quotes to ensure it is processed correctly by the OS.

Hope this helps,
Jay
 

reading whitespace in c

The only problem being SCANF...scanf takes input well but when space bar is hit, scanf terminates taking input...the alternative for scanf is GETS...using gets you can take any input and it only terminate when enter is hit...
so simple...replace your scanf with gets

Best Regards
 

string 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.
 

read input with whitespace c

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


Best Regards
 

how to read a string with white spaces in c

I don't think I can use that to capture input from the keyboard. fgets() reads from a file right? similar to fscanf etc. or am I wrong?

The problem I have is that the people that use the program will be making directory and file names of their choosing, all different lengths etc. I could give them instructions to not use spaces etc. but that looks a touch 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.
 

reading string with white spaces c++ from stdin

Correct Nick.
exactly what i mentioned.

Best Regards
 

string 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, so I can instruct the user to input a valid name.

I'm sure I remember doing this sort of thin years ago.

Cheers
 

c - scanf does not capture whitespace

The third argument to fgets() specifies where to read the input -- can be a file or, as I wrote above, stdin (reading console input from the keyboard).

See https://en.wikipedia.org/wiki/Fgets
 

    Old Nick

    Points: 2
    Helpful Answer Positive Rating
c++ string read white spaces?

I know that gets() should never be used, ever.
 

read a string containing whitespace c from stdin

well by only useing scanf you can take a string input with spaces but a bit lengthy and unprofessional look...
it will go like
char stringa, stringb,space;
space = ' ';
scanf("%s%s",stringa,stringb);
stradd(stringa,space);
stradd(stringa,stringb);


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

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

Best Regards
 

    Old Nick

    Points: 2
    Helpful Answer Positive Rating
c++ read string with white spaces from keyboard

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

Cheers,

Nick
 

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 ever suits you best...

if you have to show the code to some one then the best code is shortest code.


Best regards
 

reading in white space with c

They'll unfortunately require the source code.
 

aahhh...then you better use the shortest code but make sure its simple for ya to work with and implement what you want.

Best Regards
 

Hi Nick,

You could try this:

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

That should replace a trailing space in 'string' with a null-terminator.[/code]
 

    Old Nick

    Points: 2
    Helpful Answer Positive Rating
cheers guys,

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

Cheers for your help.

Nick
 

Hello,

it has been said, that gets() could cause string overflow, that's true, but not in a different or more dangerous way than fgets() or scanf() without length parameter, as used in the original code. So why gets() should never be used but the other functions may?

Regards,
Frank
 

fgets() and scanf() can both limit the length of the string being read:

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

Why use unsafe functions when safe alternatives exist?

Regards,
Jay
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top