By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,913 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,913 IT Pros & Developers. It's quick & easy.

How to scan multiple lines using scanf when we dont know the end of input ?

P: 56
I referred this link ,but I am unable to get the logic behind using %[^\n]%*c or %[^\t] as format specifier .

And how to scan multiple lines of input when we don't know the end of input , say in those cases where we have an online compiler which would be giving its own input test cases which can be of any length and we are asked to perform some operation on that input so how will we determine how to scan the input since there we don't know the end of input ?
Aug 24 '16 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 2,418
Refer to the man page for fscanf().
%[^\n] and %[^\t] are examples of the negated scanset specifier where the scansets are \n (newline) and \t (tab) respectively.
%*c is an example of the character specifier with the asterisk sub-specifier.

A serious problem with your examples is the risk that the string filled by the negated scanset could overflow.
Aug 25 '16 #2

Expert Mod 5K+
P: 9,197
You will need to supply and end-of-data signal that you can use in the function with the scanf. My suggestion is to use something not in your data to begin with: \\EOD\\ or some such.

No one ever knows when all the data has been received.
Aug 25 '16 #3

Expert 100+
P: 2,418
You can compare the fscanf() return value to EOF.
You can call feof() and/or ferror() after each call to fscanf().
Aug 26 '16 #4

Expert Mod 5K+
P: 9,197
Testing EOF is a design decision. EOF does not mean all the data has been received. It just means the current input file has reached

Input may span several files.
Aug 27 '16 #5

Post your reply

Sign in to post your reply or Sign up for a free account.