2016-12-03 6 views
0

最初の繰り返しの後に奇妙な文字列が出てきます。私はそれが文字列の終了のためかもしれないと思うが、私はそれを修正する方法がわからない。あるいは、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; 
} 
+0

'sizeof'関数を完全に間違って使用しています。それが実際にどのように機能するかについていくつかの研究を行う必要があります。 –

答えて

1

コードには複数の問題があります。私はそれらのいくつかに取り組むみましょう:

sizeof(getc(fp))

これはgetcの戻り値の型にsizeofを適用すると同じです。あなたの場合、あなたがここでやっていることはsizeof(int)です。それはあなたが望むものではありません。

あなたが読んでいるもののサイズがASCIIの数字であるテキストファイルがあるとすれば、あなたが探しているのは良い古いfscanfです。ここでは同様の

strcat("","") 

が、実際に悪いです。 strcat("a", "b")"ab"を返しません。それは"b""a"に連結しようとし、aのアドレスを返します。これは、あなたが望むことをしないだけでなく、文字列"a"を変更しようとするため、かなり悪いことです。文字列リテラルは変更できません。

blockByteCounterは初期化されていません。

そして、あなたはあなたの直感の権利だ:あなたは非終端文字列によって実行されますその上にtokPointerを連結しようとしたときに、空の文字列としてwordを初期化しない場合

char* word = malloc(sizeof(blockSize)); 

を。それだけでなく、tokPointerは初期化されていません

strcatを使用して単語を作成しようとしている理由もわかりません。これらのポインタはすべて必要ありません。あなたのバッファの必要なサイズを知ったら、1)単純にfscanfを使って1ワードを読むことができます。または2)fgetcを使用して、古い単純なカウンタiを使用して、各文字をバッファ配列に入れてから、印刷前に0で終了してください。

関連する問題