473,748 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(fi lename,extensio n);

if(check==0)
return;
else
strcat(filename ,extension);

return;
int main () {
char filename1 [MAX_FILENAME] = "testfile";
char filename2 [MAX_FILENAME] = "testfile.t xt";
char filename3 [MAX_FILENAME] = "testfile.d at";
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 2018
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(fi lename,extensio n);

if(check==0)
return;
else
strcat(filename ,extension);

return;
int main () {
char filename1 [MAX_FILENAME] = "testfile";
char filename2 [MAX_FILENAME] = "testfile.t xt";
char filename3 [MAX_FILENAME] = "testfile.d at";
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(file name,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.t xt";
char filename3[MAX_FILENAME] = "testfile.d at";

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(file name,extension) ) Consider the situation "BlatxtBla. ext"
{
pToString = strchr(filename ,'.'); Consider the situation "Bla.Bla.ex t"

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.t xt";
char filename3[MAX_FILENAME] = "testfile.d at";

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.c om, 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/googlegroupsrep ly/>

Apr 26 '06 #4

"Abdo Haji-Ali" <ah***@inframez .com> wrote in message
news:11******** *************@t 31g2000cwb.goog legroups.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(file name,extension) )

Consider the situation "BlatxtBla. ext"
{
pToString = strchr(filename ,'.');

Consider the situation "Bla.Bla.ex t"

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.t xt";
char filename3[MAX_FILENAME] = "testfile.d at";

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********@yah oo.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_Keit h) 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.goo glegroups.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.or g> wrote:
CBFalconer <cb********@yah oo.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

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

Similar topics

15
2275
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 loop, how can you test two separate conditions against the same $element of an array. For example, this is how I thought it would be done (similar to Perl), but it did not work:
8
2285
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 from being a "language internist" (on Python or anything else) so go easy on me if this is stupid - it just seemed quite elegant to me as a relative newbie in town :-) I also havent got a clue whether this would be easy or even possible
10
9586
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 'case' code snippet does not. (the code's function is illustrative.) ////////////////////////////////////////// //////// working 'if/else' switch //////// //////////////////////////////////////////
3
5933
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 terminated by pressing ++ and then terminate the process. I searched the entire internet and found out that there could be two things wrong (both of them are mentioned in the bug list on the access
1
16559
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
9889
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 someone helps me... Urgent - Please reply soon ! Thanks, Raghu
10
3241
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 complexity analyzer tool supposedly does not pick it up. Is it really more efficient? Personally I find this coding style extremely cryptic, misleading and error-prone. I believe that I have removed all traces of proprietary-ness from this coding...
10
3094
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 have blocks of code in different source files that were conditionally compiled based on that constant. Now that C# has done away with include files, is there any way of doing the same thing, short of defining the constant multiple times at the head...
5
1971
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. conditional structure verifies a short type and fires the fun_n in its block.
43
7584
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. How is it possible? Please help me. Thanks in advance.
1
9326
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8245
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.