Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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?
Posted 21-Aug-12 0:58am
Tarun22560
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Tarun22 at 21-Aug-12 7:00am
   
thanks i realized the silly mistake....
Tarun22 at 11-Oct-14 9:17am
   
@Richard Can i talk to u by mail
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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/[^]
  Permalink  
Comments
Tarun22 at 11-Oct-14 9:17am
   
@Legor can i talk to you by mail
Richard MacCutchan at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 434
1 Maciej Los 430
2 OriginalGriff 315
3 CHill60 240
4 Abhinav S 220
0 Sergey Alexandrovich Kryukov 10,102
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100