从函数返回char *时出现问题..需要帮助... [英] Having problems returning a char* from a function..need help...

查看:51
本文介绍了从函数返回char *时出现问题..需要帮助...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


通过在新闻组中进行google serach,我发现一个

字符串不能从函数返回,但使用char *我应该

能够做到这一点。我花了一天的大部分时间试图把这个工作拿到工作上,但却无法解决我的错误。函数应该是什么
return是用于创建日志文件的文件名。看起来像

类似


JN122345.log


JN - 月

12 - 日期

23 - 小时

45 - 最后


任何人都可以提供任何帮助。谢谢!


马克

---------源代码------------


#define MAX_TIME_LEN 6

char cMonths [12] [2] = {" JN"," FB"," MR", AP,MY,JU,

JL,AG,SP,OC,NV, DC" };

main(argc,argv)

int argc;

char * argv [];

{

char fileName [80];


* sAutoBITELogFileName = getAutoBITELogFileName();


//好像似乎只在字符上打印。

_settextposition(1,1);

printf(" lfn:''%s''",sAutoBITELogFileName);


}


char * getAutoBITELogFileName()

{

/ * ----- -----------------------------------

%d日(01-31 )

%H小时24小时制(00-23)

%M分钟(00-59)

----- ------------------------------------- * /


time_t currentTime;

struct tm * daynow;

int iResult;

char cTemp [MAX_TIME_LEN + 1];

char * fileName;


/ * sizeof * fileName总是1,无论如何乘以它* /

fileName = malloc(13 * sizeof(* fileNa me));


if(fileName == NULL){

返回NULL; //分配内存时出现问题

}


currentTime = time(NULL); //获取当前算术日历

时间


//将当前算术日历时间转换为当地时间

in a

//类型为tm的结构。

daynow = localtime(& currentTime);


strncpy(fileName,cMonths [ daynow-> tm_mon],2);


iResult = strftime(cTemp,MAX_TIME_LEN + 1,"%d%H%M",daynow);


//检查是否有错误

if(iResult == 0){

返回NULL;

}


strncat(fileName,cTemp,6);

strncat(fileName," .log",4);


//一般打印垃圾,但有时可以看到文件名。

_settextposition(25,1);

printf(" fn: %s",& fileName);


return(char *)fileName;

}

Hello all,

From doing a google serach in the newsgroups I found out that a
string can''t be returned from a function, but using a char* I should
be able to do it. I have spent most of the day trying to get this to
work, but been unable to solve my mistake. What the function should
return is a file name used for creating logs files. It will look
something like

JN122345.log

JN - Month
12 - Date
23 - Hour
45 - Min

Can anyone provide any assistance, please. Thanks!.

Mark
--------- source code ------------

#define MAX_TIME_LEN 6

char cMonths[12][2] = { "JN", "FB", "MR", "AP", "MY", "JU",
"JL", "AG", "SP", "OC", "NV", "DC" };
main( argc, argv )
int argc;
char *argv[];
{
char fileName[80];

*sAutoBITELogFileName = getAutoBITELogFileName( );

// Just seems to print only on character.
_settextposition( 1,1 );
printf( "lfn: ''%s''", sAutoBITELogFileName );

}

char *getAutoBITELogFileName( )
{
/*----------------------------------------
%d Day (01-31)
%H Hour 24-hour style (00-23)
%M Minute (00-59)
------------------------------------------*/

time_t currentTime;
struct tm* daynow;
int iResult;
char cTemp[ MAX_TIME_LEN + 1 ];
char *fileName;

/*sizeof *fileName is always 1, multiply by it anyway*/
fileName = malloc( 13 * sizeof(*fileName) );

if ( fileName == NULL ) {
return NULL; // Problem allocating memory
}

currentTime = time(NULL); // get the current arithmetic calendar
time

// Convert the current arithmetic calendar time into local time held
in a
// structure of type tm.
daynow = localtime(&currentTime);

strncpy( fileName, cMonths[daynow->tm_mon], 2 );

iResult = strftime(cTemp, MAX_TIME_LEN + 1, "%d%H%M", daynow);

// Check to see if there was an error
if ( iResult == 0 ) {
return NULL;
}

strncat( fileName, cTemp, 6 );
strncat( fileName, ".log", 4 );

// Generally prints garbage, but some times can see the filename.
_settextposition( 25, 1 );
printf( "fn: %s", &fileName );

return (char *)fileName;
}

推荐答案

LongBow< pi *** @ mud.pool>写道:
LongBow <pi***@mud.pool> wrote:
大家好,

通过在新闻组中进行谷歌搜索我发现
字符串不能从一个函数,但使用char *我应该能够做到。我花了大部分时间试图让这个工作,但是无法解决我的错误。该函数应该返回的是用于创建日志文件的文件名。它看起来像

JN122345.log

JN - 月份
12 - 日期
23 - 小时
45 - Min

请有人提供任何帮助。谢谢!

Mark

---------源代码------------
/ *你未能包括:* /

#include< stdio.h>

#include< stdlib.h>

#include< ; string.h>

#include< time.h>

#define MAX_TIME_LEN 6

char cMonths [12] [2] = {JN,FB,MR,AP,MY,JU,
JL,AG,SP, OC,NV,DC和DC。 };


/ *您未能提供原型:* /

char * getAutoBITELogFileName();

main(argc ,argv)
int argc;
char * argv [];
int main(void)/ *你不使用argc和argv * /

{
char fileName [80];
/ *您声明一个字符数组而不使用它。 * /
* sAutoBITELogFileName = getAutoBITELogFileName();
/ *您未能声明sAutoBITELogFileName。 * /

//似乎只在字符上打印。
_settextposition(1,1);
/ *非标准。不知道。 * /

printf(" lfn:''%s''",sAutoBITELogFileName);

/ *您无法从函数返回值
声明返回int:* /


返回0;}

char * getAutoBITELogFileName()
{
/ * ----------------------------------------
%d天( 01-31)
%H小时24小时制(00-23)
%M分钟(00-59)
-------------- ---------------------------- * /

time_t currentTime;
struct tm * daynow;
int iResult;
char cTemp [MAX_TIME_LEN + 1];
char * fileName;

/ * sizeof * fileName总是1,无论如何乘以它* /
fileName = malloc(13 * sizeof(* fileName));
幻数13.

if(fileName == NULL){
返回NULL; //分配内存时出现问题


currentTime = time(NULL); //获取当前算术日历

//将当前算术日历时间转换为当地时间
在tm类型的结构中。
daynow = localtime(& currentTime);

strncpy(fileName,cMonths [daynow-> tm_mon],2);

iResult = strftime(cTemp, MAX_TIME_LEN + 1,%d%H%M,daynow);

//检查是否有错误
if(iResult == 0){
返回NULL;


strncat(fileName,cTemp,6);
strncat(fileName," .log",4);
//通常打印垃圾,但有时可以看到文件名。
_settextposition(25,1);
非标准。不知道。

printf(" fn:%s",& fileName);
printf(" fn:%s",fileName);

return(char *)fileName;
返回fileName;

}
Hello all,

From doing a google serach in the newsgroups I found out that a
string can''t be returned from a function, but using a char* I should
be able to do it. I have spent most of the day trying to get this to
work, but been unable to solve my mistake. What the function should
return is a file name used for creating logs files. It will look
something like

JN122345.log

JN - Month
12 - Date
23 - Hour
45 - Min

Can anyone provide any assistance, please. Thanks!.

Mark
--------- source code ------------ /* You failed to include those: */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_TIME_LEN 6

char cMonths[12][2] = { "JN", "FB", "MR", "AP", "MY", "JU",
"JL", "AG", "SP", "OC", "NV", "DC" };

/* You failed to provide a prototype: */
char *getAutoBITELogFileName( );

main( argc, argv )
int argc;
char *argv[]; int main( void ) /* you don''t use argc and argv */
{
char fileName[80]; /* You declare an array of characters without using it. */
*sAutoBITELogFileName = getAutoBITELogFileName( ); /* You failed to declare sAutoBITELogFileName. */

// Just seems to print only on character.
_settextposition( 1,1 ); /* Non-standard. No idea. */
printf( "lfn: ''%s''", sAutoBITELogFileName );
/* You failed to return a value from a function
declared to returning int: */

return 0;}

char *getAutoBITELogFileName( )
{
/*----------------------------------------
%d Day (01-31)
%H Hour 24-hour style (00-23)
%M Minute (00-59)
------------------------------------------*/

time_t currentTime;
struct tm* daynow;
int iResult;
char cTemp[ MAX_TIME_LEN + 1 ];
char *fileName;

/*sizeof *fileName is always 1, multiply by it anyway*/
fileName = malloc( 13 * sizeof(*fileName) ); Magic number 13.

if ( fileName == NULL ) {
return NULL; // Problem allocating memory
}

currentTime = time(NULL); // get the current arithmetic calendar
time

// Convert the current arithmetic calendar time into local time held
in a
// structure of type tm.
daynow = localtime(&currentTime);

strncpy( fileName, cMonths[daynow->tm_mon], 2 );

iResult = strftime(cTemp, MAX_TIME_LEN + 1, "%d%H%M", daynow);

// Check to see if there was an error
if ( iResult == 0 ) {
return NULL;
}

strncat( fileName, cTemp, 6 );
strncat( fileName, ".log", 4 );

// Generally prints garbage, but some times can see the filename.
_settextposition( 25, 1 ); Non-standard. No idea.
printf( "fn: %s", &fileName ); printf( "fn: %s", fileName );

return (char *)fileName; return fileName;
}



请阅读clc的faq-list:
http://www.eskimo.com/~scs/C-faq/top.html


Irrwahn

-

在usenet上发帖就像在矿场打高尔夫球。


Please read the faq-list for c.l.c at:
http://www.eskimo.com/~scs/C-faq/top.html

Irrwahn
--
Posting in usenet is like playing golf in a mine-field.


LongBow写道:
LongBow wrote:
大家好,

通过在新闻组中进行google serach,我发现了一个
字符串不能从函数返回,但使用char *我应该能够做到。我花了大部分时间试图让这个工作,但是无法解决我的错误。该函数应该返回的是用于创建日志文件的文件名。它看起来像

JN122345.log

JN - 月份
12 - 日期
23 - 小时
45 - Min

请有人提供任何帮助。谢谢!

Mark

---------源代码------------
#define MAX_TIME_LEN 6

JL,AG,SP,OC,NV,DC等。 };


#include< stdio.h>

#include< time.h>

#include< stdlib。 h>


char * getAutoBITELogFileName();


避免隐式int。

main(argc, argv)
int argc;
char * argv [];


int main(int argc,char * argv [])

{
char fileName [80];

* sAutoBITELogFileName = getAutoBITELogFileName();

//似乎只在字符上打印。
_settextposition(1,1);


非标准。

printf(" lfn:''%s''",sAutoBITELogFileName);


printf(" lfn:''%s''\ n,sAutoBITELogFileName);


添加换行符以提高可读性输出。

}
char * getAutoBITELogFileName()
{
/ * ------------- ---------------------------
%d天(01-31)
%H小时24小时风格(00-23)
%M分钟(00-59)
----------------------------- ------------- * /

time_t currentTime;
struct tm * daynow;
int iResult;
char cTemp [MAX_TIME_LEN + 1];
char * fileName;

/ * sizeof * fileName始终为1,无论如何都乘以它* /
fileName = malloc(13 * sizeof(* fileName) );

if(fileName == NULL){
返回NULL; //分配内存时出现问题


currentTime = time(NULL); //获取当前算术日历

//将当前算术日历时间转换为当地时间
在tm类型的结构中。
daynow = localtime(& currentTime);

strncpy(fileName,cMonths [daynow-> tm_mon],2);

iResult = strftime(cTemp, MAX_TIME_LEN + 1,%d%H%M,daynow);

//检查是否有错误
if(iResult == 0){
返回NULL;


strncat(fileName,cTemp,6);
strncat(fileName," .log",4);
//通常打印垃圾,但有时可以看到文件名。
_settextposition(25,1);


非标准。

printf(" fn:%s",& fileName);


printf(" fn:%s \ n",fileName);


添加换行符,你不要想打印fileName的地址,

你要打印的内容。如果您在编译器中打开了所有警告,那么它应该已经捕获了这个和隐含的整数。

return(char *)fileName;


演员是多余的; fileName已经是一个char *。

}
Hello all,

From doing a google serach in the newsgroups I found out that a
string can''t be returned from a function, but using a char* I should
be able to do it. I have spent most of the day trying to get this to
work, but been unable to solve my mistake. What the function should
return is a file name used for creating logs files. It will look
something like

JN122345.log

JN - Month
12 - Date
23 - Hour
45 - Min

Can anyone provide any assistance, please. Thanks!.

Mark
--------- source code ------------

#define MAX_TIME_LEN 6

char cMonths[12][2] = { "JN", "FB", "MR", "AP", "MY", "JU",
"JL", "AG", "SP", "OC", "NV", "DC" };
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

char *getAutoBITELogFileName( );

To avoid the implicit int.
main( argc, argv )
int argc;
char *argv[];
int main(int argc, char *argv[])
{
char fileName[80];

*sAutoBITELogFileName = getAutoBITELogFileName( );

// Just seems to print only on character.
_settextposition( 1,1 );
Non-standard.
printf( "lfn: ''%s''", sAutoBITELogFileName );
printf( "lfn: ''%s''\n", sAutoBITELogFileName );

Add a newline to improve readability of the output.

}

char *getAutoBITELogFileName( )
{
/*----------------------------------------
%d Day (01-31)
%H Hour 24-hour style (00-23)
%M Minute (00-59)
------------------------------------------*/

time_t currentTime;
struct tm* daynow;
int iResult;
char cTemp[ MAX_TIME_LEN + 1 ];
char *fileName;

/*sizeof *fileName is always 1, multiply by it anyway*/
fileName = malloc( 13 * sizeof(*fileName) );

if ( fileName == NULL ) {
return NULL; // Problem allocating memory
}

currentTime = time(NULL); // get the current arithmetic calendar
time

// Convert the current arithmetic calendar time into local time held
in a
// structure of type tm.
daynow = localtime(&currentTime);

strncpy( fileName, cMonths[daynow->tm_mon], 2 );

iResult = strftime(cTemp, MAX_TIME_LEN + 1, "%d%H%M", daynow);

// Check to see if there was an error
if ( iResult == 0 ) {
return NULL;
}

strncat( fileName, cTemp, 6 );
strncat( fileName, ".log", 4 );

// Generally prints garbage, but some times can see the filename.
_settextposition( 25, 1 );
Non-standard.
printf( "fn: %s", &fileName );
printf( "fn: %s\n", fileName );

Add a newline, and you don''t want to print the address of fileName,
you want to print the contents. If you had turned all warnings on in
your compiler, it should have caught this and the implicit ints.

return (char *)fileName;
The cast is redundant; fileName is already a char *.
}






" T.M。索莫斯" < TM ** @ mail.ptd.net>写道:
"T.M. Sommers" <tm**@mail.ptd.net> wrote:
LongBow写道:
LongBow wrote:



< BIG SNIP>


<BIG SNIP>

printf(" fn:%s",& fileName);
printf( "fn: %s", &fileName );



printf(" fn:%s \ n",fileName);

添加换行符,你不想打印fileName的地址,
你想打印内容。如果你在编译器中打开了所有警告,它应该已经捕获了这个和隐含的int。



printf( "fn: %s\n", fileName );

Add a newline, and you don''t want to print the address of fileName,
you want to print the contents. If you had turned all warnings on in
your compiler, it should have caught this and the implicit ints.



如果他甚至试图编译它,编译器会有提出了

a罚款(以及很长)的错误和警告列表,指出什么是错误的




8 ^)


Irrwahn


-

不写:void main(...)

不转换malloc的返回值()

不要fflush(stdin)

读取clc-faq: http://www.eskimo.com/~scs/C-faq/top.html


这篇关于从函数返回char *时出现问题..需要帮助...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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