hello, i am trying to store a text from file into array to do some editing on it
i did this

void readFile(){

FILE *fp;

long lSize;
char *buffer;

fp = fopen ( "s.txt" , "rb" );

if( !fp ) perror("s.txt"),exit(1);

fseek( fp , 0L , SEEK_END);
lSize = ftell( fp );
rewind( fp );

/* allocate memory for entire content */

buffer = calloc( 1, lSize );
text= calloc (1,lSize);

if( !buffer )
{
fclose(fp);
fputs("memory alloc fails",stderr);
exit(1);
}

/* copy the file into the buffer */
if( 1!=fread( buffer , lSize, 1, fp) )
 fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1);

fclose(fp);

}

now i need to use this array in the main function but i could not do that in anyway i am trying all the day but the content of array always dissapear out of the function readfile

What content? You always free buffer (also note that it's a local pointer and not accessible outside of the function) and never populate text. The function certainly reads a file, but it doesn't do much more than that.

how can i do that
i need to read the a such content of a file

Hi  mom.how  is DAD today?when he        comes back HOME,tell him to      cAll me;


else,       I will caLL    him tomorrow   .   bye      for     noW.




Hi           dad           .      How       are      you      ?

put it in array and then doing some functions like deleting the spaces, capital and small letter , counting words
how can i improve the code to put this content in way i can do what i want on it, i did the functions on arrays not readed from file but i must also read from file now.

i could return i pointer to the main like

char *readfile(){
.
.
.
}
Member Avatar for Mouche

You should start with making sure you have read the whole file in. You could do that by printing what's in the buffer after you read it.

From there, you could go through the buffer one character at a time and copy the characters you want over to a second buffer. You already have a second buffer called 'text' but it's not declared anywhere. At the same time of copying characters you want, you could manipulate those characters like changing from lowercase to uppercase or vice versa.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.