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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:06:53+00:00 2026-06-07T07:06:53+00:00

I have this code to read integers from a file. I used the same

  • 0

I have this code to read integers from a file.
I used the same code to read doubles from another file
and worked perfectly, but this time the looping is
getting infinite. What could be? The file i’m reading
was written by a program, could be it? I don’t know
what does this mean but my OS says that the file is
binary. Well, any help will be appreciated. Thanks.

i=1;
cadeia = malloc ( i * sizeof(int) );
if (!cadeia){ //Avalia se a alocação de memória foi bem sucedida
  printf("Problema na alocação de memória para cadeia.");
  exit(0);
}
while ( !feof( arq_cadeia ) ){ /*Fazendo leitura e alocação de memória da matriz de transição*/
   fscanf ( arq_cadeia , "%d" , ( cadeia+i-1 ) );
   i++;
   cadeia = realloc ( cadeia , i * sizeof( int ) );
   if (!cadeia){ //Avalia se a alocação de memória foi bem sucedida
  printf("Problema na alocação de memória para cadeia.");
  exit(0);
   }
   printf("%d\n", *( cadeia+i-1 ));
}
  • 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-06-07T07:06:54+00:00Added an answer on June 7, 2026 at 7:06 am

    Your hunch is right, if your file is in binary format scanf can’t read it. If you can’t see the numbers with a text editor (e.g., Notepad), scanf can’t read them either.

    Your program has other problems, by the way: It’s terribly inefficient to realloc your entire array every time you read a number! The simplest thing would be to allocate so much space that you won’t need it all (don’t worry about the “waste”, as long as you don’t go way over the top). Second simplest is to measure the size of the file (you’ll need stat or some such) and estimate the number of integers in it. Third simplest, allocate enough for 10000 integers, and carefully check when you’re about to run out and allocate twice as much as you already had.

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

Sidebar

Related Questions

I have to read 100.000 rows from database, I use this code : List<elementClass>
Ignoring unsafe code, .NET cannot have memory leaks. I've read this endlessly from many
I have this code to read a CVS file. It reads each line, devides
I have Implemented Code to download a File from Internet, But I am getting
I have a data.frame from this code: my_df = data.frame(read_time = c(2010-02-15, 2010-02-15, 2010-02-16,
I have a case where the data read from the CSV file in the
I have a file that I read from, it contains a bunch of lines
Im trying to read in image file from a server , with the code
The problem I have is reading in multiple lines of integers from a file
I posted a similar question here: Read from a file containing integers - java

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.