我有一些C代码,它逐行读取文本文件,在每一行中散列字符串,并使用最大的哈希值保存字符串的运行计数。
这似乎是在做正确的事情,但当我发布打印声明时:
printf("Found Bigger Hash:%s\tSize:%d\n", textFile.biggestHash, textFile.maxASCIIHash);
我的打印在输出中返回如下内容:
预处理: dict1
发现BiSize:110小时:a
发现BiSize:857小时:aardvark
发现BiSize:861h:大灰狼
发现BiSize:937h:废弃
发现BiSize:951h:弃尸
发现BiSize:1172:遗弃
发现BiSize:1283:缩写
发现BiSize:1364:非生物发生
发现BiSize:1593:非生物发生
发现BiSize:1716:心不在焉
发现BiSize:1726:棘皮动物
发现BiSize:1826:住宿
发现BiSize:1932年:腺癌
发现BiSize:2162:肾上腺皮质激素
发现BiSize:2173:化学自养
发现BiSize:2224:反革命
发现BiSize:2228:反革命分子
发现BiSize:2258:树状年代学
发现BiSize:2440:脑电图
发现BiSize:4893:pneumonoultramicroscopicsilicovolcanoconiosis
最大尺寸:46umonoult--总字数:71885
所以看起来我误用了printf()。以下是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORD_LENGTH 100 // Max number of characters per word
// data1 struct carries information about the dictionary file; preprocess() initializes it
struct data1
{
int numRows;
int maxWordSize;
char* biggestWord;
int maxASCIIHash;
char* biggestHash;
};
int asciiHash(char* wordToHash);
struct data1 preprocess(char* fileName);
int main(int argc, char* argv[]){
//Diagnostics Purposes; Not used for algorithm
printf("Preprocessing: %s\n",argv[1]);
struct data1 file = preprocess(argv[1]);
printf("Biggest Word:%s\t Size:%d\tTotal Words:%d\n", file.biggestWord, file.maxWordSize, file.numRows);
//printf("Biggest hashed word (by ASCII sum):%s\tSize: %d\n", file.biggestHash, file.maxASCIIHash);
//printf("**%s**", file.biggestHash);
return 0;
}
int asciiHash(char* word)
{
int runningSum = 0;
int i;
for(i=0; i<strlen(word); i++)
{
runningSum += *(word+i);
}
return runningSum;
}
struct data1 preprocess(char* fName)
{
static struct data1 textFile = {.numRows = 0, .maxWordSize = 0, .maxASCIIHash = 0};
textFile.biggestWord = (char*) malloc(WORD_LENGTH*sizeof(char));
textFile.biggestHash = (char*) malloc(WORD_LENGTH*sizeof(char));
char* str = (char*) malloc(WORD_LENGTH*sizeof(char));
FILE* fp = fopen(fName, "r");
while( strtok(fgets(str, WORD_LENGTH, fp), "\n") != NULL)
{
// If found a larger hash
int hashed = asciiHash(str);
if(hashed > textFile.maxASCIIHash)
{
textFile.maxASCIIHash = hashed; // Update max hash size found
strcpy(textFile.biggestHash, str); // Update biggest hash string
printf("Found Bigger Hash:%s\tSize:%d\n", textFile.biggestHash, textFile.maxASCIIHash);
}
// If found a larger word
if( strlen(str) > textFile.maxWordSize)
{
textFile.maxWordSize = strlen(str); // Update biggest word size
strcpy(textFile.biggestWord, str); // Update biggest word
}
textFile.numRows++;
}
fclose(fp);
free(str);
return textFile;
}
发布于 2015-10-16 12:39:27
你的陈述
while( strtok(fgets(str, WORD_LENGTH, fp), "\n") != NULL)
不考虑来自fgets()
的返回值或strtok()
的工作方式。
这样做的方法就像
char *fptr, *sptr;
while ((fptr = fgets(str, WORD_LENGTH, fp)) != NULL) {
sptr = strtok(fptr, "\n");
while (sptr != NULL) {
printf ("%s,", sptr);
sptr = strtok (NULL, "\n");
}
printf("\n");
}
注在第一次调用strtok()
之后,对同一序列的后续调用必须传递参数NULL
。
https://stackoverflow.com/questions/33178883
复制