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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • radha gogia
    New Member
    • Feb 2015
    • 56

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

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

    http://stackoverflow.c om/questions/14494309/reading-multiple-lines-of-input-with-scanf

    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 ?
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    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.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      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.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        You can compare the fscanf() return value to EOF.
        You can call feof() and/or ferror() after each call to fscanf().

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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
          end-of-file.

          Input may span several files.

          Comment

          Working...