File Reverse

This is a discussion on File Reverse within the C Programming forums, part of the General Programming Boards category; Help me in this code. Code: #include<stdio.h> #include<stdlib.h> FILE *fp1,*fp2; int main() { char ch; int i; fp1 = fopen("File1.txt","r"); ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    India
    Posts
    3

    Post File Reverse

    Help me in this code.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    FILE *fp1,*fp2;
    
    int main()
    {
    	char ch;
    	int i;
    	
    	fp1 = fopen("File1.txt","r");
    	fp2 = fopen("File2.txt","w");
    	
    	if(fp1 == NULL)
    	{
    		printf("Can't open file\n");
    		exit(1);
    	}
    	if(fp2 == NULL)
    	{
    		printf("Can't open file\n");
    		exit(1);
    	}
    	fseek(fp1,0L,SEEK_END);
    	i = ftell(fp1);
    	i--;
    	for(i; i >= 0; i--)
    	{
    		ch = fgetc(fp1); 
    		ch = fputc(ch,fp2);
    		fseek(fp1,-1L,SEEK_CUR);
    	}
    	fclose(fp1);
    	fclose(fp2);
    }
    this code is not working.
    can u help me out in solving this.

    thanks.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    fseek(fp1,-1L,SEEK_CUR);
    As I understand - it just moves the pointer to the previous position, so you are trying to read the same byte over and over againg

    fseek(fp1,0L,SEEK_END);
    this moves the pointer to the end of file
    so next read operation will return EOF and fail

    You should really check the return values of all APi functions you call - in this case it will be easier to analyze the problem

    fgetc returns an int - so ch should be declared as int, In this case you can check whether it is EOF
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I've never tried this, but after each read, wouldn't you need to back up two positions?
    Code:
    	fseek(fp1,0L,SEEK_END);
    	i = ftell(fp1);
    	i--;
    	fseek(fp1, -1L, SEEK_CUR);
    	for(; i >= 0; i--)
    	{
    		ch = fgetc(fp1); 
    		ch = fputc(ch,fp2);
    		fseek(fp1, -2L, SEEK_CUR);
    	}
    Last edited by swoopy; 10-17-2007 at 12:45 AM.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* Copy one file to another in reverse order. */
    
    int ReverseCopyFile(const char *file1, const char *file2)
    {
        if(!file1 || !file2)
        {
                  printf("Error, NULL input file names.\n");
                  return -1;
        }
        else
        {
            FILE *fptr1, *fptr2 = NULL;
            /* Index. */
            int i = 0;
            /* Length. */
            int fLen = 0;
            /* Our character. */
            char cChar;
            
            if((fptr1 = fopen(file1, "r")) == NULL || (fptr2 = fopen(file2, "w")) == NULL)
            {
                      fprintf(stderr,"Error can not open '%s' file.\n", fptr1 == NULL ? file1 : file2);
                      return -2;
            }
            else
            {
                fseek(fptr1, 0, SEEK_END);
                if((fLen = ftell(fptr1)) == -1L)
                         return -3;
                else
                {
                    for(i = 1; i <= fLen; i++)
                    {
                          fseek(fptr1, fLen-i, SEEK_SET);
                          cChar = fgetc(fptr1);
                          fputc(cChar, fptr2);
                    }
                    /* Close Files. */
                    fclose(fptr1);
                    fclose(fptr2);
                    
                    return 0;
                }
                
            }
        }
    }
    
    int main(int argc, char *argv[])
    {
      ReverseCopyFile("from.txt", "to.txt");
      system("PAUSE");	
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2007
    Location
    India
    Posts
    3
    [To Swoopy]
    Thanks Swoopy. Now it's working good.

    And that another code is also good. But that system command is not working.


    Bye

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  2. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21