Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have the following statement:-
C++
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:-
C++
#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:-

C++
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?
Posted

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:
C++
#define buffer_ls 2500
...
sprintf(buffer_ls,"%-9s %5s %10s %4s %9s %18s %9s %10s %s\n",
...

which equates to
C++
sprintf(2500,"%-9s %5s %10s %4s %9s %18s %9s %10s %s\n",
...

which is never going to work.
 
Share this answer
 
Comments
Tarun22 21-Aug-12 7:00am    
thanks i realized the silly mistake....
Tarun22 11-Oct-14 9:17am    
@Richard Can i talk to u by mail
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/[^]
 
Share this answer
 
Comments
Tarun22 11-Oct-14 9:17am    
@Legor can i talk to you by mail
Richard MacCutchan 11-Oct-14 10:13am    
Sorry no, post your question in the.normal way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900