C program to count the number of lines in a text file and reverse the contents in the file to write in an output file.
ex :
input
one
two
three
output:
three
two
one
C program to count the number of lines in a text file and reverse the contents in the file to write in an output file.
ex :
input
one
two
three
output:
three
two
one
Here's a loose example reversing then replacing original file, you'll have to adapt to suit your needs
Edit:Made slight mistake with fgets usage at the start, correctedCode:#include <stdio.h> #define TXT_MAX_LINE_LEN 0xFFFF typedef long int li; int main(void) { int _count = 100, count = 0; size_t size = (sizeof(li) * _count), clearSize = size; li *lines = (li*)memset( malloc( size ), 0, size ); FILE *oldFile = fopen( ".\example.txt", "r" ); FILE *curFile = fopen( ".\example_to_be_renamed.txt", "w" ); char buff[TXT_MAX_LINE_LEN] = {0}; while ( !feof( oldFile ) ) { lines[count] = ftell( oldFile ); fgets( buff, TXT_MAX_LIN_LEN, oldFile ); if ( (++count) == _count ) { lines = (int*)realloc( lines, (size += clearSize) ); memset( &lines[count], 0, clearSize ); _count += 100; } } while ( count > 0 ) { fseek( oldFile, lines[--count], SEEK_SET ); fgets( buff, TXT_MAX_LINE_LEN, oldFile ); fputs( buff, TXT_MAX_LINE_LEN, curFile ); } free( lines); fclose( oldFile ); fflush( curFile ); fclose( curFile ); remove( ".\example.txt" ); rename( ".\example_to_be_renamed.txt", ".\example.txt" ); return 0; }
Edit:Forgot to add the file flush, corrected
Last edited by awsdert; 01-25-2015 at 03:28 PM.
That code is a complete mess though.
For starters: You don't check the return value from malloc(3). You don't check the return value of fopen. You shouldn't control a while loop with feof(3). The way you use fgets(3) is broken. The way you use realloc(3) is broken.
Those are the problems I spotted in 30 s.
If you are going to post a "complete" solution and disregard the homework rules, at least post one that is correct.
Last edited by Jimmy; 01-25-2015 at 04:41 PM.
A linked list can make it easy too.
Requisite parts being available of courseCode:while (fgets(buffer, sizeof buffer, read_me) != NULL) { elem = make_node(buffer); list_push(&head, elem); } /* later */ while ((cur = list_pop(&head)) != NULL) { fputs(cur->line, write_me); delete_node(cur); }![]()
Last edited by whiteflags; 01-25-2015 at 04:37 PM.
I did say loose, it wasn't meant to be perfect, he didn't mention whether it was his homework or not and if it is then he will need to fix any errors with the code itself as I did the code on memory of the functions anyway, after writing it I did a check on fgets and fseek to check my recollection was right and came back and fixed it after realising my main errors
yeah, i had at the first use of fgets this line:Even I have to admit that was pretty dumb of meCode:lines[count] = fgets( buff, TXT_MAX_LINE_LEN, oldFile );, though considering I didn't open any editors outside the page or consult any references for anything other than fgets and fseek I'd say it's not too bad an example. However I think we're veering off topic a bit now so I'll make this my last post in this thread