473,386 Members | 1,785 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,386 software developers and data experts.

Control strings scanf()

#include<stdio.h>
main()
{
char line[80];
scanf("%[^\n]", line);
printf("%s", line);
}

it will read and print the line but what is "%[^\n]" in general we
gives %s, %c .
i searched about it but i still its not clear .

please clear me "%[^\n]" how it is working and geting line with space
till end .
Jun 27 '08 #1
3 9803
Tinku wrote:
#include<stdio.h>
main()
{
char line[80];
scanf("%[^\n]", line);
printf("%s", line);
}

it will read and print the line but what is "%[^\n]" in general we
gives %s, %c .
i searched about it but i still its not clear .

please clear me "%[^\n]" how it is working and geting line with space
till end .
"%[^\n]" means to:
scan
every byte from stdin,
up to but not including the newline character,
and to write a corresponding string in line[].
There is still left a newline character in the input stream,
which you can eat with a call to getc().

"%*[^\n]" means to:
reject
every byte from stdin,
up to but not including the newline character,
and to write a corresponding string in line[].
There is still left a newline character in the input stream,
which you can eat with a call to getc().

/* BEGIN fscanf_input.c */
/*
** There are only three different values
** that can be assigned to the int variable rc,
** from the fscanf calls in this program;
** They are:
** EOF
** 0
** 1
** If rc equals EOF, then the end of file was reached,
** or there is some other input problem;
** ferror and feof can be used to distinguish which.
** If rc equals 0, then an empty line
** (a line consisting only of one newline character)
** was entered, and the array contains garbage values.
** If rc equals 1, then there is a string in the array.
** Up to LENGTH number of characters are read
** from a line of a text stream
** and written to a string in an array.
** The newline character in the text line is replaced
** by a null character in the array.
** If the line is longer than LENGTH,
** then the extra characters are discarded.
*/
#include <stdio.h>

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

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

puts("The LENGTH macro is " xstr(LENGTH) ".");
do {
fputs("Enter any line of text to continue,\n"
"or just hit the Enter key to quit:", stdout);
fflush(stdout);
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
if (rc == 0) {
array[0] = '\0';
}
if (rc == EOF) {
puts("rc equals EOF");
} else {
printf("rc is %d. Your string is:%s\n\n", rc, array);
}
} while (rc == 1);
return 0;
}

/* END fscanf_input.c */
--
pete
Jun 27 '08 #2
pete wrote:
Tinku wrote:
>#include<stdio.h>
main()
{
char line[80];
scanf("%[^\n]", line);
printf("%s", line);
}

it will read and print the line but what is "%[^\n]" in general we
gives %s, %c .
i searched about it but i still its not clear .

please clear me "%[^\n]" how it is working and geting line with space
till end .

"%[^\n]" means to:
scan
every byte from stdin,
up to but not including the newline character,
and to write a corresponding string in line[].
There is still left a newline character in the input stream,
which you can eat with a call to getc().

"%*[^\n]" means to:
reject
every byte from stdin,
up to but not including the newline character,

and to write a corresponding string in line[].
That doesn't make any sense.
I used copy and paste too much.
There is no corresponding string to write.
There is still left a newline character in the input stream,
which you can eat with a call to getc().

--
pete
Jun 27 '08 #3
On May 14, 8:27 am, Tinku <sumit15...@gmail.comwrote:
#include<stdio.h>
main()
{
char line[80];
scanf("%[^\n]", line);
printf("%s", line);

}
Some examples:
scanf("%[0-9]",line); matches any number of digits (and stores into
line)
(On many systems, this is implementation
defined)
scanf("%[abc]",line); matches any number of consecutive 'a' or 'b'
or 'c'
characters, and they are stored into line.
scanf("%s",line); skips(eats) initial white spaces, all
characters
until an other white space comes (word goes
into line)
scanf(" %[abc}",line); skips initial white spaces, then like "%[abc]"
scanf(" %[abc] ",line); skips initial and trailing white spaces...
scanf("%[^abc]",line); reads a sequence of characters into line until
an 'a', 'b' or 'c' is found, which is left in
the stream
scanf("%[\n]",line); consumes blank lines and put the corresponding
number
of '\n' characters into line.
scanf("%[^\n]",line); puts all characters into line until a newline
comes,
this '\n' character is kept in the stream

All these were quite dangerous, as a crappy input might put you in the
dark
realm of undefined behaviour.

scanf("%*[\n]"); eats and ignores all blank lines until
something
else comes
scanf("%80[^\n]",line); same as "%[^\n]", but will not put more than
79
characters into line. You can put line[79] to
zero (prior to scanf) and check it afterwards
to
see if the input was truncated before a
newline.
Alternatively:
scanf("%80[^\n]%[\n]",line,other_char_array)
will return 2 if ok (eating the newline)
or 1, if the line was too long.
Alternatively:
fgets(line,sizeof(line),stdin) but that will eat the newline, too

Also when using %s or %c, use the length modifier, to set the number
of bytes
in the character array that receives the data. %80s will stop at a
whitespace,
and put a '\0' character to the end, %80c will do neither.

Szabolcs

ps: Did you really search for this information? I might have plainly
refer you
to 7.19.6.2 in http://www.open-std.org/jtc1/sc22/wg...docs/n1124.pdf
or simply search for man scanf (which will tell more about the
implementation
defined behaviour on an implementation of not yours.)
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
7
by: happy | last post by:
I need to write a string field in a structure data type to a file using fprintf,fscanf. I failed to use the below syntax. Please I need to not go outside scanf,fprintf,printf . /* Book name ...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
25
by: sravishnu | last post by:
Hello, I have written a program to concatanae two strings, and should be returned to the main program. Iam enclosing the code, please give me ur critics. Thanks, main() { char s1,s2;...
7
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
2
by: karafire2003 | last post by:
I've been tasked to do 2 questions. I think i got the majority of it done, but i'm having trouble. Question #1: Write a C program that accepts as input from the keyboard a floating point number, an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.