Contribute  :  Web Resources  :  Past Polls  :  Site Statistics  :  Downloads  :  Forum  
    BiW ReversingThe challenge is yours    
 Welcome to BiW Reversing
 Wednesday, October 17 2018 @ 02:42 AM CEST
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to use strcat in c?

 
Post new topic   Reply to topic    www.reversing.be Forum Index -> Coding Corner
View previous topic :: View next topic  
Author Message
Zest
Frequent poster
Frequent poster


Joined: 24 Apr 2005
Posts: 56

PostPosted: Sun Aug 27, 2006 12:07 pm    Post subject: How to use strcat in c? Reply with quote

Hi,
Today I decided to code a small program that could make a backup file.
I wanted to change the extension of the given file to "BAK" to be able
to make a backup file.

So I coded the below program but when I tried to use strcat(); function
to change the extension of the given file to a backup file I faced a problem
and I was not able to handle it.

Finally after pondering a lot,I changed my program and used a for loop
to be able to solve the problem.
You can see this loop in the second program which is below.
But it was not what i wanted to do.
I wanted to use strcat(); function and I hope someone can help me
to correct the first program.

Thanks in advance.
Best Regards,
Zest.

The imcomplete program:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 40
int main(int argc,char * argv[])
{
   FILE *in,*out;
   char name[SIZE],chr = '.',bk[] = "Bak",ch;
   char *occurance;


   if(argc != 2)
   {
      puts("This program makes a BackUp of your files");
      fprintf(stderr,"Usage: %s filename\n",argv[0]);
      exit(EXIT_FAILURE);
      }
      if((in = fopen(argv[1],"r")) == NULL)
      {
         fprintf(stderr,"I can't open the file %s",argv[1]);
         exit(EXIT_FAILURE);
         }


         strncpy(name,argv[1],SIZE-5);
         name[SIZE-5] = '\0';

         occurance = strchr(name,chr);

         strcat(occurance , bk);

         if((out = fopen(name, "w")) == NULL)
         {
            fprintf(stderr,"I can't copy to the file %s",name);
            exit(EXIT_FAILURE);
            }

            //Copying Data

            while((ch = getc(in))!= EOF)
            putc(ch,out);
            
            if(fclose(in) != 0 || fclose(out) != 0)
            fprintf(stderr,"Error is closing files.\n");

            puts("Done!\n");



            getch();
            return 0;

            }



The program with for loop.
Code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 40
int main(int argc, char* argv[])
{   
   FILE *in,*out;
   char name[SIZE],chr = '.',bk[] = ".Bak",ch;
   char *occurance;


   if(argc != 2)
   {
      puts("This program makes a BackUp of your files");
      fprintf(stderr,"Usage: %s filename\n",argv[0]);
      exit(EXIT_FAILURE);
      }
      if((in = fopen(argv[1],"r")) == NULL)
      {
         fprintf(stderr,"I can't open the file %s",argv[1]);
         exit(EXIT_FAILURE);
         }


         strncpy(name,argv[1],SIZE-5);
         name[SIZE-5] = '\0';

         occurance = strchr(name,chr);
         
         for(int i = 0; i < 4; i++)
            *(occurance + i) = bk[i];

         

         if((out = fopen(name, "w")) == NULL)
         {
            fprintf(stderr,"I can't copy to the file %s",name);
            exit(EXIT_FAILURE);
            }

            //Copying Data
            
            while((ch = getc(in))!= EOF)
            putc(ch,out);

            if(fclose(in) != 0 || fclose(out) != 0)
            fprintf(stderr,"Error is closing files.\n");

            puts("Done!\n");



            getch();
            return 0;

            }
Back to top
View user's profile Send private message
Knight
Regular
Regular


Joined: 21 Aug 2005
Posts: 122

PostPosted: Mon Aug 28, 2006 7:36 am    Post subject: Reply with quote

At first instead of 'SIZE' you should use 'MAX_PATH' (you should include Windows.h for it, or well define it urself as 260 iirc).
Another important thing in this case is that from your code it seems that you rather need strcpy than strcat. Look at this:
If you want to replace file extension it goes like this: some_file.exe -> some_file.bak. I think this is what you want to do, but this should be done using strcpy. You would only need to replace strcat in your first code example.
Other way could be to concatenate extension: some_file.exe -> some_file.exe.bak. And here you still could use strcpy (you would need to get filename length and copy new extension there), but in this case it would be really much simplier to use strcat (if i'm correct ur first code example does like that, just uses some not needed code for such task, you can dirrectly use strcat, it finds string end on it's own and appends there new string, so no need to search for '.').

Regards,
Knight
Back to top
View user's profile Send private message
dila
Occasional Poster
Occasional Poster


Joined: 13 Jul 2005
Posts: 44
Location: England

PostPosted: Wed Aug 30, 2006 1:32 pm    Post subject: Reply with quote

as it turns out ".bak" is four bytes, so you can append the extension to the filename string by writing a single int.
personally i would use the operating systems native file copy api, rather than parsing each char.
Back to top
View user's profile Send private message
Zest
Frequent poster
Frequent poster


Joined: 24 Apr 2005
Posts: 56

PostPosted: Sat Sep 02, 2006 2:03 pm    Post subject: Reply with quote

Dear Knight,
Hi,
In fact,you revealed what I wanted to know.
Using strcpy instead of strcat was the solution.

The only question is about using 'MAX_PATH'
I need to know why you used this.
In fact,I want to know the privilege of using 'MAX_PATH' upon defining a constat value which is named SIZE and assigned with the value of 40.

Thanks in advance.
Regards,
Zest.
Back to top
View user's profile Send private message
detten
Site Admin


Joined: 05 Feb 2005
Posts: 317

PostPosted: Sat Sep 02, 2006 4:01 pm    Post subject: Reply with quote

Because MAX_PATH has the correct size of the maximum a filename string can be in the windows OS. (Its a windows define)
In your example a filename that exceeds 40 chars, is truncated, so the backup filename isn't the full filename.

_________________
Ignorance is bliss, knowledge is power
Back to top
View user's profile Send private message Visit poster's website
Zest
Frequent poster
Frequent poster


Joined: 24 Apr 2005
Posts: 56

PostPosted: Sun Sep 03, 2006 7:04 am    Post subject: Reply with quote

Dear Detten,
Thanks for your reply.
I want to know about other limitations like this, which are introduced in windows.h

In fact,the limitations which are for windows.
Please reccomend me a refrence to leaf through and get some information about such rules which are specialized for programming in windows.

Even a book which discusses such topics wouild be appreciable.

Best Regards,
Zest.
Back to top
View user's profile Send private message
Knight
Regular
Regular


Joined: 21 Aug 2005
Posts: 122

PostPosted: Sat Sep 16, 2006 9:49 am    Post subject: Reply with quote

Probably the best way to find such limitations is MSDN. I haven't heard of any book/article/doc/etc. discussing such topic.
Simply when u're using some of the windows resources (files/registry/mutexes/etc.) or anything else that could possibly have some limitations, check MSDN (or any other documentation) to see them.

Regards,
Knight
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    www.reversing.be Forum Index -> Coding Corner All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
 Copyright © 2018 BiW Reversing
 All trademarks and copyrights on this page are owned by their respective owners.
Powered By Geeklog 
Created this page in 0.09 seconds