Hy私はLCDディスプレイ(実行中のテキスト)のように "MSG"を書き込む必要がありますサンプルコードbleowを持って、それは再びstrartsしかし、私は"LCD"(これは10文字+終端0)のメモリを割り当て、ランダムな文字の束で埋めます。 Sample picturechar *のメモリを割り当てるときに奇妙な文字*
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
int main()
{
int LCDSize = 10;
int MSGSize;
char* LCD = (char *)malloc(LCDSize+1);
char* MSG = (char *)malloc(80);
MSG = "This is a long message, probabli will move.";
MSGSize = strlen(MSG);
if (MSGSize <= LCDSize)
{
printf(MSG);
}
else
{
char* tmpMSG;
int j = 0;
while (j < 2)
{
for (int i = 0; i < MSGSize - LCDSize + 1; i++)
{
tmpMSG = MSG+i;
strncpy(LCD, tmpMSG, LCDSize);
strcat(LCD,"\0");
printf(LCD);
delay(200);
system("cls");
}
printf("----------");
j++;
}
}
getchar();
return 0;
}
何が問題なのだろうか?
'printf(MSG);を使うのは良い考えではありません。あなたは 'printf("%s "、MSG);'を使うべきです。ユーザーがメッセージの内容を制御できず、メッセージにパーセント記号が含まれていない(すべて正常に動作していれば)が、一般的にユーザーが提供する印刷データを使用するため、使用したものは致命的な可能性があるためです。それらは「フォーマット文字列脆弱性」と呼ばれています。 –