Thread: Help scanning in numbers and printing format

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    37

    Help scanning in numbers and printing format

    I have an assignment where I have to read in numbers from a file using redirect and then break them up into groups, find the highest number, and print them in a certain way.
    I'm currently trying to figure this part out and will probably have questions about finding and printing the highest number.


    The file the numbers are stored in a file and is formatted like
    Code:
    3     9 8 7    6 5 4    3 2 1
    So far this is what I have to print out the Group number and the numbers. However this will only print on the first line with "Group1".

    Code:
     for(i = 1; i <= group; i++) {
       printf("Group %d: ", i);
            while(count < 3) {
              scanf("%d", &num);
              printf("%d ", num);
              count++;
            } //while
    Output should look something like this(it should all be right aligned):
    Code:
             Number1 Number2 Number3
    Group1         9       8       7
             Number1 Number2 Number3
    Group2         6       5       4
             Number1 Number2 Number3
    Group3         3       2       1

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need fscanf instead of scanf if you're trying to read from a file.
    Code:
    for each line in the file
        print row header
        for each column in the row
            read number from file
            print number
        print newline
    That's pretty much what you want. But, scanf takes input from the standard input device (keyboard typically), not a file.


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

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    37
    This is the output I'm getting.

    Group1: 9 8 7
    Group2:
    Group3:

    I can't seem to print anything on the last two lines.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing random decimal numbers in a text file
    By warrick in forum C Programming
    Replies: 3
    Last Post: 03-08-2010, 09:32 AM
  2. Printing Armstrong Numbers from 1-500
    By duffmckagan in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:26 AM
  3. format numbers output?
    By DanC in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2006, 06:54 AM
  4. Printing numbers in hex format
    By miclus in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2005, 07:04 AM
  5. printing only prime numbers
    By Micko in forum C Programming
    Replies: 30
    Last Post: 06-10-2004, 10:23 PM