My C程序在FILE中存储shell命令输出,将其传递给char数组并打印出来。
您不需要查看程序中的函数,因为问题出在main上。
我的shell中命令的输出是:
$ cmus-remote -Q | grep 'tag title' | sed s/'tag title'/''/g
The View From The Afternoon
gdb中的char值为:
34 fclose(tmp2);
(gdb) print title
$4 = 0x603010 " The View From The Afternoon\n"
(gdb) n
51 char *lyric = lyrics_get(artist, title);
(gdb) print title
$5 = 0x603010 " The View From The After\001\002"
这是代码
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
#include "/usr/include/glyr/glyr.h"
#include <unistd.h>
#include <string.h>
char *lyrics_get(char *a, char *b);
int main(void)
{
FILE *tmp1;
FILE *tmp2;
char *artist;
char *title;
tmp1 = popen("cmus-remote -Q | grep 'tag artist' | sed s/'tag artist'/''/g | sed '1s/^.//'", "r");
tmp2 = popen("cmus-remote -Q | grep 'tag title' | sed s/'tag title'/''/g", "r");
fseek(tmp1, 0, SEEK_END);
size_t size1 = ftell(tmp1);
rewind(tmp1);
artist = malloc((size1 +1) * (sizeof(char)));
fread(artist, sizeof(char), size1, tmp1);
fclose(tmp1);
fseek(tmp2, 0, SEEK_END);
size_t size2 = ftell(tmp2);
rewind(tmp2);
title = malloc((size2 +1) * (sizeof(char)));
fread(title, sizeof(char), size2, tmp2)
fclose(tmp2);
char *lyric = lyrics_get(artist, title);
printf("%s", title);
printf("%s", artist);
printf("%s", lyric);
return 0;
}
char *lyrics_get(char *a, char *b)
{
glyr_init();
atexit (glyr_cleanup);
GlyrQuery q;
glyr_query_init (&q);
glyr_opt_type (&q,GLYR_GET_LYRICS);
glyr_opt_artist (&q, (a));
glyr_opt_title (&q, (b));
glyr_opt_force_utf8 (&q, true);
GLYR_ERROR err;
GlyrMemCache * head = glyr_get (&q,&err,NULL);
return (head->data);
glyr_free_list(head);
glyr_query_destroy(&q);
}
为什么会有所不同?