Reading a 2d Array From File in C
04-26-2020 #1
Registered User
Read a file and put an assortment into 2D array
I accept a task. I must create a office which has specific arguments (int readArray(FILE *wp, char *name, int northward, double Grand[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 programme, but I become a partitioning mistake.Also in my file, I have the second array, so I must know, how to put them to another assortment(arr2), but I must read from the fifth line. How to exercise this? I think near using a scanf and in this scanf, I would ask the user from which line he wants to read, simply I don't know how to practice that. Or possibly I should try to detect string, and if I observe a 2d-string I would read from a 2d-cord
This is my program
This is my text file, which I have:Code:
#include <stdio.h> #include <stdlib.h> int readArray(FILE *wp, char *proper noun, int n, double Chiliad[northward][n]) { int i = 0, j; fscanf(wp, "%s", &proper noun[0]); printf("Print proper noun: %due south", proper noun); return 0; for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) fscanf(wp, "%lf", &Thou[i][j]); printf("Impress array: %due south", proper name); } int main (int argc, char *argv[]) { FILE *wz; int n = 3; char *name[1]; char *name2[i]; double arr[north][n]; double arr2[n][n]; if (argc != 2) { printf("Wrong arguments number\n"); printf("I should run this way:\northward"); printf("%s source\n",argv[0]); exit(1); } if( (wz= fopen(argv[i],"r")) == NULL) { printf("Open up error %s\n", argv[one]); get out(1); } readArray(wz, *name, due north, arr); readArray(wz, *name2, n, arr2); int fclose(FILE *wz); int fclose(FILE *wc); return 0; }
When I read only name -everything is fineLawmaking:
arr1 nine 8 vii half-dozen 5 4 3 ii 1 arr2 one 2 3 iv 5 6 7 8 nine
When I read only array, when I use this txt fileCode:
#include <stdio.h> #include <stdlib.h> int readArray(FILE *wp, char *proper noun) { int i = 0, j; fscanf(wp, "%south", &proper noun[0]); printf("Print proper noun: %southward", name); return 0; } int master (int argc, char *argv[]) { FILE *wz; int n = 3; char *name[one]; if (argc != 2) { printf("Wrong arguments number\n"); printf("I should run this way:\north"); printf("%due south source\due north",argv[0]); get out(1); } if( (wz= fopen(argv[1],"r")) == Aught) { printf("Open error %south\northward", argv[ane]); exit(1); } readArray(wz, *name); int fclose(FILE *wz); int fclose(FILE *wc); return 0; }
and this program
Lawmaking:
#include <stdio.h> #include <stdlib.h> int readArray(FILE *wp, int north, double M[n][north]) { int i, j; for (i = 0; i < north; ++i) for (j = 0; j < north; ++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 ", Thou[i][j]); } } return 0; } int master (int argc, char *argv[]) { FILE *wz; int n = iii; double arr[northward][n]; if (argc != 2) { printf("Incorrect arguments number\n"); printf("I should run this style:\north"); printf("%south source\due north",argv[0]); exit(1); } if( (wz= fopen(argv[1],"r")) == Nil) { printf("Open error %s\due north", argv[1]); exit(i); } readArray(wz, n, arr); int fclose(FILE *wz); int fclose(FILE *wc); render 0; }
Source: https://cboard.cprogramming.com/c-programming/179059-read-file-put-array-into-2d-array.html
0 Response to "Reading a 2d Array From File in C"
Post a Comment