2017-03-27 2 views
0

を呼び出してからそれを操作し、 。は動的配列、ファイルを閉じ 関数に、そのファイルの内容を読み取り機能を別の関数で動的配列を作成し、私は、ファイルを開く機能を働いている機能

これまでのところ、呼び出し元の場所(メイン)に戻ったときに動的配列が範囲外になることを除いて、私は上記のすべてを行うことができました。私はメインまたは別の機能の中で、配列に追加のデータを保存したい。動的配列にデータを追加したら、その内容をソースファイルに書き込んで新しいデータで上書きし、そのファイルを閉じます。目的は、元のファイルの先頭にデータを追加することです。関数char *LoadFileData(FILE *fp, char* charPtr);で私が何をしていないのですか?メインに戻ったり変更したりすることができません。

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

FILE *fSource;  // create source file pointer instance 
    char mode[] = "a+"; // default file open mode 
    char inStr[80];  // string to get input from user 
    char *tempFileData; // dynamic string to hold the existing text file 

// Open the source file 
    strcpy(mode, "r"); // change the file opnen mode to read 
    FileOpen(&fSource, mode); 

// Load the source file into a dynamic array 
    LoadFileData(fSource, tempFileData); // this is where I fail that I can tell. 

    printf("%s", tempFileData); // print the contents of the (array) source file //(for testing right now) 
    FileClose(&fSource); // close the source file 

J

char *LoadFileData(FILE *fp, char* charPtr) 
    { 
    int i = 0; 
    char ch = '\0'; 
    charPtr = new char; // create dynamic array to hold the file contents 
    if(charPtr == NULL) 
    { 
     printf("Memory can't be allocated\n"); 
     exit(0); 
    } 
// loop to read the file contents into the array 
    while(ch != EOF) 
    { 
     ch = fgetc(fp); // read source file one char at a time 
     charPtr[i++] = ch; 
    } 
    printf("%s", charPtr); // so far so good. 
    return charPtr; 
    } 
+0

'charPtr = new char;'は動的配列を割り当てません。代わりに 'new char [length]'を使用してください。 –

+0

あなたの配列はスコープから外れていません。ポインタはあなたの呼び出し関数に戻っていません。 charPtrパラメータはchar *&charPtrとして渡すことができます。他に提案されているように、戻り値を代入します。 –

答えて

0

皆のフィードバックに基づき、これは機能した変更です。ありがとうございました!

char* LoadFileData(FILE *fp) 
{ 
    off_t size; // Type off_t represents file offset value. 
    int i = 0; 
    char ch = '\0'; 
    char *charPtr; // dynamic arrary pointer that will hold the file contents 

    fseek(fp, 0L, SEEK_END); // seek to the end of the file 
    size = ftell(fp);  // read the file size. 
    rewind(fp);    // set the file pointer back to the beginning 

    // create a dynamic array to the size of the file 
    charPtr = new char[size + 1]; 

    if (charPtr == NULL) { 
     printf("Memory can't be allocated\n"); 
     // exit(0); 
    } 

    while (ch != EOF) { 
     ch = fgetc(fp); // read source file one char at a time 
     if (ch < 0) { // do not copy it if it is an invalid char 
     } 
     else { 
      charPtr[i++] = ch; 
      // load the char into the next ellement of the array 
      // i++; 
     }// end else 
    } // end while 

    return charPtr; 
} 
2

どうstringを返すでしょうか?代わりに、値を使用することはありませんchar *を渡すの

string LoadFileData(FILE *fp, char* charPtr) 
3

tempFileDataに関数の戻り値を割り当てます。

したがって、このような機能変更:

char *LoadFileData(FILE *fp) 
{ 
    char* charPtr; 
    ... 

は次に、このようにそれを呼び出す:

tempFileData = LoadFileData(fSource); 
3

問題の一つは、次の行の組み合わせです:

charPtr = new char; // create dynamic array to hold the file contents 

    charPtr[i++] = ch; 

あなたはただ1つのcharのためにメモリを割り当てていますが、それをcのように使用しますホールドロット文字。

以下を行う必要があります。

  1. は、ファイルに存在する文字の数を検索します。
  2. すべての文字のメモリを割り当てます(配列をnullで終了する必要がある場合は+1します)。
  3. ファイルの内容を割り当てられたメモリに読み込みます。
関連する問題