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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:01:46+00:00 2026-05-21T11:01:46+00:00

i want to read text file containing the data of 4X4 matrix each element

  • 0

i want to read text file containing the data of 4X4 matrix each element is separated by a space and it is divided into 4 rows which represent the matrix. it is in Following form

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

now i came across the code that

 static const char filename[] = "file.txt";
   FILE *file = fopen ( filename, "r" );
   if ( file != NULL )
   {
      char line [ 128 ]; /* or other suitable maximum line size */

      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
         fputs ( line, stdout ); /* write the line */
      }
      fclose ( file );
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
   }
   return 0;

which reads the file but i want to know that how to read them element wise so that i can store them in 2D 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-21T11:01:47+00:00Added an answer on May 21, 2026 at 11:01 am

    fscanf to the rescue:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    
    int main()
    {
        FILE* file = fopen("input.txt", "r");
        if (!file)
            perror("Can't open input");
    
        int matrix[4][4] = {
            { 0, 0, 0, 0, },
            { 0, 0, 0, 0, },
            { 0, 0, 0, 0, },
            { 0, 0, 0, 0, },
        };
    
        int i;
        for (i=0; i<4; i++)
        {
            int n = fscanf(file, "%i %i %i %i", &matrix[i][0], 
                    &matrix[i][1],
                    &matrix[i][2],
                    &matrix[i][3]);
            if (n != 4) {
                if (errno != 0)
                    perror("scanf");
                else 
                    fprintf(stderr, "No matching characters\n");
            }
        }
    
        for (i=0; i<4; i++)
            printf("%i %i %i %i\n", matrix[i][0], 
                    matrix[i][1],
                    matrix[i][2],
                    matrix[i][3]);
    }
    

    Of course you need to make the code more generic

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

Sidebar

Related Questions

I want to read a text file containing space separated values. Values are integers.
I want to read a text file where fields data are separated by delimiter
I want to save a matrix to a text file, so I can read
I'm trying to read values from a text file into a hashtable, I want
I want to read text file's comma separated variables in a java script array
When I try to read a comma separated values-file containing scandinavian letters into a
I want to read text from pdf file and search text into pdf file.
I want read a text file into an array. How can I do that?
i want to read from a text file in C#. But I want all
I have a text file. I want read that file. But In that if

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.