2017-08-19 1 views
0

fwrite機能を使用してテキストファイルに文章を書きたいと思います。 - 配列内の最初のオブジェクトへのポインタ書き込まれる 文字の配列をテキストファイルにコピー

  • サイズ - 各オブジェクトのサイズ
  • 数 - 数

    fwrite(const void *restrict buffer, size_t size, size_t count, FILE *restrict stream) 
    
    1. バッファ:だから私は、関数の引数としてこれらを持っている必要がありますHow to dynamically allocate memory space for a string and get that string from user?が、それは無駄に悪い習慣である、言ったように、出力ストリームへのポインタ

    から

  • ストリームを書き込まれるオブジェクトメモリ。私は答えを読んで、私の方法でコードを書く考えを得ました。

    私の考えがある:それはEOF

  • に達するまで書き込みが続け
  • mallocを使用して、その配列がどんどん大きく作る
  • その要素に文字の配列を作ると書き込みを

    1. realloc

    残念ながら私は問題に遭遇しました。私はコードをビルドして実行するたびに、それは私に、このエラーを与える:

    has stopped working

    HERESに私のコード:

    あなたはsentence = realloc(sentence, size)を記述する必要が
    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main(void) 
    { 
        size_t index=0,size=1; 
        char ch; 
        FILE *fptr; 
        fptr=fopen("E:\\Textsample.txt","w"); 
    
        /////////////////Checking/////////////// 
        if(fptr==NULL) 
         { 
          free(fptr); 
          puts("Error occured!\n"); 
          exit(EXIT_FAILURE); 
         } 
        /////////////////Checking/////////////// 
    
        char *sentence =(char*) malloc(sizeof(char)) ; 
        puts("Plaese write :\n"); 
    
        while((ch=getchar()) != EOF) 
         { 
          sentence[index]=ch; 
          index++; 
          size++; 
          realloc(sentence,size); 
    
          /////////////////Checking/////////////// 
          if(sentence==NULL) 
           { 
            printf("Error Occured!\n"); 
            free(sentence); 
            exit(EXIT_FAILURE); 
           } 
          /////////////////Checking/////////////// 
    
         } 
    
        //Copying sentnce(s) to the text file. 
        fwrite(sentence,sizeof sentence[0],index,fptr); 
        free(sentence); 
        free(fptr); 
        return 0; 
    } 
    
  • +0

    Nitpick:チェックコードでNULL ptrを解放する必要はありません。 –

    +0

    "動作を停止しました"?何?たぶん、デバッガで実行し、それがクラッシュする行を参照してください。 –

    +0

    @ HennoBrandsma-再割り当てに失敗したらどうしますか? –

    答えて

    2

    、私は思うが。

    また、必要なたびに割り当てサイズを倍増する方が効率的です。 それでは、すでに読んだとおりにもう一度読むことができるので、再配分はそれほど必要ありません。それではsizeダブルス(indexが読み取り文字を追跡しながら、それは、割り当てられたバッファのサイズです。

    ので

    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main(void) 
    { 
        size_t index=0,size=1; 
        int ch; 
        FILE *fptr; 
        fptr=fopen("./bla.txt","w"); 
    
        /////////////////Checking/////////////// 
        if(fptr==NULL) 
        { 
          fprintf(stderr, "Error occured!\n"); 
          exit(EXIT_FAILURE); 
        } 
        /////////////////Checking/////////////// 
    
        char *sentence = malloc(size) ; 
        puts("Please write, (close with return) :\n"); 
    
        while((ch=getchar()) != '\n') 
        { 
          sentence[index]=(ch & 0xff); 
          index++;; 
          if (index >= size){ 
            sentence = realloc(sentence,2*size); 
    
            /////////////////Checking/////////////// 
            if(sentence==NULL) 
            { 
            fclose(fptr); 
            fprintf(stderr, "Error Occured!\n"); 
            free(sentence); 
            exit(EXIT_FAILURE); 
            } 
            /////////////////Checking/////////////// 
            size *= 2; /* update size */ 
          } 
        } 
    
        //Copying sentence(s) to the text file. 
        fwrite(sentence,1,index,fptr); 
        free(sentence); 
        fclose(fptr); 
        return 0; 
    

    }私のシステムで正常に動作します

    +0

    あなたの言ったことはやったけど、それでも動作しません。 –

    +0

    @Ceeker自分のコードを試してみませんか? –

    +0

    ありがとうございました。 –

    0

    すでにオープンしているファイルにstdinを書きたいだけなので、malloc中間の人を省略しないのはなぜですか?

    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main(void) 
    { 
        int ch; 
        FILE *fptr; 
        fptr=fopen("./bla.txt","w"); 
    
        /////////////////Checking/////////////// 
        if(fptr==NULL) 
        { 
          fprintf(stderr, "Error occured!\n"); 
          exit(EXIT_FAILURE); 
        } 
        /////////////////Checking/////////////// 
    
        puts("Please write, (close with return) :\n"); 
    
        while((ch=getchar()) != '\n') 
        {  
          fputc(ch, fptr); 
        } 
        //if you want the newline too 
        fputc('\n', fptr); 
        fclose(fptr); 
        return 0; 
    

    関連する問題