最初の繰り返しの後に奇妙な文字列が出てきます。私はそれが文字列の終了のためかもしれないと思うが、私はそれを修正する方法がわからない。あるいは、mallocを間違った方法で使用している可能性があります。ファイルからgetcを読み込んだ後、Cで奇妙な文字列を取得する
私はどんなヒントもありがとうございます。
#include <stdio.h>
#include <memory.h>
#include <malloc.h>
#include <ctype.h>
#include "file_reader.h"
/**
* Opens a text file and reads the file. The text of the file is stored
* in memory in blocks of size blockSize. The linked list with the text is
* returned by the function. Each block should contain only complete words.
* If a word is split by the end of the block, the last letters should be
* moved into the next text block. Each text block must be NULL-terminated.
* If the reading of the file fails, the program should return a meaningful
* error message.
*/
int getFileSize(FILE* file) {
FILE* endOfFile = file;
fseek(endOfFile, 0, SEEK_END);
long int size = ftell(file);
fseek(file, 0, SEEK_SET);
return (int) size;
}
LinkedList* read_text_file(const char* filename, int blockSize) {
int globalByteCounter = 0;
LinkedList* list = LinkedList_create();
int blockByteCounter;
FILE* fp = fopen(filename, "r");
int fileSize = getFileSize(fp);
char* tokPointer = malloc(sizeof(getc(fp)));
char* block = malloc(sizeof strcat("",""));
//Loop for blocks in list
while (globalByteCounter <= fileSize) {
blockByteCounter = 0;
char* word = malloc(sizeof(blockSize));
//loop for each block
while(blockByteCounter<blockSize) {
char tok;
//Building a word
do {
strcat(word, tokPointer);
tok = (char) getc(fp);
tokPointer=&tok;
blockByteCounter++;
}while (isalpha(tok));
//Does this word still fit the block?
if (blockByteCounter + strlen(word) < blockSize) {
strcat(block, word);
//Setze Wort zurück und füge Sonderzeicehen an
word = strcpy(word,tokPointer);
} else {
strcpy(block,word);
}
}
globalByteCounter += blockByteCounter;
LinkedList_append(list, block);
free(word);
}
LinkedList_append(list,block);
fclose(fp);
free(block);
free(tokPointer);
return list;
}
'sizeof'関数を完全に間違って使用しています。それが実際にどのように機能するかについていくつかの研究を行う必要があります。 –