趋势科技实习--3个tool的实现

趋势科技实习--3个tool的实现_第1张图片


三个tool的效果展示:

Cpu100.exe(中间的是CPU显示)

AllocMemory.exe(这里是在windows下,mac系统下的效果还不是很明显,我继续完善)

AllocMemory.exe所占内存一直在增加

NewFile.exe

输入NewFile  c  1024

会生成文件c,size为1KB

趋势科技实习--3个tool的实现_第2张图片

附上代码:

cpu100.cpp
#include<stdio.h>
int main()
{
	while (1)
	{
	}
}
AllocMemory.cpp
#include <stdlib.h>
#include <stdio.h>

size_t A_MEGABYTE;

int main()
{
	char *some_memory = NULL;
	int i = 0;
	while (i<20)
	{
		scanf("%ld", &A_MEGABYTE);
		size_t size_to_allocate;
		size_to_allocate = A_MEGABYTE;
		some_memory = (char*)malloc(size_to_allocate);
		//printf("%s\n",&some_memory);
		i++;
	}
	system("pause");
	return 0;
} 

NewFile.cpp
// NewFile.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

/*long long GetFreeSpaceSize()
{
long long nFreeSize = 0;
ULARGE_INTEGER FreeBytesAvailable;
ULARGE_INTEGER TotalNumberOfBytes;
ULARGE_INTEGER TotalNumberOfFreeBytes;

char szPath[MAX_PATH] = {0};
GetModuleFileName(GetModuleHandle(NULL), szPath, MAX_PATH);
char* p = strstr(szPath,":\\");
*(p+2)='\0';

GetDiskFreeSpaceEx(szPath,&FreeBytesAvailable,&TotalNumberOfBytes,&TotalNumberOfFreeBytes);
nFreeSize = TotalNumberOfFreeBytes.HighPart;
nFreeSize = nFreeSize<<(4*8)+TotalNumberOfFreeBytes.LowPart;

cout<<szPath<<" , "<<nFreeSize<<endl;
return nFreeSize;
}
*/

bool NewFile(string strFileName, long long nFileSize)
{
	cout << "FileName:" << strFileName << " , FileSize:" << nFileSize << "B" << endl;

	//windows下可以使用GetFreeSpaceSize得到最大的剩余空间
	//long long nFreeSize = GetFreeSpaceSize();
	long long nFreeSize = 102400;

	if (nFileSize>nFreeSize)
	{
		cout << "Error : nFileSize>nFreeSize" << endl;
		return 0;
	}

	ofstream outfile(strFileName.c_str());
	if (!outfile)
	{
		cout << "can not open!" << endl;
		exit(0);
	}
	char ch = 'A';
	for (long long i = 0; i<nFileSize; i++)
	{
		outfile << ch;
	}

	outfile.close();

	return true;
}

void Usage()
{
	cout << "Usage : NewFile.exe filename filesize(B)" << endl;
}

int main(int argc, char* argv[])
{
	Usage();
	NewFile(argv[1], atoi(argv[2]));
	//NewFile("a",10);
	return 0;
}

你可能感兴趣的:(趋势科技实习--3个tool的实现)