469,926 Members | 2,533 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

reading a line through scanf

I want to read a line with white spaces though scanf.
So i used:
scanf("%['/n']",string);

above is working in one program, but in other..what may be the reason?

Nov 15 '05 #1
7 13038


gyan wrote:
I want to read a line with white spaces though scanf.
So i used:
scanf("%['/n']",string); ^
Should you not use '\n' instead?And should you not exclude the newline
when trying to read a newline? I feel [^\n] might just be more
appropriate. above is working in one program, but in other..what may be the reason?


Nov 15 '05 #2
gyan wrote:

I want to read a line with white spaces though scanf.
So i used:
scanf("%['/n']",string);

above is working in one program, but in other..what may be the reason?


#define LENGTH 20
#define str(x) # x
#define xstr(x) str(x)

int rc;
char array[LENGTH + 1];

rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
if (rc == 0) {
*array = '\0';
}

/* rc will be either 1, 0, or EOF */

--
pete
Nov 15 '05 #3


pete wrote:
gyan wrote:

I want to read a line with white spaces though scanf.
So i used:
scanf("%['/n']",string);

above is working in one program, but in other..what may be the reason?
#define LENGTH 20
#define str(x) # x
#define xstr(x) str(x)

int rc;
char array[LENGTH + 1];

rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);

^
Just a query, should we not write "[^\n]%*1[^\n]", instead? On my gcc
(4.0.0)
it keeps waiting if I don't specify the length. if (!feof(stdin)) {
getchar();
}
if (rc == 0) {
*array = '\0';
}

/* rc will be either 1, 0, or EOF */

Neat, really very neat!

Nov 15 '05 #4
Suman wrote:

pete wrote:
gyan wrote:

I want to read a line with white spaces though scanf.
So i used:
scanf("%['/n']",string);

above is working in one program,
but in other..what may be the reason?
#define LENGTH 20
#define str(x) # x
#define xstr(x) str(x)

int rc;
char array[LENGTH + 1];

rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);

^
Just a query, should we not write "[^\n]%*1[^\n]", instead?


No.
That's supposed to eat *all*
of the line characters which exceded LENGTH, if there are any,
up to but not including the newline.
On my gcc (4.0.0)
it keeps waiting if I don't specify the length.
if (!feof(stdin)) {
getchar();
}
if (rc == 0) {
*array = '\0';
}

/* rc will be either 1, 0, or EOF */

Neat, really very neat!


/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

#define LENGTH 30
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char string[LENGTH + 1];

fputs("Enter a string with spaces:", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", string);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
printf("Your string is:%s\n\n"
"Hit the Enter key to end,\nor enter "
"another string to continue:", string);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", string);
if (!feof(stdin)) {
getchar();
}
}
return 0;
}

/* END new.c */
--
pete
Nov 15 '05 #5
Suman wrote:
Neat, really very neat!


I especially like it for use with text files.
The only possible drawback, is that you get no feedback
on whether or not the lines are longer than LENGTH.

int nonblank_line(FILE *fd, char *line)
{
int rc;

do {
rc = fscanf(fd, "%" xstr(LENGTH) "[^\n]%*[^\n]", line);
if (!feof(fd)) {
getc(fd);
}
} while (rc == 0 || rc == 1 && blank(line));
return rc;
}

int blank(char *line)
{
while (isspace(*line)) {
++line;
}
return *line == '\0';
}

--
pete
Nov 15 '05 #6
On Thu, 30 Jun 2005 01:30:36 -0400, gyan wrote:
I want to read a line with white spaces though scanf.
So i used:
scanf("%['/n']",string);

above is working in one program, but in other..what may be the reason?

This scand for a matching sequence of ' / and n characters so I would be
very surprised if this "works" for what you want opn any system.

The appropriate function for reading a line is fgets(). While scanf() can
be made to do this it is not what it is designed for and is awkward. Once
you've read a line you can use all of C's string handing functions
including sscanf() to decode it.

Lawrence

Nov 15 '05 #7
gyan wrote:

I want to read a line with white spaces though scanf.
So i used:
scanf("%['/n']",string);

above is working in one program, but in other..what may be the
reason?


Why bother - scanf is not easy to handle. If you want a complete
line, get a complete line. fgets is one was. gets is not (never
use it). Another possibility is my ggets, which is also written in
portable standard c, has the simplicity of gets together with the
complete safety (although you do have to remember to free the line
storage when you are done with it). See:

<http://cbfalconer.home.att.net/download/ggets.zip>

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Andreas | last post: by
20 posts views Thread by plmanikandan | last post: by
8 posts views Thread by bahoo | last post: by
3 posts views Thread by xyz | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.