Click here to Skip to main content
11,641,708 members (54,351 online)
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 20-Aug-12 23:58pm
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 Mika Wendelius 290
1 DamithSL 223
2 Afzaal Ahmad Zeeshan 139
3 OriginalGriff 110
4 Suvendu Shekhar Giri 65
0 Mika Wendelius 460
1 OriginalGriff 215
2 DamithSL 213
3 CPallini 169
4 Afzaal Ahmad Zeeshan 129


Advertise | Privacy | Mobile
Web02 | 2.8.150731.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