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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:47:49+00:00 2026-06-10T08:47:49+00:00

I have a file(both .dat and .txt format) which contains numbers(integers) in rows and

  • 0

I have a file(both .dat and .txt format) which contains numbers(integers) in rows and columns form. I need to read
numbers(integers) from this file. That data is to be stored in a 2D-array. This array is defined in my C program.
I have tried to use file handling in C to accomplish this, but it is not reading the whole file.
The program stops abruptly at some data in the file and exits the program.
Following is my C code used for this :

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define EOL '\n'

int main(){

int i = 0,j = 0,array[][];          //i is row and j is column variable, array is the     target 2d matrix 
FILE *homer;
int v;
homer = fopen("homer_matrix.dat","w");   //opening a file named "homer_matrix.dat"
for(i=0;;i++)
  {
   for(j=0;;j++)
    {
            while (fscanf(homer, "%d", &v) == 1)           //scanning for a readable  value in the file
            {
                if(v==EOL)                                      //if End of line occurs , increment the row variable
                   break;
                array[i][j] = v;                                 //saving the integer value in the 2d array defined
            }
        if(v==EOF)
           break;                                                //if end of file occurs , end the reading operation.
    }

  }
fclose(homer);                                                        //close the opened file

for(i=0;i<=1000;i++)
  {
     for(j=0;j<=1200;j++)
        printf(" %d",array[i][j]);                          //printing the values read in the matrix.

   printf("\n");
   }


 }

Thanks Guys for the response, But the issue is something else..
allocated the memory for the 2-d array using the following code:

#define ROW 512

#define CLMN 512


for(i = 0; i <  ROW; i++)

  {

    for(j = 0;  j < CLMN; j++)

        {

array[i][j] = 0;

        }

  }  

Also I modified the permission to ‘r’ in the following code.

homer = fopen(" homer_matrix.txt" , "r"); 

Still, however, I am not able to get the 2-D entries into my variable ‘array’.

p.s. The “homer_matrix.txt” is generated using matlab through following commands:

CODE:

A=imread('homer.jpg');

I=rgb2gray(A);

dlmwrite('homer_matrix.txt',I);

This code will generate the file ‘homer_matrix.txt’ which contains the grayscale values of the image in a 768 X 1024 entry form.

  • 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-10T08:47:50+00:00Added an answer on June 10, 2026 at 8:47 am

    The following code will work for you.
    It will calculate exactly how many rows and columns you have in your text file.

    do {  //calculating the no. of rows and columns in the text file
        c = getc (fp);
    
        if((temp != 2) && (c == ' ' || c == '\n'))
        {
            n++;
        }
        if(c == '\n')
        {
            temp =2;
            m++;
        }
    } while (c != EOF);
    fclose(fp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control in silverlight which contains both XMAL and .cs file.
I have a setup in which I make use of a txt file (both
i have file which both show a form and validate it. when showing the
I have two string variables which are both file paths. The code that worked
I have 2 files namely: uploading.php emaillinks.php both include a file inc.php which has
I have the following in a file which is sourced both by .bashrc and
I have a properties file contains the file name only say file=fileName.dat . I've
I have a pdf file having both text and images contents. I need to
I am using one .g file for both lexer and parser. I have the
I have a category/file tree structure. Both categories and files can have parents, so

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.