这是来自getline手册页的代码片段,效果非常好。
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu :\n", read);
printf("%s", line);
printf("%s", line[0]);
}
但是,当我尝试从行变量中访问单个字符时,例如:
Declare @SQL varchar(max) = ''
Set @SQL = Stuff((Select Distinct ',' +QuoteName('C'+cast(RN as varchar(25)))
+','+QuoteName('P'+cast(RN as varchar(25)))
From (Select Distinct RN=Row_Number() over (Order By Pays) From YourTable) A
Order by 1 For XML Path('')),1,1,'')
Set @SQL = '
Select ' + replace(replace(@SQL,'[P','Pays=[P'),'[C','Count=[C') + '
From (
Select B.*
From (
Select *,RN = Row_Number() over (Order By Pays)
From YourTable
) A
Cross Apply (values (''P''+cast(A.RN as varchar(25)),A.Pays)
,(''C''+cast(A.RN as varchar(25)),cast(A.Count as varchar(50)))
) B (Item,Value)
) A
Pivot (max(value) For [Item] in (' + @SQL + ') ) p'
Exec(@SQL);
我收到了分段错误(核心转储)错误。
答案 0 :(得分:1)
此:
printf("%s", line[0]);
不是如何打印字符。 %s
告诉printf()
期望C中的字符串是char数组(相当于指向char的指针)。因此,char被视为字符串的地址,导致分段错误。
这是你想要做的:
printf("%c", line[0]);