I'm having difficulty trying to solve this programming issue I've been battling for the past four days. I've been to the forums, searched the web and read all of the related class materials (twice). Whatever I try does not seem to work.
I need an example of a C program that opens a text file, loads the content into a buffer, reverses the text in the buffer and then uses that data to overwrite the file. When you open the file, everything is backwards.
I'm using this to open the file I've selected:
fp = fopen(filename, "r");
I'm using this to allocate the space needed in the buffer and read the text into the buffer:
char *buffer = malloc(20000);
char buf[20000];
n = fread(buffer, 20000, 1, fp);
n = strlen(buffer);
I'm using this to reverse the string:
for ( ; n>=0 ;n--)
{
if ( (c=*(buffer+n)) !='\0' )
{
buf[i++]= c ;
}
}
I'm using this to close the file:
fclose(fp);
The way I have it now, the reversed text is displayed on the screen, but does not overwrite the file with the reversed text and save it. I tried something else that only ended up deleting everything in the text file.
Any assistance/direction would be greatly appreciated...
Thanks!

New Topic/Question
Reply
MultiQuote









|