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 6867219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:16:55+00:00 2026-05-27T03:16:55+00:00

I have a file with data in this format: Name WeekDay Month day, Year

  • 0

I have a file with data in this format:

Name WeekDay Month day, Year StartHour:StartMin Distance Hour:Min:Sec

Example:
John Mon September 5, 2011 09:18 5830 0:26:37

I want to scan this into a struct:

typedef struct {
    char name[20];
    char week_day[3];
    char month[10];
    int day;
    int year;
    int startHour; 
    int startMin;
    int distance;
    int hour;
    int min;
    int sec;
} List;

i use fscanf():

List listarray[100];
for(int i = 0; ch = fgetc(file) != 'EOF'; ch = fgetc(file), i++){
    if(ch != '\0'){
        fscanf(file, "%s %s %s %d %d %d %d %d %d %d %d", &listarray[i].name...etc)
    }
}

My issue is that I want to filter out the noise in the input string, that being:

Month day*,* year <- the comma is consistent in all entries. I just want the month in the char array, the day in int.

And the time stamps:

startHour:startmin and hour:min:sec <- here I want to filter out the colon.

Do I need to put it into a string first and then do some splitting, or can I handle it in fscanf?

Update:

Okay, så I’ve been trying to get this to work now, but I simply cannot. I literally have no idea what the issue is.

#include <stdio.h>

/*
 Struct to hold data for each runners entry
 */
typedef struct {

    char name[21];
    char week_day[4];
    char month[11];
    int date,
    year,
    start_hour,
    start_min,
    distance,
    end_hour,
    end_min,
    end_sec;

} runnerData;

int main (int argc, const char * argv[])
{
    FILE *dataFile = fopen("/Users/dennisnielsen/Documents/Development/C/Afleveringer/Eksamen/Eksamen/runs.txt", "r");
    char ch;
    int i, lines = 0;

    //Load file
    if(!dataFile)
        printf("\nError: Could not open file!");

    //Load data into struct.
    ch = getc(dataFile);

    //Find the total ammount of lines
    //To find size of struct array
    while(ch != EOF){
        if(ch == '\n')
            lines++;

        ch = getc(dataFile);
    }

    //Allocate memory
    runnerData *list = malloc(sizeof(runnerData) * lines);

    //Load data into struct
    for(i = 0; i < lines; i++){

        fscanf(dataFile, "%s %s %s %d, %d %d:%d %d %d:%d:%d %[\n]",
               list[i].name,
               list[i].week_day,
               list[i].month,
               list[i].date,
               list[i].year,
               list[i].start_hour,
               list[i].start_min,
               list[i].distance,
               list[i].end_hour,
               list[i].end_min,
               list[i].end_sec);

        printf("\n#%d:%s", i, list[i].name);
    }  

    fclose(dataFile);


    return 0;
}

I’ve been told that “only strings to do not require & in front of them in fscanf();” but I tried both with and without ampersand to no avail.

  • 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-27T03:16:56+00:00Added an answer on May 27, 2026 at 3:16 am

    Put the “noise” in the format string.

    Also you might like to limit the size of strings.

    And get rid of the & for arrays.

    And test the return value from scanf!

    // John Mon September 5, 2011 09:18 5830 0:26:37
    if (scanf("%19s%2s%9s%d,%d%d:%d%d%d:%d:%d", ...) != 11) /* error */;
    //             ^^^ error: not enough space
    

    Notice week_day has space for 2 characters and the zero terminator only.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this data from a xml file: <?xml version=1.0 encoding=utf-8 ?> <words> <id>...</id>
I have a web service that queries data from this json file, but I
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
I'm probably doing this all wrong. I have a text file full of data
I have an app where a property file contains data in the following format
I have this php code: $lines = file(data.csv); $nested = array(); $links = array();
I have very little knowledge of pig. I have protobuf format data file. I
I have problem with this code..I want to extract data from flat file and
I have a file data.base which looks like: 1234 XXXX 4321 XXXX 9884 ZZZZ
I have a file with data listed as follows: 0, 2, 10 10, 8,

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.