Thread: Recovering Matrix from a file.

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    5

    Recovering Matrix from a file.

    Hello, so i've this program that scan and print the matrix from a file. anyways the program i have works with normal matrix i mean Square matrix but now i want to make a manual matrix i mean i've to enter the number of lines/columns and then i call the lines and columns in the main.
    So the program below explains the situation
    Code:
    int recuperation (int t[][20], char *nomFichier){
        int nbElement=0 ,i,j,nbElement2=0;
        FILE *fp;
        fp=fopen(nomFichier,"r");
        if(fp!=NULL)
        {
    
    
            fscanf(fp,"%d\n",&nbElement);
            fscanf(fp,"%d\n",&nbElement2);
            if(nbElement && nbElement2)
            {
                for(i=1;i<=nbElement;i++)
                {
                   for(j=1;j<=nbElement2;j++)
                   {
                    fscanf(fp,"%d",&t[i-1][j-1]);
                   }
    
    
                }
            }
        }
        else
            printf("\n Fichier vide \n");
        return nbElement;;
    }
    You see the return? nbElement that's the number of lines but i want to return the number of columns too, which is nbElement2.
    Because later in main() i've to call this function by typing:
    l= recuperation(t,txtfile)
    but i can't call the columns since i returned only 1 value.
    Hope that you got what i mean, thanks.

  2. #2
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
     
    #define MAX_ROWS 20
    #define MAX_COLS 20
     
    bool recuperation(int t[][MAX_COLS], const char *nomFichier, int *rows, int *cols) {
        FILE *fp = fopen(nomFichier, "r");
     
        *rows = *cols = 0;
     
        if (!fp) {
            printf("\n Fichier vide \n");
            return false;
        }
     
        fscanf(fp, "%d%d", rows, cols);
     
        if (*rows <= 0 || *rows > MAX_ROWS || *cols <= 0 || *cols > MAX_COLS) {
            fclose(fp);
            printf("\nNumber of rows or columns out of range\n");
            return false;
        }
     
        for (int r = 0; r < *rows; r++)
           for (int c = 0; c < *cols; c++)
            fscanf(fp, "%d", &t[r][c]);
     
        fclose(fp);
        return true;
    }
     
    int main() {
        int t[MAX_ROWS][MAX_COLS];
        int rows, cols;
        if (!recuperation(t, "myfile", &rows, &cols)) {
            printf("Could not read file\n");
            exit(EXIT_FAILURE);
        }
         
        for (int r = 0; r < rows; r++) {
           for (int c = 0; c < cols; c++)
               printf("%2d ", t[r][c]);
           printf("\n");
        }
     
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Recovering jpeg images from a .raw file
    By Dude22 in forum C Programming
    Replies: 18
    Last Post: 03-07-2013, 10:55 AM
  3. How do I go about recovering these files?
    By JFonseka in forum Tech Board
    Replies: 2
    Last Post: 01-15-2010, 06:43 AM
  4. Recovering CD-RW Files.
    By civilwarsearch in forum Tech Board
    Replies: 6
    Last Post: 01-08-2004, 08:13 AM
  5. recovering lost files
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-08-2002, 06:09 PM

Tags for this Thread