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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:08:10+00:00 2026-05-23T18:08:10+00:00

I am having trouble with some file IO stuff. I have this file: db.dat:

  • 0

I am having trouble with some file IO stuff.

I have this file:

db.dat:

Ryan
12 69.00 30.00 0.00
Bindy Lee
25 120.00 89.00 1.00

And this is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define RECORDS 30
#define LEN 20

main()
{
    FILE *fptr;
    fptr = fopen("db.dat", "r");
    int i;
    int counter = 2;

    for (i = 0; i < counter; i++)
    {
        char temp1[LEN];
        char temp2[LEN + 10];

        fgets(temp1, LEN, fptr);
        fgets(temp2, LEN, fptr);
        printf("%s %s", temp1, temp2);
    }

    fclose(fptr);      
}

I am supposed to get both lines, but I am getting this instead:

Ryan
 12 69.00 30.00 0.00
 Bindy Lee

Can someone please help! I don’t know why I am not getting both lines, and why I am getting spaces. Very odd…Thanks!!!!

  • 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-23T18:08:11+00:00Added an answer on May 23, 2026 at 6:08 pm

    fgets stops after it reads LEN characters OR reaches the end of the line. I think your problem here is that you made LEN too small.

    Change your printf to something more verbose like printf("temp1='%s'\ntemp2='%s'\n", temp1, temp2); and you should be able to see what was actually read into each string.

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

Sidebar

Related Questions

I'm having some trouble with an xml file, I have found all sorts of
I'm having some trouble setting up a htaccess file. Currently I have 15 domains
I'm having some trouble reading data from a file into a vector of Orders.
I am having some trouble trying to print from a file. Any ideas? Thanks
I'm having some trouble with replacing a portion of a file in binary mode.
I'm having some trouble with some struct typedef declarations in a header file not
I'm having trouble with some URL rewriting. All of the stuff below works fine,
I'm having some trouble getting an SVG file set as a background of an
I need to parse an XLIFF file using C#, but I'm having some trouble.
I'm having trouble deciding whether not this code should compile or if just both

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.