I have actually a task. I must create a duty which has details arguments (int readArray(FILE *wp, char *name, int n, twin M). I have to read range name indigenous a document and placed in an array of pointers, climate I must read variety and put their value right into an array. I try to run my program, however I acquire a segmentation fault.Also in my file, I have the 2nd array, so I need to know, exactly how to put them to another array(arr2), however I need to read indigenous the fifth line. Exactly how to perform this? ns think about using a scanf and also in this scanf, I would certainly ask the user from which heat he wants to read, but I don"t know just how to carry out that. Or possibly I should try to finding string, and if i detect a second-string ns would read from a second-stringThis is my program


You are watching: Read text file into 2d array c++

#include #include int readArray(FILE *wp, char *name, int n, twin M){ int i = 0, j; fscanf(wp, "%s", &name<0>); printf("Print name: %s", name); return 0; for (i = 0; i
#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;

*

C++ Witch
*
Join day Oct 2003Location SingaporePosts 28,336
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.
*
I get maybe 2 dozen requests for aid with some sort of programming or design problem every day. Many have an ext sense 보다 to send me hundreds of lines that code. If castle do, ns ask lock to uncover the smallest instance that exhibits the problem and also send me that. Mostly, castle then find the error themselves. "Finding the smallest regime that demonstrates the error" is a an effective debugging tool.

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 pages
Exactly 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
*