一:
#ifdef UNICODE
wchar_t str[1024]={0};
#else
char str[1024]={0};
#endif- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <windows.h>
- #include <graphics.h>
-
- #include <mmsystem.h>
- #pragma comment(lib,"winmm.lib")
- #pragma comment(lib,"user32.lib")
-
- #ifdef _MSC_VER
- #pragma warning(disable:4996)
- #endif
-
- int main (){
-
- #ifdef UNICODE
- wchar_t str[1024]={0};
- #else
- char str[1024]={0};
- #endif
-
- TCHAR *szBuffer=(TCHAR *)calloc(1024,sizeof(TCHAR));
- initgraph(1200, 700);
- int a = 0;
-
- while (!kbhit())
- {
- MOUSEMSG msg = GetMouseMsg();
- if (msg.uMsg == WM_LBUTTONDOWN) {
- wsprintf(szBuffer,TEXT("%s %d.mp3"),TEXT("close"),a);
- mciSendString(szBuffer, NULL, 0, NULL);
- a=a+1; }
-
- if (msg.uMsg == WM_RBUTTONDOWN) {
- wsprintf(szBuffer,TEXT("%s %d.mp3"),TEXT("close"),a);
- mciSendString(szBuffer, NULL, 0, NULL);
- a=a-1; }
-
- wsprintf(szBuffer,TEXT("%s %d.mp3"),TEXT("play"),a);
- mciSendString(szBuffer, NULL, 0, NULL);
-
- #ifdef UNICODE
- swprintf(str,111,L"%d.jpg",a);
- #else
- sprintf(str,"%d.jpg",a);
- #endif
-
- IMAGE img;
- loadimage(&img,str,1200,500);
- putimage(0, 0, &img);
-
- #ifdef UNICODE
- swprintf(str,111,L"%d.txt",a);
- #else
- sprintf(str,"%d.txt",a);
- #endif
-
- FILE *file = fopen(str, "r");// 打开并读取文本文件
- if (file)
- {
- char buffer[1024];
- int y = 600; // 假设图片下方开始显示文本,上下600位置写字
- char *line;
- while (fgets(buffer, sizeof(buffer), file))
- {// 逐行读取文件内容并显示
- line = strdup(buffer); // 复制行以便修改
- outtextxy(20, y, line); // 显示文本,假设从(20, y)开始
- free(line);
- y += 20; // 下一行的y坐标
- }
- fclose(file); // 关闭文件
- }
-
- }
-
- if(szBuffer != NULL) {free(szBuffer);szBuffer=NULL;}
- closegraph();
-
- return 0;
- }
复制代码 二:
TCHAR str[1024]={0};- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <windows.h>
- #include <graphics.h>
-
- #include <mmsystem.h>
- #pragma comment(lib,"winmm.lib")
- #pragma comment(lib,"user32.lib")
-
- #ifdef _MSC_VER
- #pragma warning(disable:4996)
- #endif
-
- int main (){
-
- TCHAR str[1024]={0};
-
- TCHAR *szBuffer=(TCHAR *)calloc(1024,sizeof(TCHAR));
- initgraph(1200, 700);
- int a = 0;
-
- while (!kbhit())
- {
- MOUSEMSG msg = GetMouseMsg();
- if (msg.uMsg == WM_LBUTTONDOWN) {
- wsprintf(szBuffer,TEXT("%s %d.mp3"),TEXT("close"),a);
- mciSendString(szBuffer, NULL, 0, NULL);
- a=a+1; }
-
- if (msg.uMsg == WM_RBUTTONDOWN) {
- wsprintf(szBuffer,TEXT("%s %d.mp3"),TEXT("close"),a);
- mciSendString(szBuffer, NULL, 0, NULL);
- a=a-1; }
-
- wsprintf(szBuffer,TEXT("%s %d.mp3"),TEXT("play"),a);
- mciSendString(szBuffer, NULL, 0, NULL);
-
- wsprintf(str,TEXT("%d.jpg"),a);
-
- IMAGE img;
- loadimage(&img,str,1200,500);
- putimage(0, 0, &img);
-
- wsprintf(str,TEXT("%d.txt"),a);
-
- FILE *file = fopen(str, "r");// 打开并读取文本文件
- if (file)
- {
- char buffer[1024];
- int y = 600; // 假设图片下方开始显示文本,上下600位置写字
- char *line;
- while (fgets(buffer, sizeof(buffer), file))
- {// 逐行读取文件内容并显示
- line = strdup(buffer); // 复制行以便修改
- outtextxy(20, y, line); // 显示文本,假设从(20, y)开始
- free(line);
- y += 20; // 下一行的y坐标
- }
- fclose(file); // 关闭文件
- }
-
- }
-
- if(szBuffer != NULL) {free(szBuffer);szBuffer=NULL;}
- closegraph();
-
- return 0;
- }
复制代码 三:
char str[1024]={0};- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <windows.h>
- #include <graphics.h>
-
- #include <mmsystem.h>
- #pragma comment(lib,"winmm.lib")
- #pragma comment(lib,"user32.lib")
-
- #ifdef _MSC_VER
- #pragma warning(disable:4996)
- #endif
-
- int main (){
-
- char str[1024]={0};
-
- TCHAR *szBuffer=(TCHAR *)calloc(1024,sizeof(TCHAR));
- initgraph(1200, 700);
- int a = 0;
-
- while (!kbhit())
- {
- MOUSEMSG msg = GetMouseMsg();
- if (msg.uMsg == WM_LBUTTONDOWN) {
- wsprintf(szBuffer,TEXT("%s %d.mp3"),TEXT("close"),a);
- mciSendString(szBuffer, NULL, 0, NULL);
- a=a+1; }
-
- if (msg.uMsg == WM_RBUTTONDOWN) {
- wsprintf(szBuffer,TEXT("%s %d.mp3"),TEXT("close"),a);
- mciSendString(szBuffer, NULL, 0, NULL);
- a=a-1; }
-
- wsprintf(szBuffer,TEXT("%s %d.mp3"),TEXT("play"),a);
- mciSendString(szBuffer, NULL, 0, NULL);
-
- sprintf(str,"%d.jpg",a);
-
- IMAGE img;
- loadimage(&img,str,1200,500);
- putimage(0, 0, &img);
-
- sprintf(str,"%d.txt",a);
-
- FILE *file = fopen(str, "r");// 打开并读取文本文件
- if (file)
- {
- char buffer[1024];
- int y = 600; // 假设图片下方开始显示文本,上下600位置写字
- char *line;
- while (fgets(buffer, sizeof(buffer), file))
- {// 逐行读取文件内容并显示
- line = strdup(buffer); // 复制行以便修改
- outtextxy(20, y, line); // 显示文本,假设从(20, y)开始
- free(line);
- y += 20; // 下一行的y坐标
- }
- fclose(file); // 关闭文件
- }
-
- }
-
- if(szBuffer != NULL) {free(szBuffer);szBuffer=NULL;}
- closegraph();
-
- return 0;
- }
复制代码 三个都可以编译运行,大赞。
原本想把文本的星号*,看要不要也通用模式用一遍。
行 line
char *line=NULL;
替换 char *line;
if(line != NULL) {free(line);line=NULL;}
替换 free(line);
文本 file
FILE *file =NULL;
file = fopen(str, "r");
if(file != NULL) {free(file);file=NULL;}
替换 FILE *file = fopen(str, "r");
但是不是编译就是exe执行时各种问题,放whlie外面最后清理和whlie里面都不行,就算了。
就最后结束的部分 if(szBuffer != NULL) {free(szBuffer);szBuffer=NULL;}
// 或者直接 free(szBuffer) // |