回复 42 楼 TonyDeng
我看了 由于是C++的程序 鄙人没有学过C++只是略微学了点C 我做了做注释 不知道对不对 你有时间看看行不
#include <Windows.h>
#include <cstdio> //相当于C里面的stdio.h
#include <cstdlib> //相当于stdlib.h
#include <conio.h> //控制台输入输出 需要从网上下载
bool OpenSourceFile(FILE** file, const char* path, const char* name); /*打开原文件*/
bool CreateTargetFile(FILE** file, const char* path, size_t count); /*创建目标文件*/
HLOCAL GetErrorMessage(DWORD error_code); /*指向内存首地址,得到错误报告(双字节)*/
void Pause(void);
int main(int argc, char* argv[]) /*argc: 整数,用来统计你运行程序时送给main函数的命令行参数的个数;* argv[ ]: 字符串数组,用来存放指向你的字符串参数的指针数组, 每一个元素指向一个参数*/
{
if (argc < 2)
{
printf_s("程序使用格式: %s \"数据文件所在路径\"\n", strrchr(argv[0], '\\') + 1); /*argv[0] 指向程序运行的全路径名,strrchr是从argv[0]中从右起查询到第一 个'\\',返回值为‘\\’到结尾,即文件路径*/
Pause();
return EXIT_SUCCESS; /*已定义的常量EXIT_SUCCESS*/
}
//const char* dataPath = "..\\open circuit\\";
const char* dataPath = argv[1]; /*argv[1] 指向在DOS命令行中执行程序名后的第一个字符串,路径*/
const char* flagString = " TIME V"; /*标志字符串为TIME V*/
printf_s("处理路径: %s\n", dataPath);
FILE* inputFile; /*文件指针*/
if (!OpenSourceFile(&inputFile, dataPath, "data.out"))
{
Pause();
exit(EXIT_FAILURE);
}
char buffer[1024];
size_t count = 0; /*size_t相当于无符号长整形*/
while (fgets(buffer, sizeof(buffer) - 1, inputFile) != NULL) /*char *fgets(char *buf, int bufsize, FILE *stream);参数buf指向用来存储的地址,bufsize 存储的数据长度,stream读取的文件流*/
{
if (strstr(buffer, flagString) != NULL) /* strstr(str1,str2)返回st2在st1中的位置,若没有在st1中找到则返回null*/
{
++count;
FILE* outputFile;
if (!CreateTargetFile(&outputFile, dataPath, count)) /*没创建则跳出*/
{
Pause();
exit(EXIT_FAILURE);
break;
}
printf_s("正在生成输出文件: %d.txt\n", count); /*count.txt*/
bool escape = false;
while ((fgets(buffer, sizeof(buffer) - 1, inputFile) != NULL) && !escape) /*buffer指针位置存储inputfile非空无错*/
{
if (strlen(buffer) > 0) /*buffer长度大于0*/
{
if (buffer[0] == 0x0c) /*Form Feed (馈页),什么意思? */
{
escape = true;
break;
}
fprintf_s(outputFile, "%s", buffer);
}
}
fclose(outputFile);
}
}
fclose(inputFile);
Pause();
return EXIT_SUCCESS;
}
bool OpenSourceFile(FILE** file, const char* path, const char* name)
{
char fullFilePath[MAX_PATH];
bool success = true;
strcpy_s(fullFilePath, MAX_PATH - 1, path); /*把path中长度为MAX_path-1的内容复制给fullFilePath,path存储的是路径*/
strcat_s(fullFilePath, MAX_PATH - 1, name); /*name是data.out,字符串链接,把name连接path后面*/
if (fopen_s(file, fullFilePath, "rt") != 0) /*打开的文件指针file,文件名fullFilenPath,访问类型rt(以文本文件读)*/
{
HLOCAL errorMessage = GetErrorMessage(GetLastError());
printf_s("文件\"%s\"打开失败: %s\n", fullFilePath, errorMessage);
LocalFree(errorMessage); /*释放锁定的内存*/
success = false;
}
return success;
}
bool CreateTargetFile(FILE** file, const char* path, size_t count) /*创建*.txt文件*/
{
char fullFilePath[MAX_PATH];
char numberString[4];
bool success = true;
strcpy_s(fullFilePath, MAX_PATH - 1, path); /*把path中长度为MAX_path-1的内容复制给fullFilePath,path存储的是路径*/
_itoa_s(count, numberString, sizeof(numberString), 10); /*errno_t _itoa_s(int value,char *buffer,size_t sizeInCharacters,int radix);radix为10进制buffer的进制, sizeInCharacters为buffer字符数组的长度,value为待转换整数。整体功能是把一个整数转换成字符串*/
strcat_s(fullFilePath, MAX_PATH - 1, numberString);
strcat_s(fullFilePath, MAX_PATH - 1, ".txt");
if (fopen_s(file, fullFilePath, "wt") != 0)
{
HLOCAL errorMessage = GetErrorMessage(GetLastError());
printf_s("文件\"%s\"创建失败: %s\n", fullFilePath, errorMessage);
LocalFree(errorMessage);
success = false;
}
return success;
}
HLOCAL GetErrorMessage(DWORD errorCode)
{
HLOCAL messageText = NULL;
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageText, 0, NULL);
return messageText;
}
void Pause(void)
{
printf_s("\nPress any key to continue...");
_getch();
}