user-0 提问: 只有一个error，文件相似度问题

回复:
user: user-1
content: 在占用内存空间较大的局部数组声明的前面加static将其从堆栈数据段挪到全局数据段即可避开因局部数组大小超过默认堆栈大小1MB造成程序不能正常运行的问题。  ... int main(int argc, char *argv[]) { FILE *file = NULL; int i = 0; int iWordCountOne = 0; int iWordCountTwo = 0; char *pTextOne = NULL; char *pTextTwo = NULL; static T_WordCount tWordSetOne[MAX_WORD_COUNT]; static T_WordCount tWordSetTwo[MAX_WORD_COUNT]; ...

user: user-0
content: 不行啊

user: user-2
content: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #include<stdlib.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <errno.h>    #define MAX_WORD_COUNT 1024 //最多能保存的单词数，自己修改 #define MAX_WORD_LENGTH 50//每个单词的最大长度，自己修改 #define OUT_FILE "f:\\result.txt"//默认输出文件名,自己修改  //保存单词及在文章中出现个数的结构体 typedef struct WordCount  { 	char cWord[MAX_WORD_COUNT]; 	int  iCount; }T_WordCount;  //保存相同单词在数组中位置的结构体 typedef struct  SameWordInfo { 	int iIndexOne; 	int iIndexTwo;  }T_SameWordInfo;  //将单词转换成小写形式，方便统计 void LowerText(char *pText) { 	char *pTmp = pText; 	while (*pTmp != '\0') 	{ 		if ((*pTmp >= 'A' && *pTmp <= 'Z')) 		{ 			*pTmp += 32 ; 		}  		pTmp++; 	} }  //从文章中提取单词 //并统计出单词总数及每个单词出现的次数 int ExtractWord(const char *pText, T_WordCount *pWordSet, int *pWordCount) { 	char cTmp[MAX_WORD_LENGTH] = {0};  	int  i  = 0; 	char *pTmp   = cTmp; 	int  iFlag   = 0;  	while (*pText != '\0') 	{  		if ((*pText >= 'A' && *pText <= 'Z') || (*pText >= 'a' && *pText <= 'z')) 		{  			*pTmp = *pText; 			pTmp++; 		} 		else if (*pText == '-') 		{ 			++pText; 			continue; 		} 		else 		{  			if (strlen(cTmp) > 0) 			{ 				LowerText(cTmp); 				iFlag = 0; 				for (i = 0; i < MAX_WORD_COUNT; ++i) 				{ 					if (strlen(pWordSet[i].cWord) > 0) 					{ 						if (strcmp(pWordSet[i].cWord, cTmp) == 0) 						{ 							iFlag = 1; 							pWordSet[i].iCount++; 							(*pWordCount)++;  							break; 						}  					} 					else 					{ 						strcpy(pWordSet[i].cWord, cTmp); 						pWordSet[i].iCount = 1; 						iFlag = 1; 						(*pWordCount)++;  						break; 					}  				}  				if (!iFlag) 				{ 					printf("No more space to save word.\n"); 				} 			} 			memset(cTmp, 0, MAX_WORD_LENGTH); 			pTmp = cTmp; 		}  		++pText; 	}   	return 0; }  //计算结果 //同时输出结果到屏幕和文件 int CalcResult(T_WordCount *pSetOne, T_WordCount *pSetTwo, int *pWordCountOne, int *pWordCountTwo) { 	int i = 0; 	int j = 0; 	int iCurIndex = 0; 	int iSameCountOne = 0; 	int iSameCountTwo = 0; 	FILE *file = NULL;  	T_SameWordInfo tSameWordInfo[MAX_WORD_COUNT]; 	memset(&tSameWordInfo, 0, sizeof(tSameWordInfo));  	for (i = 0; i < MAX_WORD_COUNT; ++i) 	{ 		if (strlen(pSetOne[i].cWord) <= 0) 		{ 			break; 		}  		for (j = 0; j < MAX_WORD_COUNT; ++j) 		{ 			if (strlen(pSetTwo[j].cWord) <= 0) 			{ 				break; 			}  			if (strcmp(pSetOne[i].cWord, pSetTwo[j].cWord) == 0) 			{ 				tSameWordInfo[iCurIndex].iIndexOne = i; 				tSameWordInfo[iCurIndex].iIndexTwo = j; 				++iCurIndex; 			} 		} 	}   	file = fopen(OUT_FILE, "w"); 	if (file == NULL) 	{ 		printf("Can not open file to write.\n"); 		return -1; 	} 	printf("相似度统计结果:\n"); 	fprintf(file, "相似度统计结果:\n");  	printf("%12s  %14s  %12s\n", " 相同单词数 |", " 文1数量  |", " 文2数量"); 	fprintf(file, "%12s  %14s  %12s\n", " 相同单词数 |", " 文1数量  |", " 文2数量"); 	printf("------------+---------------+-----------\n"); 	fprintf(file, "------------+---------------+-----------\n");  	for (i = 0; i < iCurIndex; ++i) 	{ 		iSameCountOne += pSetOne[(tSameWordInfo[i].iIndexOne)].iCount; 		iSameCountTwo += pSetTwo[(tSameWordInfo[i].iIndexTwo)].iCount; 		printf("%-12s|  %-13d|  %-12d\n", pSetOne[(tSameWordInfo[i].iIndexOne)].cWord,  			pSetOne[(tSameWordInfo[i].iIndexOne)].iCount, 			pSetTwo[(tSameWordInfo[i].iIndexTwo)].iCount 			);  		fprintf(file, "%-12s|  %-13d|  %-12d\n", pSetOne[(tSameWordInfo[i].iIndexOne)].cWord,  			pSetOne[(tSameWordInfo[i].iIndexOne)].iCount, 			pSetTwo[(tSameWordInfo[i].iIndexTwo)].iCount 			); 	} 	printf("------------+---------------+-----------\n"); 	fprintf(file, "------------+---------------+-----------\n");  	printf("相同单词数:%d\n", iCurIndex); 	fprintf(file, "相同单词数:%d\n", iCurIndex);  	printf("在文1中所占比:%d/%d=%.0f%%\n", iSameCountOne, *pWordCountOne, 100 * (float)iSameCountOne/(*pWordCountOne)); 	fprintf(file, "在文1中所占比:%d/%d=%.0f%%\n", iSameCountOne, *pWordCountOne, 100 * (float)iSameCountOne/(*pWordCountOne)); 	printf("在文2中所占比:%d/%d=%.0f%%\n", iSameCountTwo, *pWordCountTwo, 100 * (float)iSameCountTwo/(*pWordCountTwo));  	fprintf(file, "在文2中所占比:%d/%d=%.0f%%\n", iSameCountTwo, *pWordCountTwo, 100 * (float)iSameCountTwo/(*pWordCountTwo));   	fclose(file);  	return 0;  }   //主函数 //首先获得文件大小，并动态分配内存，然后读取文章到内存中，并处理 //如果文章比较大，会比较耗内存 //可以考虑优化一下 int main(int argc, char *argv[]) { 	FILE *file = NULL; 	int i = 0; 	int iWordCountOne = 0; 	int iWordCountTwo = 0; 	char *pTextOne = NULL; 	char *pTextTwo = NULL; 	T_WordCount tWordSetOne[MAX_WORD_COUNT]; 	T_WordCount tWordSetTwo[MAX_WORD_COUNT];  	struct stat stStatus;   	//如果参数个数不对，退出，并显示使用方法 	if (argc != 3) 	{ 		printf("Usage:app file1 file2\n"); 		return -1; 	}  	//获取文件信息 	if (0 != stat(argv[1], &stStatus)) 	{ 		printf("Can not get file info of [%s],%d\n", argv[1], errno); 		return  -1; 	}  	//以只读模式打开文件 	file = fopen(argv[1], "r"); 	if (file == NULL) 	{ 		printf("Can not open file:%s\n", argv[1]); 		return  -1; 	}  	//分配比较文件大小多1个字节的内存 	pTextOne = (char *)malloc(stStatus.st_size + 1); 	if (pTextOne == NULL) 	{ 		printf("Can not allocate memory.\n"); 		return -1; 	} 	//内存初始化 	memset(pTextOne, 0, stStatus.st_size + 1);  	//读取文章到内存中 	if(fread(pTextOne, sizeof(char), (size_t)stStatus.st_size, file) <= 0) 	{ 		printf("Can not read file.\n"); 		return -1; 	}  	//以下处理文章2，步骤同上 	if (0 != stat(argv[2], &stStatus)) 	{ 		printf("Can not get file info of [%s]\n", argv[2]); 		return  -1; 	}  	file = fopen(argv[2], "r"); 	if (file == NULL) 	{ 		printf("Can not open file:%s\n", argv[2]); 		return  -1; 	}  	pTextTwo = (char *)malloc(stStatus.st_size + 1); 	if (pTextTwo == NULL) 	{ 		printf("Can not allocate memory.\n"); 		return -1; 	} 	memset(pTextTwo, 0, stStatus.st_size + 1);  	if(fread(pTextTwo, sizeof(char), (size_t)stStatus.st_size, file) <= 0) 	{ 		printf("Can not read file.\n"); 		return -1; 	}  	//fclose(file);  	//printf("%s\n", pTextOne); 	//printf("%s\n", pTextTwo);   	memset(tWordSetOne, 0, sizeof(tWordSetOne)); 	memset(tWordSetTwo, 0, sizeof(tWordSetTwo));  	//提取单词 	ExtractWord(pTextOne, tWordSetOne,&iWordCountOne); 	ExtractWord(pTextTwo, tWordSetTwo,&iWordCountTwo);  	//计算结果 	CalcResult(tWordSetOne, tWordSetTwo,&iWordCountOne,&iWordCountTwo);  	//释放内存 	free(pTextOne); 	free(pTextTwo);  	return 0;  }   编译通过~~~ 我修改了

user: user-1
content: 编译错误： 偶遇到类似问题都是用 “每次用/*...*/注释掉不同部分再重新编译，直到定位到具体语法出错的位置。” 的方法解决的。  运行错误： 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-0
content: 求大神帮帮忙