On Wed, Jul 13, 2011 at 11:05:34PM +0800, 温德鑫 wrote:
> HI,all:
> I wrote a file like this: test.c
> #include <stdio.h>
> #include <stdlib.h>
> #include<locale.h>
> int main()
> {
> FILE *fpipe;
> char *command="cd /data/cdc/document/;/usr/bin/svn status 73/2788
> --config-dir /home/svnroot/.subversion 2>&1";
> char line[256];
>
> if ( !(fpipe = (FILE*)popen(command,"r")) )
> { // If fpipe is NULL
> perror("Problems with pipe");
> exit(1);
> }
>
> while ( fgets( line, sizeof line, fpipe))
> {
> //printf("%s", line);
> puts(line);
> }
> pclose(fpipe);
> }
>
> and I compiled it, run on the OpenSUSE
> gcc -o test test.c
> ./test
> This way works fine, There is no errors output, but when I use crontab to
> run it,
> * * * * * /root/test >> /var/log/test.log 2>&1
>
> The error comes out:
> tailf -f /var/log/test.log
>
>
> svn: Error converting entry in directory '73/2788' to UTF-8
>
> svn: Can't convert string from native encoding to 'UTF-8':
>
> svn: ?\228?\189?\160?\229?\165?\189.txt
>
>
> The directory contains a file using chinese character,I have searched
> online,most articles said using " export LC_ALL='en_US.utf-8' " to solve
> this problem,I try to add this code to the file
>
> #include <stdio.h>
> #include <stdlib.h>
> #include<locale.h>
> int main()
> {
> FILE *fpipe;
> char *command="cd /data/cdc/document/;/usr/bin/svn status 73/2788
> --config-dir /home/svnroot/.subversion 2>&1";
> char line[256];
> char *b = setlocale(LC_ALL, "en_US.utf-8");
> puts(b);
> b = setlocale(LC_ALL, NULL);
> puts(b);
>
> if ( !(fpipe = (FILE*)popen(command,"r")) )
> { // If fpipe is NULL
> perror("Problems with pipe");
> exit(1);
> }
>
> while ( fgets( line, sizeof line, fpipe))
> {
> //printf("%s", line);
> puts(line);
> }
> pclose(fpipe);
> }
>
> I compile it again, run by the crontab, but it does not work, the error
> still exists. How can I solve this problem?
Running setlocale() in your program is not the same as exporting
an environment variable. The call to setlocale() in your own program
will not affect the locale of the program you are spawning via popen().
Try setting the environment variable in your crontab instead.
Put this in your crontab:
LC_ALL=en_US.UTF-8
* * * * * /root/test >> /var/log/test.log 2>&1
I think that should work. Most cron programs support setting environment
variables like this.
Received on 2011-07-13 22:56:06 CEST