Conditional Statements

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
 
Z

Zero

Gregc. said:
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

One solution, which works but has to be checked clearly :)

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

#define MAX_FILENAME 256

void checkFilename (char *filename, char *extension)
{
int length;
int check;
char * pToString;

if(!strstr(filename,extension))
{
pToString = strchr(filename,'.');

if (pToString)
{
pToString[1] = 't';
pToString[2] = 'x';
pToString[3] = 't';
pToString[4] = '\0';
}
else
{
strcat(filename,extension);
}
}
}

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);

getch();
}
 
C

CBFalconer

Gregc. said:
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:

Look up strrchr() function.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
A

Abdo Haji-Ali

Zero said:
One solution, which works but has to be checked clearly :)

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

#define MAX_FILENAME 256

void checkFilename (char *filename, char *extension)
{
int length;
int check;
char * pToString;

if(!strstr(filename,extension))
Consider the situation "BlatxtBla.ext"
{
pToString = strchr(filename,'.');
Consider the situation "Bla.Bla.ext"
if (pToString)
{
pToString[1] = 't';
pToString[2] = 'x';
pToString[3] = 't';
pToString[4] = '\0';
}
else
{
strcat(filename,extension);
Consider the situation where the buffer is not big enough to hold the
extenstion
}
}
}

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);

getch();
Where's the return statement?

Abdo Haji-Ali
Programmer
In|Framez
 
F

Fred Kleinschmidt

Abdo Haji-Ali said:
One solution, which works but has to be checked clearly :)

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

#define MAX_FILENAME 256

void checkFilename (char *filename, char *extension, int maxlen)
{
int length;
int check;
char * pToString;

if(!strstr(filename,extension))
Consider the situation "BlatxtBla.ext"
{
pToString = strchr(filename,'.');
Consider the situation "Bla.Bla.ext"
if (pToString)
{
pToString[1] = 't';
pToString[2] = 'x';
pToString[3] = 't';
pToString[4] = '\0';
}
else
{
strcat(filename,extension);
Consider the situation where the buffer is not big enough to hold the
extenstion
}
}
}

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);

getch();
Where's the return statement?

Abdo Haji-Ali
Programmer
In|Framez

checkFileName( filename, extension, MAX_FILENAME );

int checkFilename (char *filename, char *extension, int maxlen) {
size_t nchf, nche;
char *p = NULL;
if ( filename!=NULL && extension != NULL ) {
nche = strlen( extension);
nchf = strlen( filename );
if ( nchf > nche ) {
p = &filename[nchf-nche];
}
else {
p = filename;
}
if ( strcmp( p, extension ) != 0 ) {
if ( nchf+nche < maxlen ) {
strcpy( p, extension );
return 0; /* success */
}
}
}
return 1; /* failed to add extension */
}
 
A

Abdo Haji-Ali

Fred said:
int checkFilename (char *filename, char *extension, int maxlen) {
size_t nchf, nche;
char *p = NULL;
if ( filename!=NULL && extension != NULL ) {
nche = strlen( extension);
nchf = strlen( filename );
if ( nchf > nche ) {
p = &filename[nchf-nche];
}
else {
p = filename;
}
if ( strcmp( p, extension ) != 0 ) {
if ( nchf+nche < maxlen ) {
strcpy( p, extension );
return 0; /* success */
}
}
}
return 1; /* failed to add extension */
}

This function doesn't append the extention when necessary, it replaces
the last 'nche' (length of extenstion) characters of the file name with
the extension... Consider the situation when filename is "Blah" and the
extenstion is ".txt" the returned file name would be just ".txt";
which, I believe, isn't what the OP wanted.

Abdo Haji-Ali
Programmer
In|Framez
 
F

Fred Kleinschmidt

Abdo Haji-Ali said:
Fred said:
int checkFilename (char *filename, char *extension, int maxlen) {
size_t nchf, nche;
char *p = NULL;
if ( filename!=NULL && extension != NULL ) {
nche = strlen( extension);
nchf = strlen( filename );
if ( nchf > nche ) {
p = &filename[nchf-nche];
}
else {
p = filename;
}
if ( strcmp( p, extension ) != 0 ) {
if ( nchf+nche < maxlen ) {
strcpy( p, extension );
return 0; /* success */
}
}
}
return 1; /* failed to add extension */
}

This function doesn't append the extention when necessary, it replaces
the last 'nche' (length of extenstion) characters of the file name with
the extension... Consider the situation when filename is "Blah" and the
extenstion is ".txt" the returned file name would be just ".txt";
which, I believe, isn't what the OP wanted.

Oops - it should be
strcpy( filename, extension );
 
K

Keith Thompson

CBFalconer said:
Look up strrchr() function.

I'm not sure that strrchr() is going to be useful. It searches for a
single character; the OP wants to determine whether the name ends in a
specified string.

