You are watching: Read text file into 2d array c++
#include #include int readArray(FILE *wp, char *name, int n, twin M
#include #include 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<>) record *wz; int n = 3; char *name<1>; if (argc != 2) printf("Wrong disagreements number\n"); printf("I need to 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;
Presently, you declared name in main to be selection of 1 guideline to char. Due to the fact that you did not set that guideline to point to anything, friend don"t in reality have an are allocated to store a name. It would be better to declare it as:
where NAME_MAXLEN is a constant for the maximum length of a name. Then you pass name quite than *name come readArray. The same goes for name2.To protect against buffer overflow, NAME_MAXLEN should be supplied to build the style string because that the fscanf call that reads right into name, but you have the right to do this after you have gotten the simple version to work.You must be aware that you"re utilizing variable size arrays, even though the worth of n shows up to be known at translate into time. If that was not intended, then adjust n to it is in a macro (and maybe rename it to N or a much more descriptive name).Note that you go not call fclose correctly: where you show up to it is in trying to contact it, you wrote function declarations rather of role calls.
Quick NavigationC ProgrammingTopSite AreasForumsGeneral Programming BoardsPlatform particular Boardsbrianowens.tv.com and AIHorizon.com"s synthetic Intelligence BoardsCommunity Boards
See more: H I Can Has Poland ? Cat Meme
Popular pagesExactly how to acquire started with C++ (or C) this particular day C TutorialC++ Tutorial5 ways you have the right to learn to program faster The 5 Most common Problems brand-new Programmers FaceHow to collection up a compiler8 typical programming mistakes What is C++11?Creating a game, from begin to finishRecent additions