G
Gregc.
Hi
I am writing a conditional statement, using strcat. What I am trying
to do is to add the file extenstion '.txt' if it doesn't already exist.
What I am having trouble with is returning a value if one already
exists. Attached is the code below:
#include <stdio.h>
#include <string.h>
const int MAX_FILENAME = 256;
oid checkFilename (char *filename, char *extension)
{
int length;
int check;
length = strlen(filename);
check=strcmp(filename,extension);
if(check==0)
return;
else
strcat(filename,extension);
return;
int main () {
char filename1 [MAX_FILENAME] = "testfile";
char filename2 [MAX_FILENAME] = "testfile.txt";
char filename3 [MAX_FILENAME] = "testfile.dat";
char extension [] = ".txt";
checkFilename (filename1, extension);
checkFilename (filename2, extension);
checkFilename (filename3, extension);
printf("1: %s\n", filename1);
printf("2: %s\n", filename2);
printf("3: %s\n", filename3);
}
Could someone point me in the right direction.
Greg
I am writing a conditional statement, using strcat. What I am trying
to do is to add the file extenstion '.txt' if it doesn't already exist.
What I am having trouble with is returning a value if one already
exists. Attached is the code below:
#include <stdio.h>
#include <string.h>
const int MAX_FILENAME = 256;
oid checkFilename (char *filename, char *extension)
{
int length;
int check;
length = strlen(filename);
check=strcmp(filename,extension);
if(check==0)
return;
else
strcat(filename,extension);
return;
int main () {
char filename1 [MAX_FILENAME] = "testfile";
char filename2 [MAX_FILENAME] = "testfile.txt";
char filename3 [MAX_FILENAME] = "testfile.dat";
char extension [] = ".txt";
checkFilename (filename1, extension);
checkFilename (filename2, extension);
checkFilename (filename3, extension);
printf("1: %s\n", filename1);
printf("2: %s\n", filename2);
printf("3: %s\n", filename3);
}
Could someone point me in the right direction.
Greg