Difficulties reading a contents of text file into 2*2 array?

There's more than one problem with your code.

Up vote 0 down vote favorite share g+ share fb share tw.

I encountered some difficulties reading a chars from a text file into a two-dimensional dynamic array -- it fails with "Unhandled exception at 0x011d155d in Problem_03_life. Exe: 0xC0000005: Access violation writing location 0xfdfdfdfd. " Here is a code: #include #include #include int main(int argc, char* argv){ if(argc "); FILE* f; f = fopen(argv1,"r"); if(f == NULL) printf("File cannot be opened!

\n"); /* Determining how many lines and length of string */ int m = 0; int n = 0; char c; while((c = fgetc(f))! =EOF) { if(c == '\n') {n++;} else m++; } m /= n; // counting quantity of chars in one line /* here i'm allocating memory for array (am I right? ) */ char** field = (char**)malloc(n*sizeof(char*)); for(int I = 0; I.

There's more than one problem with your code: The second for goes "one-too-far". Change " By definition it can only return fgetc. You need to rewind or consider a different strategy Also, you're making it harder than it has to be: *(field + i) can be written as fieldi *(*(field + i)+ j) can be written as fieldij.

And thanks -- all other things I changed. – Dima Reshetnikov Mar 17 at 9:14 @DimaReshetnikov Look into fseek or rewind. – cnicutar Mar 17 at 9:16 Do I need fseek() function to set position to the beginning of a file?

– Dima Reshetnikov Mar 17 at 9:18.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions