Thread: Read a file and put an array into 2D array

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    1

    Read a file and put an array into 2D array

    I have a task. I must create a function which has specific arguments (int readArray(FILE *wp, char *name, int n, double M[n][n]). I must read an array name from a file and put in an array of pointers, then I must read an array and put their value into an array. I try to run my program, but I get a segmentation fault.

    Also in my file, I have the second array, so I must know, how to put them to another array(arr2), but I must read from the fifth line. How to do this? I think about using a scanf and in this scanf, I would ask the user from which line he wants to read, but I don't know how to do that. Or maybe I should try to detect string, and if I detect a second-string I would read from a second-string

    This is my program
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int readArray(FILE *wp, char *name, int n, double M[n][n])
    {
        int i = 0, j;
        fscanf(wp, "%s", &name[0]);
        printf("Print name: %s", name);
        return 0;
        for (i = 0; i < n; ++i)
            for (j = 0; j < n; ++j)
                fscanf(wp, "%lf", &M[i][j]);
        printf("Print array: %s", name);
    
    }
    
    int main (int argc, char *argv[])
    {
        FILE *wz;                         
        int n = 3;
        char *name[1];
        char *name2[1]; 
        double arr[n][n];
        double arr2[n][n];
    
        
        if (argc != 2) {                                
        printf("Wrong arguments number\n");
        printf("I should run this way:\n");
        printf("%s source\n",argv[0]);
        exit(1);
        }
        
        if( (wz= fopen(argv[1],"r")) == NULL) {
            printf("Open error %s\n", argv[1]);
            exit(1);
        }
    
        readArray(wz, *name, n, arr);
        readArray(wz, *name2, n, arr2);    
        
        int fclose(FILE *wz);
        int fclose(FILE *wc);
        
        return 0;
    }
    This is my text file, which I have:
    Code:
    arr1
    9 8 7
    6 5 4
    3 2 1
    arr2
    1 2 3
    4 5 6
    7 8 9
    When I read only name -everything is fine
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int readArray(FILE *wp, char *name)
    {
        int i = 0, j;
        fscanf(wp, "%s", &name[0]);
        printf("Print name: %s", name);
    
        return 0;
    }
    
    int main (int argc, char *argv[])
    {
        FILE *wz;                         
        int n = 3;
        char *name[1];
    
    
        
        if (argc != 2) {                                
        printf("Wrong arguments number\n");
        printf("I should run this way:\n");
        printf("%s source\n",argv[0]);
        exit(1);
        }
        
        if( (wz= fopen(argv[1],"r")) == NULL) {
            printf("Open error %s\n", argv[1]);
            exit(1);
        }
    
        readArray(wz, *name);
        
        int fclose(FILE *wz);
        int fclose(FILE *wc);
        
        return 0;
    }
    When I read only array, when I use this txt file
    Code:
    9 8 7
    6 5 4
    3 2 1
    and this program
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int readArray(FILE *wp, int n, double M[n][n])
    {
        int i, j;
        for (i = 0; i < n; ++i)
            for (j = 0; j < n; ++j)
                fscanf(wp, "%lf", &M[i][j]);
        printf("Two Dimensional array elements:\n");
        for(i=0; i<3; i++) 
        {
            for(j=0;j<3;j++) 
            {
                printf("%f ", M[i][j]);
            }
        }
        return 0;
    }
    
    int main (int argc, char *argv[])
    {
        FILE *wz;                         
        int n = 3;
        double arr[n][n];
    
        
        if (argc != 2) {                                
        printf("Wrong arguments number\n");
        printf("I should run this way:\n");
        printf("%s source\n",argv[0]);
        exit(1);
        }
        
        if( (wz= fopen(argv[1],"r")) == NULL) {
            printf("Open error %s\n", argv[1]);
            exit(1);
        }
    
        readArray(wz, n, arr);
        
        int fclose(FILE *wz);
        int fclose(FILE *wc);
        
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Presently, you declared name in main to be an array of 1 pointer to char. Since you did not set that pointer to point to anything, you don't actually have space allocated to store a name. It would be better to declare it as:
    Code:
    char name[NAME_MAXLEN + 1];
    where NAME_MAXLEN is a constant for the maximum length of a name. Then you pass name rather than *name to readArray. The same goes for name2.

    To avoid buffer overflow, NAME_MAXLEN should be used to construct the format string for the fscanf call that reads into name, but you can do this after you have gotten the basic version to work.

    You should be aware that you're using variable length arrays, even though the value of n appears to be known at compile time. If that was not intended, then change n to be a macro (and perhaps rename it to N or a more descriptive name).

    Note that you did not call fclose correctly: where you appear to be trying to call it, you wrote function declarations instead of function calls.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  2. How to read file to array...help please
    By MG2610 in forum C Programming
    Replies: 1
    Last Post: 11-12-2009, 11:45 PM
  3. Read file into array
    By neobarn in forum C Programming
    Replies: 6
    Last Post: 01-09-2009, 12:41 PM
  4. Read .txt file into 2D array.
    By crazygopedder in forum C Programming
    Replies: 11
    Last Post: 10-21-2008, 08:42 PM
  5. read from file into Array
    By tinus in forum C Programming
    Replies: 11
    Last Post: 04-13-2007, 07:11 AM

Tags for this Thread