![]() |
|
#1
|
|||
|
|||
|
How to use strcat in c?
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;
}
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;
}
|
|
#2
|
|||
|
|||
|
Now why would you want to use strcat?? This routine you have now is quite slow reading on a character base is too slow. Use fread and fwrite.
strcat useage is as follows: ... char dest[20]; dest[0]='\'; strcat(dest,"this is a "); //now dest="this is a\0" strcat(dest,"test"); //now dest="this is a test\0" strcat(dest,"123456789012345678901234567890"); //now you get a crash because your dest string overflows, so this is a buffer overflow. Easier loop: char buffer[1024]; ... while !feof(in){ nrbytes=fread(buffer,1,1024,in); if (nrbytes) fwrite(buffer,1,nrbytes,out); } etc.. Backupping files can be done faster using windows functions if yer running windoze. |
|
#3
|
|||
|
|||
|
Just some small remarks regarding your program.
It seems that it can not handle well an extension of 1 or 2 characters or more than 3 characters. You should start from the back of the file name (argv[1]) and search for the last point '.' character. And BTW, you don't need to use strcat in case you have at least 3 characters for extension. Just find the last '.' in the name and replace the next 4 characters with "BAK" and '\0'. The other remarks from piccolo also remain. |
|
#4
|
|||
|
|||
|
Don't know if it's useful for you, but you could use the API PathRenameExtension , which is in shlwapi.dll, this API replace any extension with any extension you want:
PathRenameExtension ("file.ext", ".bak"); Hope it works for you, just #include <shlwapi.h> This only works in windows, off course... |
|
#5
|
|||
|
|||
|
Here is a generalized routine good for your situation
and can be easily tailored or adapted to fit most any situation: // EX: OPEN A FILE FOR LOGGING OUTPUT IN ANALYZE MODE. // Find the last '\\' to obtain a pointer to just the base file name part if your buffer contains any path type info // We could just as eaily searched for last '.' to obtain base file name extension pointer. Code:
char *szBuffer = buffer;
PCSTR pszBaseName = strrchr( szBuffer, '\\' );
if ( pszBaseName ) // We found a '\\', so advance to the base FILE name
{
// Increment 1 byte past our pointer
pszBaseName++;
strncpy(pszBaseName, "Asprlog.txt\0", 12); // we need 12 to include '\0'
null char
//replace base file name with newname , here you could have appended
bak or BAK extension if you had searched on the '.' char Ex: as so:
strncpy(pszBaseName, "bak\0", 4);
}
FILE * pFile;
pFile=fopen(szBuffer,"wt"); // open for write
cheers! |
|
#6
|
|||
|
|||
|
maybe you could try the following routine to search for the '.' from the end of the filename:
Code:
char Filename[]= "c:\\path\\file.ext";
char *BaseFileName;
strrev(Filename);
BaseFileName = strstr(Filename,"."); // search for the '.'
if (BaseFileName){
BaseFileName = (char*)(BaseFileName+1); // exclude the '.'
strcpy(Filename,BaseFileName);
}
strrev(Filename);
strcat(Filename,".bak\0");
well of course there are still a bunch of ways to implement this. anyways, hope this helps... |
|
#7
|
|||
|
|||
|
Quote:
fnsplit() , fnmerge() i think its in dir.h splitpath() , makepath() defined in stdlib.h these all take care to find . on thier own all you need to give is buffers for specific paths and then just switch what you dont with what you need and compose back |
|
#8
|
|||
|
|||
|
Dear condzero,
Hi, Thanks for your help and reply. But there is still a problem for me that I couldn't manage it. Would you please let me know how to OPEN A FILE FOR LOGGING OUTPUT IN ANALYZE MODE? Which function(s) do you use to OPEN A FILE FOR LOGGING OUTPUT IN ANALYZE MODE? That would be great if you can discuss the case a bit. I look forward to hearing from you. Also thank anybody else who has spend time to answer to this topic to bring some help to the point. Regards, Zest. |
|
#9
|
|||
|
|||
|
Hi Zest,
Not sure what you mean by "Analyze Mode". In my example, I open a file with the parameter "wt" (Write, text mode). Then I simply create my output: Code:
char b[1024];
unsigned int o = 0; //Loop counter
FILE * pFile;
pFile=fopen("myfile.txt","wt"); // open for write text mode
// Use a loop and print your output, or just keep appending data to a buffer
then print the entire buffer (Note: Use of new line char n)
LOOP:
sprintf( b, "Some text\n")
fputs (b,pFile); // or possibly fwrite (b , 1 , 80 , pFile); (where 80 is the
length of your text, not sure which way is more applicable as I generally
use the code below)
...
//or
for ( o = 0; o < strlen(b); o++ )
{
putc(b[o], pFile);
}
// CLOSE THE FILE.
fclose (pFile);
Perhaps others can elaborate more. cheers. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|