Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7698039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:08:59+00:00 2026-05-31T22:08:59+00:00

#include <stdio.h> #include <stdlib.h> #define MAX 21 #define MAX_ELEM 8 #define SCORE 12 #define

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

#define MAX 21
#define MAX_ELEM 8
#define SCORE 12
#define NUM_SKATER 4
#define BASE 3.1

typedef struct{
                char  name[MAX];
                int   elements;
                float baseval[MAX_ELEM];
                int score[MAX_ELEM][SCORE];
                float total_base;
                float tech_score;
                float total_score;
              }SKATER;

int  getData(SKATER skater[NUM_SKATER]);



int main (void)
{
    // Global Declarations
    SKATER skater[NUM_SKATER];

    // Function calls
    getData(skater);
    return 0;
}

/********************************* getData ************************************
Pre:
Post:
*/
int getData(SKATER skater[NUM_SKATER])
{
    // LOcal Declarations
    FILE* fpIn;
    int   i = 0;
    int   k;
    int   j;
    char  buffer[256];

    // Statements
    if((fpIn = fopen("lab6data.txt","r"))==NULL)
    {
        printf("File opening error");
        system("PAUSE");
        exit(100);
    }

    while(i < NUM_SKATER && fgets(buffer, sizeof(buffer) - 1, fpIn))
    {
        sscanf(buffer,"%19[^0123456789]", &skater[i].name);
        for(k = 0; k < MAX_ELEM; k++)
        {puts(buffer);
            if(fgets(buffer, sizeof(buffer)-1, fpIn) != NULL)
            {
                sscanf(buffer,"%d %f", &skater[i].elements, &skater[i].baseval[k]);
                for(j = 0; j < SCORE; j++)
                {
                    sscanf(buffer,"%d", &skater[i].score[k][j]);

                }
            }

       }

        i++;
    }

    system("PAUSE");
    fclose(fpIn);

    return i;
}

I am having some trouble with reading the data from the file, so when I add a printf to the sscanf of the score and found out that it was actually print out the element numbers but not the score. I am confused about how this happen.

Can anyone suggest away to fix this or explain to me what happen to the score?

lets say that the sample data was
1 13.0 1 2 3 0 0 0 2 1 0 3

with the first number being the elements number, the second number is the base number and the rest is scores
now that i wanted to print out score it would print out
1 1 1 1 1 1 1 1 1 1
instead of
1 2 3 0 0 0 2 1 0 3

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:09:00+00:00Added an answer on May 31, 2026 at 10:09 pm

    The problem you have is that you are assuming that sscanf does some magic, namely that it remembers where it was in buffer after the last time it was called.

    sscanf does no such magic, it will being processing at the start of the string you give it, no matter how many times you’ve called it before with that same string.

    So the nested

    sscanf(buffer,"%d", &skater[i].score[k][j]);
    

    is always reading the very first int in buffer, i.e. 1 in your sample. There is no “overwriting” going on, you’re just reading the same thing over and over again.

    To make this work, you’ll need to keep track of where you should restart parsing yourself. This can be done by using the %n format specifier, and a “cursor” char * into your buffer for example.

    Here’s a sample of how you use that:

    #include<stdio.h>
    #include<stdlib.h>
    
    int main(int argc, char **argv)
    {
        int val;
        int pos;
        char *cur = argv[1];
        printf("Input: [%s]\n", cur);
        while (sscanf(cur, "%d%n", &val, &pos)==1) {
            printf("Read %d, rest is [%s]\n", val, cur+pos);
            cur += pos;
        }
        return 0;
    }
    

    Try running this code with different parameters:

    $ gcc -Wall t.c
    $ ./a.out "1"
    Input: [1]
    Read 1, rest is []
    $ ./a.out "1 2"
    Input: [1 2]
    Read 1, rest is [ 2]
    Read 2, rest is []
    $ ./a.out "1 a 2"
    Input: [1 a 2]
    Read 1, rest is [ a 2]
    $ ./a.out "1 2 3 54"
    Input: [1 2 3 54]
    Read 1, rest is [ 2 3 54]
    Read 2, rest is [ 3 54]
    Read 3, rest is [ 54]
    Read 54, rest is []
    $ ./a.out "1.0 2 3 54"
    Input: [1.0 2 3 54]
    Read 1, rest is [.0 2 3 54]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<iostream.h> #include<conio.h> #include<stdlib.h>(TOP) #include<fstream.h> #define MAX 5 int top = -1; int stack_arr[MAX];
#include<stdio.h> #include<stdlib.h> #define n ((sizeof(char)) * 100 ) int stringlength(char * str) { int
#include<stdio.h> #include<stdlib.h> #define MAX 1000 struct island{ double left; //gobal double right; } island[MAX];
#include <stdio.h> #include <stdlib.h> #define MAX 20 #define MAX_BASE 8 #define ROW 9 #define
#include<stdlib.h> #include<string.h> #include<stdio.h> int pstrcmp( char **p,char **q) { return strcmp(*p,*q) ; } int
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include stdio.h #include conio.h #include stdlib.h #define RANGE(i, min, max) (i<min) || (i>max) ?
#include <stdio.h> #include <stdlib.h> #define MAX 20 #define MAX_BASE 8 #define ROW 9 #define
#include <stdio.h> #include <stdlib.h> #define calc(a,b) (a*b)/(a-b) void calculate(){ int a = 20, b

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.