scanf

This is a discussion on scanf within the C Programming forums, part of the General Programming Boards category; Hello, In what circumstance does scanf return a value of '-1'? Also, using arrays (and 'for loops'), how could I ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    79

    scanf

    Hello,

    In what circumstance does scanf return a value of '-1'?

    Also, using arrays (and 'for loops'), how could I 'count' the number of 'rows' of data that scanf scans in. (so that I can use it with printf later without printing the maximum number of 'rows' that I've set).

    Eg, if I've set my max number of rows to be 500 and I only scan in 100 rows of data, how can I print only the 100 rows of data? I know that I need to place a counter somewhere, but whenever I do this, the counter is always the maximum number of rows, which is what I've set, meaning that scanf is running for the full 500 instances (the max number), instead of just 100.

    Thanks
    Last edited by Mini; 04-17-2010 at 05:01 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It never specifically returns -1. It can return EOF, which might be -1.

    Control your loop with a counter. Keep track of when you stop looping.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Quote Originally Posted by quzah View Post
    Control your loop with a counter. Keep track of when you stop looping.
    I'm trying to do this and I'm doing something wrong Could you give an example of such a loop counter with scanf?

    Thanks

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Mini View Post
    I'm trying to do this and I'm doing something wrong Could you give an example of such a loop counter with scanf?
    It's easier if you post what you have tried, keeping it as short as possible.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    I've actually fixed that problem, but there another problem now... How can I get scanf to ignore 'headers' in input files?

    ie. If I have a set of data in a text file like

    Student No. Exam Score
    1234 90
    2344 85

    etc

    then how do I ignore the first line?

    Thanks

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Again, this depends specifically on how you are doing this, so you would need to post the code you are using.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Here's my code so far for the scanf bit

    Code:
       scanned=1;
       for (i=0; (i<MAX_ROW_SIZE)&&(scanned==1); i++) {
          for (j=0; j<NUM_DATA_TYPES; j++) {
              scanned=scanf("%lld", &data[i][j]);
          }
          k=k+1;
       }
    Cheers

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can use fgets to read a line of data. Read the line of data you don't care about into a variable you don't care about and ignore it.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Quote Originally Posted by tabstop View Post
    You can use fgets to read a line of data. Read the line of data you don't care about into a variable you don't care about and ignore it.
    Umm, I've never used that before. How would I implement it in this context?

    Also, If I'm not allowed to use it then what other method could I employ?

    Thanks

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, so you are feeding the files into stdin, not opening them with fopen() or something.

    If you know that is how the file is, you can do this before the loop:
    Code:
    char trash[256];
    fgets(trash,256,stdin);
    that will take care of the first line.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Quote Originally Posted by MK27 View Post
    Okay, so you are feeding the files into stdin, not opening them with fopen() or something.

    If you know that is how the file is, you can do this before the loop:
    Code:
    char trash[256];
    fgets(trash,256,stdin);
    that will take care of the first line.
    Thanks for that. I'm doing something like "./project < data.txt"

    Also, could I ask what the '256' in that variable means?

    Thanks again

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    256 is the length of the char array. fgets reads upto this value -1, since it then tacks '\0'. Presumably your first line is shorter than 256 characters.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 08:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 07:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 05:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21