【发布时间】:2014-08-15 15:01:39
【问题描述】:
我的程序扫描一个文本文件并返回字符数、单词数和行数。我需要对其进行修改,使其能够将文本文件扫描成 4 个相等的部分。该文件将包含编号的文本文件,如 每个文件名都换行。
1_100.txt 1_101.txt 1_10.txt 1_11.txt 1_12.txt ......
大约有 240 行文件。将它们拆分为 4 个数组后,我需要创建 4 个线程,它们将对数组中的文件执行计数操作,为它扫描的每个文件(单词、字符、行)返回 3 个值。现在我只需要知道如何将原始文本文件拆分为 4 个数组,然后我需要弄清楚如何让每个线程将其数组中的值与实际文件匹配,以便可以处理其计数。
#include "Definition.h"
#include <stdio.h>
#include "ExternalVar.h"
#include <stdlib.h>
#include <string.h>
extern int Readline(),CountWord(),CountsUpdate();
char Line[MaxLine]; /* array of scanned file */
char Line2[MaxLine];
char Line3[MaxLine];
char Line4[MaxLine];
int NChars = 0, /* number of characters seen so far */
NWords = 0, /* number of words seen so far */
NLines = 0, /* number of lines seen so far */
LineLength; /* length of the current line */
int wc = 0,
lc = 0,
cc = 0,
tc = 0;
int i;
main(int argc, char *argv[])
{
FILE *fp;
fp=fopen(argv[1],"r");
if (fp)
{
while(fgets(Line,sizeof Line,fp) != NULL)
{
//This is where I need to figure out how to split the array Line into 4 array with equal distribution.
//create threads and pass each an array
//threads return counts for their files
cc = Readline(Line);
NChars += cc;
wc = CountWord(Line);
NWords += wc;
NLines++;
}
printf("Total Lines : %d \n",NLines);
printf("Total Words : %d \n",NWords);
printf("Total Chars : %d \n",NChars);
fclose(fp);
}
return 0;
}
【问题讨论】: