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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:49:41+00:00 2026-05-21T06:49:41+00:00

I want a simple C program, which will read a file and saves the

  • 0

I want a simple C program, which will read a file and saves the content of each line to an array element. The file contains all the integer value. Only one integer value is present per line. In this way each of the integer value is to stored in an array.

  • 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-21T06:49:42+00:00Added an answer on May 21, 2026 at 6:49 am

    Here is an example which does what you ask, with error checking, and dynamically resizing your array as more data is read in.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char ** argv)
    {
        char buf[512];
        FILE * f;
        int * array = 0;
        size_t array_len = 0, count = 0;
    
        if (argc != 2) {
            fprintf(stderr, "Please provide a filename to read\n");
            exit(1);
        }
    
        f = fopen(argv[1], "r");
    
        if (f == NULL) {
            perror("fopen");
            exit(1);
        }
    
        while (fgets(&buf[0], 512, f) != 0) {
            if (count == array_len) {
                array_len *= 2;
                if (array_len == 0) {
                    array_len = 32;
                }
                array = realloc(array, array_len * sizeof(int));
                if (array == NULL) {
                    perror("realloc");
                    exit(1);
                }
            }
            array[count++] = strtol(buf, 0, 10);
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing a haskell program which compares each line of a file with
I want to write a simple program which shows my internet activity over a
Basically I want to make simple toggle program (that will be mapped to some
I want to create an extremely simple iPhone program that will open a telnet
I want to compile a simple hello-world-style program using the Windows command line. cl
I've written a simple FTP based file synchronization program. The settings of which can
so I'm making a really simple program in which I want one sprite to
I'm trying to write a very simple program, I want to print out the
I want to develop simple Serverless LAN Chat program just for fun. How can
I wrote a simple Windows Forms program in C#. I want to be able

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.