我应该用什么代替sprintf [英] what should i use instead of sprintf

查看:136
本文介绍了我应该用什么代替sprintf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下声明:-

 printf(" " " " " " " " " " ); 




我有以下全局声明:-

 #define buffer_ls 2500
 字符 file11 [ 200 ] = " /sdcard/NewLs.txt";
 int  WriteToLog( char  * str)
   {
__android_log_print(ANDROID_LOG_INFO," "  IN :: WriteToLog");
      FILE *日志;
      log = fopen(file11," );
      如果(日志== NULL)
    __android_log_print(ANDROID_LOG_INFO," " 无法打开文件错误%s",strerror(errno));
         返回- 1 ;
      fprintf(log," ,str);
      fclose(log);
      __android_log_print(ANDROID_LOG_INFO," "  OUT :: WriteToLog");
      返回  0 ;
   } 




我想要而不是在文件中打印我想在txt文件中打印它,所以我确实这样:-

 sprintf(buffer_ls," " " " " " " " " " );
WriteToLog(buffer_ls); 



我的代码在sprinf失败,我应该使用什么代替sprintf?

解决方案

而不是想知道要使用什么函数代替sprintf(),您应该花时间调试代码以看看为什么失败.这是您所拥有的:

 #define buffer_ls 2500
 ...
sprintf(buffer_ls," ,
... 


等于

 sprintf( 2500 " %-9s%5s%10s%4s%9s%18s%9s%10s%s \ n",
... 



要将文本写到文件中,可以使用fprintf().
函数sprintf()用于写入char数组(char *).

参见:
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/ [ ^ ]

http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/ [ ^ ]


i have the following statement:-

printf("%-9s %5s %10s %4s %9s %18s %9s %10s %s\n",
            "COMMAND",
            "PID",
            "USER",
            "FD",
            "TYPE",
            "DEVICE",
            "SIZE/OFF",
            "NODE",
            "NAME");




i have the following declaration global:-

#define buffer_ls 2500
 char file11[200]="/sdcard/NewLs.txt";
int WriteToLog(char* str)
   {
	   __android_log_print(ANDROID_LOG_INFO,"Tarun1","IN::WriteToLog");
      FILE* log;
      log = fopen(file11, "a+");
      if (log == NULL)
    	  __android_log_print(ANDROID_LOG_INFO,"Tarun1","cannot open file error %s", strerror(errno));
         return -1;
      fprintf(log, "%s\n", str);
      fclose(log);
      __android_log_print(ANDROID_LOG_INFO,"Tarun1","OUT::WriteToLog");
      return 0;
   }




i want rather than printing in file i would like to print it in txt file so i did like this:-

sprintf(buffer_ls,"%-9s %5s %10s %4s %9s %18s %9s %10s %s\n",
            "COMMAND",
            "PID",
            "USER",
            "FD",
            "TYPE",
            "DEVICE",
            "SIZE/OFF",
            "NODE",
            "NAME");
WriteToLog(buffer_ls);



My code fails at sprinf what should i use instead of sprintf?

解决方案

Instead of wondering what function to use instead of sprintf() you should spend time debugging your code to see why it failed. Here is what you have:

#define buffer_ls 2500
...
sprintf(buffer_ls,"%-9s %5s %10s %4s %9s %18s %9s %10s %s\n",
...


which equates to

sprintf(2500,"%-9s %5s %10s %4s %9s %18s %9s %10s %s\n",
...


which is never going to work.


To write text into a file you can use fprintf().
The function sprintf() is used to write into a char array (char*).

See:
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/[^]

http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/[^]


这篇关于我应该用什么代替sprintf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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