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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:47:45+00:00 2026-06-12T05:47:45+00:00

I am trying to read a file which contains the coordinate values for my

  • 0

I am trying to read a file which contains the coordinate values for my code.

My question is, how do I make the array size large enough to contain future addition to the file?

Below is my code; when I set the array to size 905, my loop continues until the space is filled. Why is that?

FILE.txt:

S (60,70)(200,200)
S (30,40)(100,200)
S (10,20)(80,10)
S (60,400)(700,200)
S (160,70)(240,20)
S (160,70)(240,20)
S (160,70)(240,20)

My code:

#include <stdio.h>
int a;
int b;
int c;
int d;
int data[905][4];
int main ( int argc, char *argv[] )
{
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s filename", argv[0] );
    }
    else 
    {
        // We assume argv[1] is a filename to open
        FILE *file = fopen( argv[1], "r" );

        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            int j=0;int count=1
            for (j=0; j < count; j++) 
            {    
                fscanf(file, "S (%d,%d)(%d,%d)", &a, &b, &c, &d);

                printf("%d,%d,%d,%d\n",a, b, c, d);
                count++
            }             

            fclose( file );
        }
    }
}
  • 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-12T05:47:46+00:00Added an answer on June 12, 2026 at 5:47 am

    You will need to use the malloc and realloc functions from <stdlib.h> for this. The basic idea is to allocate a certain amount of space up front, and then enlarge the array when you discover that it is not big enough.

    It will make life easier if you use an array of structures rather than an array of arrays:

    struct segment { int x0, y0, x1, y1; };
    

    and then you do something like this:

    size_t nsegs = 0, segs_allocated = 10;
    struct segment *segs = malloc(segs_allocated * sizeof(struct segment));
    if (!segs) abort();
    
    while (getline(&line, &linesz, stdin) >= 0)
    {
        if (!parse_line(&segs[nsegs], line)) continue;
        nsegs++;
        if (nsegs == segs_allocated)
        {
            segs_allocated *= 2;
            segs = realloc(segs, segs_allocated * sizeof(struct segment));
            if (!segs) abort();
         }
    }
    

    Obligatory tangential comment: Forget you ever heard of fscanf. It is far more trouble than it is worth. Read entire lines with getline (if you don’t have it, it’s not hard to implement), extract individual number-strings from the line with a hand-coded parser, and convert them to machine integers with strtol, strtoul, or strtod as appropriate.

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

Sidebar

Related Questions

I am trying to write some simple code which will read a text file
I am trying to read the contents of a file (which contains both 'standard'
I'm trying to read a file which contain English & Arabic characters on each
I am trying to read lines from a file which contains hashed data. The
Here I'm trying to read a text file which contains only integers.in every line.For
I'm trying to read a file which is encoded in ISO-8859(ansi), and it contains
I am trying to read a html file which is a plain page with
I am trying to read the file with the following format which repeats itself
I have an xml file(from federal government's data.gov) which I'm trying to read with
I need to read the string in the text file which contains the default

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.