#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<string.h>intGetfilelen (FILE *file) { intLen =0; if(File = =NULL) { return-1; } Charbuf[1024x768]; //read each row while(Fgets (BUF,1024x768, file)! =NULL) {Len++; } //Set CursorFseek (file,0, Seek_set); returnLen;} //file pointer length heap arrayvoidReadfiledata (file * file,intLenChar**Parray) { if(File = = NULL | | Len <=0|| Parray = =NULL) { return; } Charbuf[1024x768]; //read each row intindex =0; while(Fgets (BUF,1024x768, file)! =NULL) { intCurrentlen = strlen (BUF) +1; Char* CURRENP = (Char*)malloc(sizeof(Char) *Currentlen); strcpy (CURRENP, buf); Parray[index++] =CURRENP; memset (BUF,0,1024x768); }}voidShowfiledata (Char* * Parray,intLen) { for(inti =0; i < Len; i++) {printf ("Line%d content:%s", i+1, Parray[i]); }}voidFreeSpace (Char* * Parray,intLen) { if(Parray = = NULL | | len = =NULL) { return; } for(inti =0; i < Len; i++) { if(Parray[i]! =NULL) { Free(Parray[i]); Parray[i]=NULL; } }}voidtest01 () {FILE* File = fopen ("./test.txt","R"); if(File = =NULL) {printf ("failed to open file! \ n"); return; } intLen =Getfilelen (file); Char* * Parray = (Char**)malloc(sizeof(Char*) *Len); Readfiledata (file, Len, Parray); Showfiledata (Parray, Len); FreeSpace (Parray, Len); Parray=NULL;}voidMain () {test01 (); Putchar ('\ n'); System ("Pause");}
C reads txt to put data in the heap area