条件陈述 [英] Conditional Statements

查看:81
本文介绍了条件陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在使用strcat编写条件语句。我正在尝试

要做的是添加文件扩展名''。txt''如果它还不存在。

我遇到的问题是如果已存在

,则返回一个值。随附的代码如下:


#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(文件名,扩展名);


返回;

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(&quo t; 2:%s \ n",filename2);

printf(" 3:%s \ n",filename3);

}


有人能指出我正确的方向吗。


格雷格

解决方案
Gregc。 schrieb:



我正在编写条件语句,使用strcat。我正在尝试做的是添加文件扩展名''。txt'',如果它还没有存在的话。
我遇到的问题是返回一个值,如果一个已经
存在。随附的代码如下:

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

strcat(文件名,扩展名);

返回;
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);
}

有人能指出我正确的方向。

Greg



一个解决方案,有效,但必须清楚地检查:)


#include< stdio.h>

#include< string.h>


#define MAX_FILENAME 256

void checkFilename(char * filename,char * extension)

{

int length;

int check;

char * pToString;


if(b !strstr(文件名,扩展名))

{

pToString = strchr(filename,''。'');


if(pToString)

{

pToString [1] =''t'';

pToString [2] =''x'' ;

pToString [3] =''t'';

pToString [4] =''\ 0'';

}

else

{

strcat(文件名,扩展名);

}

}

}


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

}




Zero写道:

一个解决方案,有效,但必须清楚检查:)

#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))
考虑情况BlatxtBla.ext

{
pToString = strchr(filename,' ')。';
考虑情况Bla.Bla.ext

如果(pToString)
{
pToString [1] =''t'';
pToString [2] =''x'';
pToString [3] =''t'';
pToString [4] =''\ 0'';
}

{
strcat(文件名,扩展名);
考虑缓冲区不足以保持

扩展的情况

}
}
}

int main(){/ name> [MAX_FILENAME] =" testfile";
char filename2 [MAX_FILENAME] =" testfile.txt" ;;
char filename3 [MAX_FILENAME] =" testfile.dat";

char extension [] =" .txt" ;;

checkFilename(filename1,extension);
checkFilename (filename2,扩展名);
checkFilename(filename3,extension);

printf(" 1:%s \ n",filename1);
printf(" 2 :%s \ n",filename2);
printf(" 3:%s \ n",filename3);

getch();
哪里是退货声明?

}




Abdo Haji-Ali

程序员

In | Framez


" Gregc。写道:


我正在编写一个条件语句,使用strcat。我想要做的是添加文件扩展名''。txt'',如果它还没有存在的话。我遇到的问题是返回一个
值,如果已存在的话。附上的代码如下:




查找strrchr()函数。


-

如果你想通过groups.google.com发布一个后续内容,请不要使用

破坏的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson

更多详细信息:< http://cfaj.freeshell.org/google/>

另见< http://www.safalra .com / special / googlegroupsreply />


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

解决方案

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



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


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


这篇关于条件陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