fscanf help

This is a discussion on fscanf help within the C Programming forums, part of the General Programming Boards category; for my intro to c programming class, our homework requires us to read a data file and emit various bits ...

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    4

    Unhappy fscanf help

    for my intro to c programming class, our homework requires us to read a data file and emit various bits of information. the data file looks like this

    #Domain: the URL
    #Alexa: some kind ranking, small numbers are better
    #Rank: another sort of ranking, big numbers are better

    #Domain Alexa Rank Site Name
    #------------ ----- ------- ---------
    amazon.com 13 1177136 Amazon.com
    google.com 1 4533883 Google
    youtube.com 3 3637788 YouTube
    blogspot.com 16 30878 Blogspot
    hao123.com 14 33978 Hao123
    qq.com 7 472087 Tencent QQ
    wordpress.com 19 2031545 WordPress.com
    vk.com 25 129655 VKontakte
    yahoo.com 4 1888093 Yahoo!
    twitter.com 12 6183107 Twitter
    yandex.ru 24 320854 Yandex
    weibo.com 17 133824 Sina Weibo
    bing.com 21 162052 Bing
    yahoo.co.jp 20 153463 Yahoo! Japan
    360.cn 23 39486 360 Safeguard
    google.co.in 15 47760 Google India
    tmall.com 22 32156 Tmall
    facebook.com 2 8190877 Facebook
    taobao.com 9 185399 Taobao
    wikipedia.org 6 2154423 Wikipedia
    linkedin.com 10 1454069 LinkedIn
    sina.com.cn 11 206014 Sina Corp
    baidu.com 5 325710 Baidu
    163.com 18 158645 NetEase
    live.com 8 149315 Windows Live

    with and expected output of

    Lines: 25
    Best Rank: 8190877 facebook.com
    Best Alexa: 1 google.com
    .com: 19
    .net: 0
    .org: 1
    Other: 5


    I've started and am able to get the correct number of lines, and the alexa number, but I keep getting some errors and am just unsure where to correct my code.

    this is what it looks like;

    Code:
     
    Code:
    // Lexi Anderson
    //CS 156
    #include <stdio.h>
    
    
    int main()
    {
            int c;
            int size[80];
            int line=0;
            int last = '\n';
            char domain;
            int alexa;
            int rank;
            char name;
            int min=0;
    
    
            FILE *first = fopen("web-data", "r");
    
    
            if (first == NULL)
            {       
                    printf("sorry, I couldn't open web-data\n");
                    return 0;
            }
    
    
            if (first != NULL)
            {
                    if(size[0] != '\0')
                    {
                            --line;
                    }
                    while ((c=fgetc(first)) != EOF)
                    {
                            if(c=='#')
                            {
                                    --line;
                            }
                            else if(c==last)
                            {
                                    ++line;
                            }
                    }
                    printf("Lines: %d\n", line);
            }
    
    
            fscanf(first, "%s %d %d %s", &domain, &alexa, &rank, &name);
            if(rank>min)
                    min = rank;
                    printf("Best Rank: %d %s\n", rank, domain);
    
    
            fscanf(first, "%s %d %d %s", &domain, &alexa, &rank, &name);
            if(alexa=1)
                    printf("Best Alexa: %d %s\n", alexa, domain);
    
    
    fclose(first);
    return 0;
    }


    and once compiled, I get the errors,

    www.c: In function ‚main‚:
    www.c:46:3: warning: format ‚%s‚ expects argument of type ‚char *‚, but argument 3 has type ‚int‚ [-Wformat=]
    printf("Best Rank: %d %s\n", rank, domain);
    ^
    www.c:49:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    if(alexa=1)
    ^
    www.c:50:3: warning: format ‚%s‚ expects argument of type ‚char *‚, but argument 3 has type ‚int‚ [-Wformat=]
    printf("Best Alexa: %d %s\n", alexa, domain);
    ^



    any suggestions and explanations would be greatly appreciated!
    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,681
    An C String is an char array with an trailing ASCII nul char (or zero byte).
    The format code of "%s" means a C String.

    This "=" is a assignment operator; while "==" is a comparison operator ofter used in if statements.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    Tim,

    when I changed the if(alexa == 1)
    then my output line Best Alexa: 1 (null)
    doesn't show at all

    Lexi

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,681
    Did you stop trying to put multiple characters into a single char?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    I honestly have no idea to do that.
    I found something that mentions you must declare char *name;

    I am very bad at this, sorry

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,681
    Note, I choose 255 because it the default length I use when I have no idea of a proper size to use.

    I used the +1 to hold the ASCII nul/zero byte.

    Tim S.

    Code:
    #define DOMAIN_LENGTH 255
    #define NAME_LENGTH 255
    
    int main()
    {
            int c;
            int size[80];
            int line=0;
            int last = '\n';
            char domain[DOMAIN_LENGTH + 1];
            int alexa;
            int rank;
            char name[NAME_LENGTH + 1];
            int min=0;
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    okay, right that makes sense. Our professor told us the max length they would be using is 80, so used that in place of 255. It now tells me

    sacramento:~$ c99 -Wall www.c
    www.c: In function ‚main‚:
    www.c:54:2: warning: format ‚%s‚ expects argument of type ‚char *‚, but argument 3 has type ‚char (*)[1]‚ [-Wformat=]
    fscanf(first, "%s %d %d %s", &domain, &alexa, &rank, &name);
    ^
    www.c:54:2: warning: format ‚%s‚ expects argument of type ‚char *‚, but argument 6 has type ‚char (*)[1]‚ [-Wformat=]
    www.c:59:2: warning: format ‚%s‚ expects argument of type ‚char *‚, but argument 3 has type ‚char (*)[1]‚ [-Wformat=]
    fscanf(first, "%s %d %d %s", &domain, &alexa, &rank, &name);
    ^
    www.c:59:2: warning: format ‚%s‚ expects argument of type ‚char *‚, but argument 6 has type ‚char (*)[1]‚ [-Wformat=]

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,681
    Remove the "&" from in front of the array names. Array are already an address like value therefor "&" is NOT needed in scanf type functions.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with fscanf
    By dford425 in forum C Programming
    Replies: 8
    Last Post: 01-16-2011, 05:44 PM
  2. fscanf
    By darren78 in forum C Programming
    Replies: 8
    Last Post: 02-12-2009, 12:00 PM
  3. fscanf
    By ktran03 in forum C Programming
    Replies: 14
    Last Post: 01-19-2009, 10:59 PM
  4. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 10:44 PM
  5. fscanf and EOF
    By .ZG. in forum C Programming
    Replies: 3
    Last Post: 05-30-2004, 07:49 AM

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