C language file Operation 05--matrix (data) reading method

Source: Internet
Author: User

Data read
/*
===============================================================
Title: Read The matrix A, b from Test.txt, and calculate their product, output to the screen!
===============================================================
*/
#include <stdio.h>
#define M 3
#define N 5
void Main ()
{
FILE *FP;
int a[m][n],b[n][m],c[m][m];
int i,j,k;
Fp=fopen ("D:\\test.txt", "R");
if (fp==null)
printf ("File open failed!") \ n ");
Else
{
Read a, just like normal write, change scanf to FSCANF
printf ("Read Matrix a:\n");
for (i=0;i<m;i++)
for (j=0;j<n;j++)
FSCANF (FP, "%d", &a[i][j]);
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
printf ("%3d", A[i][j]);
printf ("\ n");
}

Read in B, just like normal write, change scanf to FSCANF
printf ("Read Matrix b:\n");
for (i=0;i<n;i++)
for (j=0;j<m;j++)
FSCANF (FP, "%d", &b[i][j]);
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
printf ("%3d", B[i][j]);
printf ("\ n");
}
Fclose (FP);
Multiplication of two-dimensional matrices
printf ("Matrix A and B product: \ n");
for (i=0;i<m;i++)
for (j=0;j<m;j++)
{
c[i][j]=0;
for (k=0;k<n;k++)
C[i][j]+=a[i][j]*b[j][i];
}
Output
for (i=0;i<m;i++)
{
for (j=0;j<m;j++)
printf ("%5d", C[i][j]);
printf ("\ n");
}
}
}
/*
========================================================================
Evaluation:

The reading of a two-dimensional array is actually a single fetch number, i.e. A[I][J];FSCANF (FP, "%d", &a[i][j]);

Use "R" when you save your data without any other characters, and a set of data is saved with a single file to ensure that the data can be

The reading sex.

========================================================================

*/




Copyright Notice: Bo Main article can be reproduced non-commercial, but please be sure to indicate the source, because the level is limited, inevitably error, in this disclaimer.

C language file Operation 05--matrix (data) reading method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.