C++写了一个死循环例子,有时能循环,有时不能循环,各位老师看看
C++写了一个读取内存数据写入txt文件中死循环例子,有时能循环,有时不能循环,特别是double c =a - b改为double c = (a - b)/100就只能循环一次,极不稳定,一直找不到原因,麻烦各位老师看看,谢谢了。
#include <windows.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int CountLines(char *filename)//获取文件的行数
{
说实话,我是来学习的,但是没看懂,完了再来学学 #include <windows.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int CountLines(char *filename)//获取文件的行数
{
ifstream ReadFile;
int n = 0;
string temp;
ReadFile.open(filename, ios::in);//ios::in 表示以只读的方式读取文件
if (ReadFile.fail())//文件打开失败:返回0
{
return 0;
}
else//文件存在,返回文件行数
{
while (getline(ReadFile, temp))
{
n++;
}
return n;
}
ReadFile.close();
}
string ReadLine(char *filename, int line)
{
int lines, i = 0;
string temp;
fstream file;
file.open(filename, ios::in);
lines = CountLines(filename);
if (line <= 0)//(line <= 0)
{
return "Error 1: 行数错误,不能为0或负数。";
}
if (file.fail())
{
return "Error 2: 文件不存在。";
}
if (line>lines)
{
return "Error 3: 行数超出文件长度。";
}
while (getline(file, temp) && i<line -1)
{
i++;
}
file.close();
return temp;
}
int main()
{
system("pause");
while (1)
{
double dwsunshineAddressValue = 0;
Sleep(2000);
//1.获取窗口
HWNDhGameWnd = FindWindow(NULL, "窗口");
if (hGameWnd == 0)
{
printf("没有运行(找到该窗口).\n");
getchar();
return 0;
}
//2.通过窗口句柄获取进程ID
unsigned long dwPID = 0;
GetWindowThreadProcessId(hGameWnd, &dwPID);
if (dwPID == 0)
{
printf("获取ID失败.\n");
getchar();
return 0;
}
//printf("进程PID:%d\n", dwPID);
//3.通过PID获取进程句柄
//打开进程
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID);
if (hProcess == 0)
{
printf("打开进程失败.\n");
getchar();
return 0;
}
ShowWindow(hGameWnd, SW_RESTORE);//如果界面最小化,将其激活
Sleep(50);
keybd_event(0x01, 0, 0, 0);//按一下鼠标左键
Sleep(50);
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 120, 0);//向上(-120向下)滚动一次鼠标中键
char buf;//
HWND hw = (HWND)67412; // 这是一个窗口的句柄的数值
int len = SendMessage(hw, WM_GETTEXTLENGTH, 0, 0);
SendMessage(hw, WM_GETTEXT, len + 1, (LPARAM)buf);//获取窗口数字
//1、读取静态地址1
if (FALSE == ReadProcessMemory(hProcess, (void*)0x163BD7F4, &dwsunshineAddressValue, sizeof(double), NULL))
{
//printf("读取静态地址.\n");
}
double a = dwsunshineAddressValue;
//2、读取静态地址2
if (FALSE == ReadProcessMemory(hProcess, (void*)0x163BDBB4, &dwsunshineAddressValue, sizeof(double), NULL))
{
//printf("读取静态地址.\n");
}
double b = dwsunshineAddressValue;
//3、读取静态地址3
if (FALSE == ReadProcessMemory(hProcess, (void*)0x163BDB8C, &dwsunshineAddressValue, sizeof(double), NULL))
{
//printf("读取静态地址.\n");
}
double d = dwsunshineAddressValue;
double c = (a - b)/100;//
char* str = new char;
memset(str, 0, 900);
sprintf(str, "%.3f", c);//将a格式化输出到str中,取小数点后7位
ifstream file;
char mrg1[] = "30107312";
char mrg2[] = "30107222";
char mcg1[] = "30107769";
char mcg2[] = "30107372";
//
if (!strcmp(buf, mcg1))//
{
int LINES;
char filenameA = "C:\\Users\\Administrator\\Desktop\\TableA.txt";
file.open(filenameA, ios::in);
if (file.fail())
{
cout << "文件不存在." << endl;
file.close();
cin.get();
}
else//文件存在
{
LINES = CountLines(filenameA);
int *tc = new int;
char *t = new char;
int i = 0;
while (!file.eof()) //读取数据到数组
{
file >> tc;
//file >> t;
i++;
}
file.close(); //关闭文件
Sleep(300);
cout << "卖股1txt的行数为: " << CountLines("C:\\Users\\Administrator\\Desktop\\TableA.txt") << endl;
cout << ReadLine(filenameA, LINES - 1) << endl;//TableA.txt最后第二行
cout << ReadLine(filenameA, LINES) << endl;//TableA.txt最后一行
if (ReadLine(filenameA, LINES) == str) //
{
cout << "不写入" << endl;
}
else
{
ofstream ff("C:\\Users\\Administrator\\Desktop\\TableA.txt", ios::app);
ff << endl; //写入换行符
if (!ff)
{
cout << "File could not be opened" << endl;
}
else
{
ff.write(str, strlen(str));
}
delete str;
}
Sleep(300);
ofstream in;
in.open("1.txt", ios::trunc); //ios::trunc表示在打开文件前将文件清空,由于是写入,文件不存在则创建
if (ReadLine(filenameA, LINES) >ReadLine(filenameA, LINES - 1))//Table.txt最后一行数字大于Table.txt最后第二行数字
{
cout << "yes" << endl;
in << "yes" << endl;//写入“1.txt”
//fout << "上冲" << endl;//写入“com2.txt”
}
else
{
cout << "no" << endl;
in << "no" << endl;//写入“2.txt”
}
in.close();//关闭文件
}
}
//
if (!strcmp(buf, mcg2))//
{
int LINES;
char filename = "C:\\Users\\Administrator\\Desktop\\Table.txt";
file.open(filename, ios::in);
if (file.fail())
{
cout << "文件不存在." << endl;
file.close();
cin.get();
}
else//文件存在
{
LINES = CountLines(filename);
int *tc = new int;
char *t = new char;
int i = 0;
while (!file.eof()) //读取数据到数组
{
file >> tc;
//file >> t;
i++;
}
file.close(); //关闭文件
Sleep(300);
cout << "卖股2txt的行数为: " << CountLines("C:\\Users\\Administrator\\Desktop\\Table.txt") << endl;
cout << ReadLine(filename, LINES - 1) << endl;//Table.txt最后第二行
cout << ReadLine(filename, LINES) << endl;//Table.txt最后一行
if (ReadLine(filename, LINES) == str) //(
{
cout << "不写入" << endl;
}
else
{
ofstream ff("C:\\Users\\Administrator\\Desktop\\Table.txt", ios::app);
ff << endl; //写入换行符
if (!ff)
{
cout << "File could not be opened" << endl;
}
else
{
ff.write(str, strlen(str));
}
delete str;
}
Sleep(300);
ofstream in;
in.open("2.txt", ios::trunc); //ios::trunc表示在打开文件前将文件清空,由于是写入,文件不存在则创建
if (ReadLine(filename, LINES) >ReadLine(filename, LINES - 1))//Table.txt最后一行数字大于Table.txt最后第二行数字
{
cout << "yes" << endl;
in << "yes" << endl;//写入“2.txt”
}
else
{
cout << "no" << endl;
in << "no" << endl;//写入“2.txt”
}
in.close();//关闭文件
}
}
}
}
太长了,看不下去
页:
[1]