An outline of a solution:

If the name is less than 4 characters long, it doesn't end in
".txt".

Get a pointer to the fourth-to-last character of the string. Use
strcmp() to compare the (sub)string pointed to by that pointer to
".txt".

This is easily generalizable to arbitrary suffixes.

And if you append the ".txt", make sure you've allocated enough memory
to hold it.
 
A

Abdo Haji-Ali

Fred said:
Oops - it should be
strcpy( filename, extension );
Which is even worse, it replaces the filename with the extension... I
think you meant:
strcat( p, extension );

Abdo Haji-Ali
Programmer
In|Framez
 
M

Mark McIntyre

I'm not sure that strrchr() is going to be useful. It searches for a
single character; the OP wants to determine whether the name ends in a
specified string.

starting with a dot. He can search for dots, and check that the
remaining part of the string a) has no more dots in it and b) is the
right pattern.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
K

Keith Thompson

Mark McIntyre said:
starting with a dot. He can search for dots, and check that the
remaining part of the string a) has no more dots in it and b) is the
right pattern.

That's useful if you're asking what extension a file name has. In
this case, the OP wanted to know specifically whether the file name
ends in ".txt"; in that case, there's nothing special about the '.'
character.

Yes, you could use the approach you suggest *in this case*, but it
doesn't generalize well to the problem of determining whether a string
ends in a specified substring. Consider determining whether a file
name ends in ".tar.gz"; if the file name is "foo.tar.gz", searching
for the last '.' misses the ".tar".

If you're always going to be looking for a suffix consisting of a '.'
followed by one or more additional characters that will never include
another '.', strrchr() might be useful. But personally, I'd rather
not hardwire that assumption into my code unless it makes things
significantly easier.
 
A

August Karlstrom

Gregc. said:
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.

I would do

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

#define FILENAME_LENGTH_MAX 256

/* Return a pointer to the start of the file extension (including the
dot) or a pointer to the string terminator if no extension is
found. */

char *extension(const char *filename)
{
char *result, *endp;

endp = filename + strlen(filename);
result = endp;
while ((result > filename) && (*result != '.'))
result--;
return (result == filename)? endp: result;
}


/* test */

int main(void)
{
char filename[FILENAME_LENGTH_MAX] = "testfile";
char *ext = ".txt";

if (strcmp(extension(filename), ext) != 0)
strcat(filename, ext);
puts(filename);
return 0;
}


August
 
W

Walter Roberson

Gregc. said:
What I am trying
to do is to add the file extenstion '.txt' if it doesn't already exist.

What is the result to be if the file name *is* '.txt' ?
 
G

Gregc.

Keith said:
Don't you mean it will return ".txt"?

Yep, your correct. It's suppose to add .txt if one doesn't exist, and
it .txt already exist then there is no change.
 
W

Walter Roberson

Gregc. said:
Yep, your correct. It's suppose to add .txt if one doesn't exist, and
it .txt already exist then there is no change.

In the original problem statement, you phrased it in terms of
adding the file extension '.txt' if it was not already there.

For the file which is named just '.txt' with nothing else,
the base file name for which '.txt' would be the extension,
would be the empty filename. It is arguable that you have to
have a non-empty filename in order for '.txt' to be an
extension of that filename.

Not that it really matters to me whether '.txt' should be
left as is or changed to '.txt.txt'. I asked only because
I happened to notice some unusual logic in one of the suggested
implementations; when I traced the logic I realized it was
testing for this case, at which point I realized you had not
definitively indicated what should happen for it.
 
R

Rod Pemberton

Gregc. said:
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.

Yes, I think the following is closer to what you want.


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

typedef unsigned int oid;
const int MAX_FILENAME = 256;

oid checkFilename (char *filename, char *extension)
{
int check=0;

if(strchr(filename,'.')==NULL) /* no period, so no extension */
{
strcat(filename,extension);
check=1;
}
else /* possible extension */
{
if(!strcmp(strchr(filename,'.'),&extension[1]))
{ /* no matching extension */
strcat(filename,extension);
check=1;
}
}

if(check==0)
return(1);

return(0);
}

int main(void)
{
char filename1[MAX_FILENAME];
char filename2[MAX_FILENAME];
char filename3[MAX_FILENAME];
char extension[] = ".txt";
unsigned int error;

strcpy(filename1,"testfile");
strcpy(filename2,"testfile.txt");
strcpy(filename3,"testfile.dat");

error=checkFilename (filename1, extension);
printf("1:%d| %s\n",error, filename1);

error=checkFilename (filename2, extension);
printf("2:%d| %s\n",error, filename2);

error=checkFilename (filename3, extension);
printf("3:%d| %s\n",error,filename3);
}


Rod Pemberton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top