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

  • Home
  • SEARCH
  • 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 6737287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:13:53+00:00 2026-05-26T11:13:53+00:00

I need to read from a file from using C. #include <stdio.h> struct record{

  • 0

I need to read from a file from using C.

 #include <stdio.h>

struct record{
    char name[2];
    int arrival_time;
    int job_length;
    int job_priority;
};

const int MAX = 40;

main(){

struct record jobs[MAX];
FILE *f;
fopen("data.dat","rb");
int count =0;
while(fscanf(f, "%c%c %d %d %d", &jobs[count].name, &jobs[count].arrival_time, &jobs[count].job_length, &jobs[count].job_priority) != EOF){
count++;
}
int i;
for(i =0;i<count;i++){
printf("%c%c %d %d %d", &jobs[count].name, &jobs[count].arrival_time, &jobs[count].job_length, &jobs[count].job_priority);
}


}

The data file’s format is the following:

     A1 3 3 3
     B1 4 4 4
     C1 5 5 5

…

First one is char[2] and the other three int. I can’t get the code right to read in until the end of file.

Anyone come across this before?

Updated Code.

  • 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-26T11:13:53+00:00Added an answer on May 26, 2026 at 11:13 am

    There are a couple of modifications needed – most notably, you need to reference the jobs array of structures:

    #include <stdio.h>
    
    struct record{
        char name[2];
        int arrival_time;
        int job_length;
        int job_priority;
    };
    
    const int MAX = 40;
    
    int main(void)
    {
        struct record jobs[MAX];
        int i = 0;
        int j;
        FILE *f = fopen("data.dat","rb");
    
        while (fscanf(f, "%c %d %d %d", &jobs[i].name[0], &jobs[i].arrival_time,
                      &jobs[i].job_length, &jobs[i].job_priority) == 4 && i < MAX)
            i++:
    
        for (j = 0; j < i; j++)
            printf("%c %d %d %d\n", jobs[j].name[0], jobs[j].arrival_time,
                   jobs[j].job_length, jobs[j].job_priority);
    
        return(0);
    }
    

    I make sure the loop doesn’t overflow the array. I print the data out (it is the most basic form of checking that you’ve read what you expected). The most subtle issue is the use of jobs[i].name[0]; this is necessary to read and print a single character. There’s no guarantee that there is any particular value in jobs[i].name[1]; in particular, it is quite probably not an NUL '\0', and so the name is not null terminated. It seems a bit odd using a single character name; you might want to have a longer string in the structure:

    char name[MAX];  // Lazy reuse of MAX for two different purposes!
    
    fscanf(f, "%.39s ...", jobs[i].name, ...
    
    printf("%s ...", jobs[i].name, ...
    

    The & is now not needed. The %.39s notation is is used to read a length-limited string up to the first space. Note that the size in the string is one less than the size of the array. It can often be simplest simply to create the format string with sprintf() to get the size right.

    The code does not error check the fopen() statement.


    Your code prints out A 1 3 3 instead of A1 3 3 3. I tried to add a second %c to it and it did not resolve it.

    I discussed names longer than one character…

    If you need to read “A1”, you need the name member to be bigger so that you can null terminate the string, and you need to use %s rather than %c to read the value, and you need to lose the & and subscript, and you need to protect your code from buffer overflow (because where you expect ‘A1’, someone will inevitably enter ‘A1B2C3D4D5F6G7H8I9J10K11L12M13’ and wreak havoc on your code if you do not protect against the buffer overflow. Incidentally, the change in the test of the result of fscanf() (from == EOF to != 4 was also a protection against malformed input; you can get zero successful conversions without reading any characters, in general – though your %c would eat one character per iteration).

    #include <stdio.h>
    
    struct record{
        char name[4];
        int arrival_time;
        int job_length;
        int job_priority;
    };
    
    const int MAX = 40;
    
    int main(void)
    {
        struct record jobs[MAX];
        int i = 0;
        int j;
        FILE *f = fopen("data.dat","rb");
    
        while (fscanf(f, "%.3s %d %d %d", jobs[i].name, &jobs[i].arrival_time,
                      &jobs[i].job_length, &jobs[i].job_priority) == 4 && i < MAX)
            i++:
    
        for (j = 0; j < i; j++)
            printf("%s %d %d %d\n", jobs[j].name, jobs[j].arrival_time,
                   jobs[j].job_length, jobs[j].job_priority);
    
        return(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read a file from the local file system using JSP, save
I need to read selected files, matching on the file name, from a remote
I need to read some filenames from an xml config file using xmlstarlet with
I need to read a BufferedImage from file, which doesn't use DataBufferInt (as normally),
I need to read the data from a file that can be either comma
I need to read some data from an input file and plot a graph
I need to read the value of a property from a file in an
I need to read small sequences of data from a 3.7 GB file. The
I need to read from and write to Excel spreadsheets using Delphi 2010. Nothing
All I need to do is a simple read from a file in the

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.