Beginner and scanf

This is a discussion on Beginner and scanf within the C Programming forums, part of the General Programming Boards category; I'm trying to learn C programming in Linux, and I can get to the point where I can redirect the ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Beginner and scanf

    I'm trying to learn C programming in Linux, and I can get to the point where I can redirect the output of a file containing 4 numbers (1 2 3 4) that are space delimited to the input of a program that finds the highest number (4). Then when I try to comma, colon, or semi colon Delimite the same program i end up with the number 134513856 when my numbers are 1,2,3,4.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    CLI programs separate parameters (arc and argv) based on spaces... not commas.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    CLI programs separate parameters (arc and argv) based on spaces... not commas.
    That doesn't really have anything to do with scanf though. The f in scanf stands for "formatted". You have to have specifically formatted input for scanf to do what you want. More than likely, you haven't told it to ignore the fact that you've inserted , into your stream. What normally would have worked (white space) to tell it to stop scanning, isn't there anymore.


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

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    If you don't want to handle separate command line parameters, you could accept the string "1,2,3,4" as a single command line parameter and split it up on commas to get the individual numbers (strtok is one way).

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Linux Programming

    I have a test coming up that involves me doing the following:

    1.) Create a program that reads four numbers, space seperated, from a data file printing only the smallest.
    2.) Create a program that reads four numbers, comma seperated, from a data file printing only the largest.
    3.) Create a program that reads four numbers, colon seperated, from a data file printing only the odd numbers.
    4.) Create a program that reads four numbers, simicolon seperated, from a data file printing only the even numbers.

    That's what I'm going for, so am i reading the question wrong?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So it sounds like the only thing you need to be doing then, is telling it to expect a different character in your input stream. See my post above.


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

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    I'm wondering what I'm missing to allow the file being read into the program via comma's, colon's, and semicolons so that I can fulfill the requirements for the upcoming test.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    I think so, yes. You're probably expected to use fopen, fread/fscanf and fclose. In your current method, your program doesn't actually read the data from a file.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    Here's my teachers example program for us to study and readin file for his progam:

    Readin: Filename = mydata
    16,23,78

    Program: p1.o

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int i_a,i_b,i_c,i_sm;

    scanf("%i,%i,%i",&i_a,&i_b,&i_c);

    i_sm=i_a;

    if(i_b < i_sm)
    i_sm=i_b;

    if(i_c < i_sm)
    i_sm=i_c;

    printf("The smallest number is %i\n",i_sm);

    return(0);
    }

    My readin: filename = mydata
    1,2,3,4

    My program: p1.o
    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int i_1,i_2,i_3,i_4,i_sm;

    scanf("%i\n %i\n %i\n %i\n",&i_1,&i_2,&i_3,&i_4);

    i_sm=i_1;

    if(i_sm < i_2)
    i_sm=i_2;

    if (i_sm < i_3)
    i_sm=i_3;

    if (i_sm < i_4)
    i_sm=i_4;

    printf("The largest number is %i\n",i_sm);

    return (0);
    }



    Am I missing something completely obvious?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Chleborad View Post
    Am I missing something completely obvious?
    Yes. As I have said before, scanf takes precisely formatted input. In this case:
    Code:
    scanf("%i,%i,%i",&i_a,&i_b,&i_c);
    This means "expect a NUMBER,NUMBER,NUMBER". Exactly. You are telling it that you will enter exactly one number, then, without spaces, a comma, and another number, and again another comma, and finally another number. Exactly. No deviation. scanf requires precise usage.


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

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Chleborad View Post
    I have a test coming up that involves me doing the following:

    1.) Create a program that reads four numbers, space seperated, from a data file printing only the smallest.
    2.) Create a program that reads four numbers, comma seperated, from a data file printing only the largest.
    3.) Create a program that reads four numbers, colon seperated, from a data file printing only the odd numbers.
    4.) Create a program that reads four numbers, simicolon seperated, from a data file printing only the even numbers.

    That's what I'm going for, so am i reading the question wrong?
    Here, play with this and tell me if it gives you any ideas...
    Code:
    int _cdecl main(int argc, char *argv[])
    { int a,b,c;
      printf("3 coma separated numbers please :  ");
      if( scanf("%d , %d , %d",&a,&b,&c) < 3)
        puts("Input Error");
      printf("%d  %d  %d\n",a,b,c);
      return 0; }

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    OH!! Perfect thank you quzah!! That makes perfect sense, sorry for not getting it right away.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > Am I missing something completely obvious?
    Yes, you're missing code tags.
    See << !! Posting Code? Read this First !! >>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Scanf question for beginner
    By somekid413 in forum C Programming
    Replies: 1
    Last Post: 12-15-2009, 03:56 PM
  2. Problem using scanf with array
    By xcubic33 in forum C Programming
    Replies: 12
    Last Post: 12-04-2009, 09:00 AM
  3. Beginner Question: Problem with scanf and strings. Help!
    By lucidrave in forum C Programming
    Replies: 8
    Last Post: 08-11-2009, 10:22 PM
  4. Character literals in scanf
    By DL1 in forum C Programming
    Replies: 11
    Last Post: 08-09-2008, 02:36 PM
  5. Replies: 9
    Last Post: 11-27-2001, 08:25 AM

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