Thread: Reverse the Order of Lines

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    1

    Reverse the Order of Lines

    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

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Here's a loose example reversing then replacing original file, you'll have to adapt to suit your needs
    Code:
    #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:Made slight mistake with fgets usage at the start, corrected
    Edit:Forgot to add the file flush, corrected
    Last edited by awsdert; 01-25-2015 at 03:28 PM.

  3. #3
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by awsdert View Post
    Here's a loose example reversing then replacing original file, you'll have to adapt to suit your needs
    Code:
    #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:Made slight mistake with fgets usage at the start, corrected
    Edit:Forgot to add the file flush, corrected
    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.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A linked list can make it easy too.

    Code:
    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);
    }
    Requisite parts being available of course
    Last edited by whiteflags; 01-25-2015 at 04:37 PM.

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    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

  6. #6
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by awsdert View Post
    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
    You mean it was even more broken before you "fixed" it?

  7. #7
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    yeah, i had at the first use of fgets this line:
    Code:
    lines[count] = fgets( buff, TXT_MAX_LINE_LEN, oldFile );
    Even I have to admit that was pretty dumb of me , 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-30-2010, 10:22 PM
  2. Reverse order printing
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 08-19-2007, 06:47 AM
  3. Printing in reverse order
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 08-17-2007, 04:30 AM
  4. reverse the order of an array
    By dustyd in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2003, 11:14 AM
  5. how do I reverse order of my array
    By jgonzales in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2002, 03:48 PM