cfopen

Write file and read file in C


I am doing some homework and preparing for a final exam. Here is my problem:

A square matrix (nxn)

Write down the Even elements into matran.txt, then read from file and display it on screen

Code:

//this is function write down file matrix.txt, and it's work normally 
void infile(int **p,int n)
{
    bool a=false;
    FILE *f=fopen("matran.txt","w");
    if (f==NULL)
    {
        printf ("\nLoi file.");
        exit(1);
    }
    for (int i = 0; i < n; i++) 
      for (int j = 0; j < n; j++) {
        if ((p[i][j]) % 2 == 0) {
          fprintf (f,"%d\t",p[i][j]);
          a=true;
        }
      }   
    if (a) printf ("\nFile da duoc ghi tai ./matrix.txt");
    else printf ("\nKhong co so chan trong ma tran");

}

I know that it's has a limit from 0->n but I don't know what is limit of i and I tried set limit to 100 but it's still print out 3 values and it's display 100 diff address:

//but this didnt work
void DocTep(int **p, int n) {
  int *ptr=(int*)malloc(n*sizeof(int));
  FILE *f = fopen("matran.txt", "r");
  if (f == NULL) {
    printf("\nLoi file.");
    exit (1);
  }
  for (int i = 0; i < n; i++) {
        fscanf(f, "%d ",ptr[i]);
  }

  fclose(f);
  printf ("\n");
  for (int i = 0; i < n; i++) {
    printf ("%d\t",ptr[i]);
  }
}

This is full program

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void nhapmt (int **p,int n)
{
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      printf("a[%d][%d] = ", i, j);
      scanf("%d", &p[i][j]);
    }
  }
}

void inmt (int **p,int n)
{
    for (int i=0;i<n;i++)
    {
    printf ("\n");
    for (int j=0;j<n;j++)
    {
        printf ("%d\t",p[i][j]);
    }
}
}

void tongcheo(int **p,int n)
{
    int sum=0;
    for (int i=0;i<n;i++)
    {
        sum += p[i][i];
    }   
    printf ("\nTong cac so tren duong cheo chinh la: %d",sum);
}

void tongh(int **p,int n,int h)
{
    int sum=0;
    for (int i=0;i<n;i++)
    {
        sum += p[i][h];
    }   
    printf ("Tong cot %d la: %d",h,sum);
}

void infile(int **p,int n)
{
    bool a=false;
    FILE *f=fopen("matran.txt","w");
    if (f==NULL)
    {
        printf ("\nLoi file.");
        exit(1);
    }
      for (int i = 0; i < n; i++) 
      for (int j = 0; j < n; j++) {
        if ((p[i][j]) % 2 == 0) {
        fprintf (f,"%d\t",p[i][j]);
        a=true;
    }
    }   
    if (a) printf ("\nFile da duoc ghi tai ./matran.txt");
    else printf ("\nKhong co so chan trong ma tran");

}


void DocTep(int **p, int n) {
    int *ptr=(int*)malloc(100*sizeof(int));
  FILE *f = fopen("matran.txt", "r");
  if (f == NULL) {
    printf("\nLoi file.");
    exit (1);
  }
//    fscanf(f,"%d",&ptr[0]);
  for (int i = 0; i < n; i++) {
        fscanf(f, "%d ",ptr[i]);
  }

    fclose(f);
    printf ("\n");
    for (int i = 0; i < n; i++) {
        printf ("%d\t",ptr[i]);
}
}

int main()
{
    int **p,n,h;
    printf ("Hay nhap vao n: ");
    scanf("%d",&n);
    p=(int**)malloc(n*sizeof(int*));
    for (int i=0;i<n;i++){
    p[i]=(int*)malloc(n*sizeof(int));
    }
    nhapmt(p,n);
    inmt(p,n);
    tongcheo(p,n);
    printf ("\nHay nhap vao h: ");
    scanf("%d",&h); 
    tongh(p,n,h);
    infile (p,n);
    DocTep(p,n);
    free(p);
    return 0;
}

Input n=3 2 4 6 8
in matran.txt: 2 4 6 8
Output: 2 4 6 8


Solution

  • In the DocTep routine, you won't know in advance how many items you need to read. (So where is the n parameter coming from?)

    If all you need to do is print the even values that were found in infile, you don't need to worry about managing the memory for **p or storing the values. You can use a while loop that terminates when you reach end-of-file (which the feof() routine can tell you), or use the return value of fscanf to tell when there are no more values left to read.