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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:42:47+00:00 2026-05-23T14:42:47+00:00

I am trying to read in from a file, and I can’t get the

  • 0

I am trying to read in from a file, and I can’t get the pattern of it right. Can someone tell me what I can do to get it working?

int main()
{

char name[20];
int age;
float highbp, lowbp, risk;
FILE *fp;
fp = fopen("data.dat", "r");

if(fp == NULL){

    printf("cannot open file\n\n");

}

while(fscanf(fp, "name:%s\nage:%d\nbp:%f\nrisk:%f", name, &age, &highbp, &risk) != EOF){


}
printf("Name: %s\n", name);
printf("%d\n", age);
printf("%f\n", highbp);
printf("%f\n", risk);

}

data.dat:

name:tom
age:32
bp:43.00
risk:0.0
  • 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-23T14:42:47+00:00Added an answer on May 23, 2026 at 2:42 pm

    If it can’t open the file it prints a message, but then continues. Instead it should return from main.

    if (fp == NULL) {
        printf("cannot open file\n\n");
        return 1;
    }
    

    fscanf will return the number of items parsed, so it’s probably safer to stop reading when the number returned < 4 (not all the items could be read).

    Presumably “data.dat” contains multiple records and each line has a line ending. This means that after reading the first record the next character in the file is the line ending for the “risk:0.0” line. You should end the fscanf template with \n.

    This is because the second time it tries to parse the file, fscanf will see that character, which it isn’t expecting (the fscanf template starts “name:”), so it will stop reading, and you’ll get only the first record.

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

Sidebar

Related Questions

I'm trying to read a specific line from a file and I can get
I'm trying to read from a file, but the only thing I get on
I'm trying to read encrypted struct data from file using fread(). once i get
I am trying to read a char from a file being passed in via
I'm trying to create a Zip file from .Net that can be read from
I am trying to read from file: The file is multiline and basically i
I am trying to read from a file and encrypt the data using AES
I am trying to read from a file on the local machine using Ajax.The
I'm trying to read from a file called quiz_questions.txt in my res/raw folder. The
I am trying to read characters from a file and writing them to another.

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.