user-0 提问: sprintf赋值255个char型数组 有什么好方法？

回复:
user: user-1
content: #pragma warning(disable:4996) //开头加这句或项目、属性、配置属性、C/C++、预处理器、预处理器定义中添加“_CRT_SECURE_NO_WARNINGS” #include <stdio.h> char s[8192]; int DayGridWeights[255]; int NightGridWeights[255]; int i,L,p; int main() {     for (i=0;i<255;i++) {           DayGridWeights[i]=i;         NightGridWeights[i]=i;     }      p=0;     L=0;      p+=L;  L=sprintf(s+p,"{\"DayGridWeights\":[");     for (i=0;i<255-1;i++) {         p+=L;  L=sprintf(s+p,"%d,",DayGridWeights[i]);     }     p+=L;  L=sprintf(s+p,"%d]",DayGridWeights[255-1]);      p+=L;  L=sprintf(s+p,",\"NightGridWeights\":[");     for (i=0;i<255-1;i++) {         p+=L;  L=sprintf(s+p,"%d,",NightGridWeights[i]);     }     p+=L;  L=sprintf(s+p,"%d]}",NightGridWeights[255-1]);     p+=L;      printf("len:%d\n",p);     printf("%s",s);      return 0; } //len:1861 //{"DayGridWeights":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254],"NightGridWeights":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254]}

user: user-2
content: 我的理解，你是不是想这样做：     char buffer[255];     for (int i = 0; i < 255; i++) {         sprintf(buffer, "{i = %d, \"DayGridWeights\":[%d],\"NightGridWeights\":[%d]}", i, DayGridWeights[i], NightGridWeights[i]);         printf("buffer = %s\n", buffer);     }

user: user-3
content: sprintf函数会清空的，你这相当于重复赋值了255次而已。 sprintf(buffer, "%s {i = %d, \"DayGridWeights\":[%d],\"NightGridWeights\":[%d]}",buffer, i, DayGridWeights[i], NightGridWeights[i]); 这样才能把255次拼接成一个字符串

user: user-0
content: 不好意思 我应该表达的不清楚 我想是：sprintf("{\"DayGridWeights\":[%d,%d,%d....],\"NightGridWeights\":[%d,%d,%d....]}", DayGridWeights[0],DayGridWeights[1],DayGridWeights[2],.....NightGridWeights[0],NightGridWeights[1],NightGridWeights[2]........)); 一次把225个成员全部赋值进去，一个一个的 一直要写225次，太麻烦，所以想问下有没有简单的方法

user: user-4
content: 开个大区间，循环了往里加，一次加一组数据？

user: user-0
content: 不好意思 我应该表达的不清楚 我想是：sprintf("{\"DayGridWeights\":[%d,%d,%d....],\"NightGridWeights\":[%d,%d,%d....]}", DayGridWeights[0],DayGridWeights[1],DayGridWeights[2],.....NightGridWeights[0],NightGridWeights[1],NightGridWeights[2]........)); 一次把225个成员全部赋值进去，一个一个的 一直要写225次，太麻烦，所以想问下有没有简单的方法，，sprintf怎么用循环往里加？恕我知识浅薄，麻烦详细说明

user: user-5
content: 根据已有的描述，无法理解你的意图。

user: user-0
content: 不好意思 我应该表达的不清楚 我想是：sprintf("{\"DayGridWeights\":[%d,%d,%d....],\"NightGridWeights\":[%d,%d,%d....]}", DayGridWeights[0],DayGridWeights[1],DayGridWeights[2],.....NightGridWeights[0],NightGridWeights[1],NightGridWeights[2]........)); 一次把225个成员全部赋值进去，一个一个的 一直要写225次，太麻烦，所以想问下有没有简单的方法