473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Conditional Statements

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

Apr 26 '06 #1
17 1991
Gregc. schrieb:
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();
}

Apr 26 '06 #2

Zero wrote:
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

Apr 26 '06 #3
"Gregc." wrote:

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/>

Apr 26 '06 #4

"Abdo Haji-Ali" <ah***@inframez.com> wrote in message
news:11*********************@t31g2000cwb.googlegro ups.com...

Zero wrote:
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 */
}
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


Apr 26 '06 #5

Fred Kleinschmidt wrote:
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

Apr 26 '06 #6
CBFalconer <cb********@yahoo.com> writes:
"Gregc." wrote:

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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 26 '06 #7

"Abdo Haji-Ali" <ah***@inframez.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...

Fred Kleinschmidt wrote:
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 );

Abdo Haji-Ali
Programmer
In|Framez

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Apr 26 '06 #8

Fred Kleinschmidt wrote:
"Abdo Haji-Ali" <ah***@inframez.com> wrote in message
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 );

Which is even worse, it replaces the filename with the extension... I
think you meant:
strcat( p, extension );

Abdo Haji-Ali
Programmer
In|Framez

Apr 26 '06 #9
On Wed, 26 Apr 2006 18:04:55 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
CBFalconer <cb********@yahoo.com> writes:

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.


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
Apr 26 '06 #10
Mark McIntyre <ma**********@spamcop.net> writes:
On Wed, 26 Apr 2006 18:04:55 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
CBFalconer <cb********@yahoo.com> writes:

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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 27 '06 #11
Gregc. wrote:
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
Apr 27 '06 #12
In article <11**********************@y43g2000cwc.googlegroups .com>,
Gregc. <gr*********@bigpond.com> wrote:
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' ?
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Apr 27 '06 #13
Walter Roberson wrote:
What is the result to be if the file name *is* '.txt' ?
--

Then it will return testfile.txt ie no change.

Apr 27 '06 #14
"Gregc." <gr*********@bigpond.com> writes:
Walter Roberson wrote:
What is the result to be if the file name *is* '.txt' ?


Then it will return testfile.txt ie no change.


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 27 '06 #15

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

Apr 27 '06 #16
In article <11**********************@e56g2000cwe.googlegroups .com>,
Gregc. <gr*********@bigpond.com> wrote:
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.

--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Apr 27 '06 #17

"Gregc." <gr*********@bigpond.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
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
Apr 28 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Max | last post by:
Hi, I'm a perl programmer and am trying to learn PHP. So far I have figured out most of the differences, but have not been able to find out how to do the following: When running through a...
8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
1
by: chris han | last post by:
Hi, all, I'm trying to use Conditional Compilation Statements in my code. using System; #define DEBUG public class MyClass { public static void Main() {
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
10
by: Dave | last post by:
I'm a C++ programmer of many years, trying to get my feet wet in C#. I have a question about conditional compilation. In C++, I would sometimes define a constant in an include file, and then...
5
by: Gary Wessle | last post by:
Hi I have a group of functions which have the same signature. void fun_n(void); according to a conditional structure "be it if-else or switch-case" I get to choose which one to run. ...
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.