473,385 Members | 2,243 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,385 software developers and data experts.

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 13234


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andreas | last post by:
Hi, I'm just developing a .net (compact) application and therefore make use of the StreamReader class - and especially the ReadLine function. Now, when I try to read lines from a text file that...
20
by: plmanikandan | last post by:
Hi, I need to read a file line by line.each line contains different number of characters.I opened file using fopen function.is there any function to read the file line by line Regards, Mani
5
by: loveme | last post by:
i am reading a file line by line...But in some case some exception comes,i catch exception but program not exceute next line.Van u please tell how skip the exception comes on line and exceute next...
8
by: bahoo | last post by:
Hi, I have a text file containing a single line of text, such as 0024 How should I read it into a "list"? I tried this, but the "join" did not work as expected. Any suggestions?
3
by: xyz | last post by:
Hi, I have a text file around 7GB includes 100 million lines... I want to read the data line by line when I approach my module.. ie., when i read for the first time , my program shuld read only...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.