简体   繁体   English

在单独的函数中创建动态数组,并从调用函数进行操作

[英]Create a dynamic array in separate function and manipulate it from calling function

I am working a function that opens a file, a function that reads the contents of that file into a dynamic array, a function that closes the file. 我正在使用一个打开文件的函数,一个将该文件的内容读入动态数组的函数,一个关闭文件的函数。

So far I am able to do all of the above except the dynamic array is going out of scope when I get back to the calling location (main). 到目前为止,我能够完成上述所有操作,但当我返回到调用位置(主)时,动态数组超出了范围。 I want to store additional data in the array while in main or even a separate function. 我想在主要甚至单独的函数中将其他数据存储在数组中。 Once I am done adding data to the dynamic array I will write the contents of it back to the source file overwriting it with the new data then closing that file. 将数据添加到动态数组后,我将其内容写回到源文件中,并用新数据覆盖它,然后关闭该文件。 The purpose is to append data to the top of the original file. 目的是将数据附加到原始文件的顶部。 What am I doing wrong with the function char *LoadFileData(FILE *fp, char* charPtr); 函数char *LoadFileData(FILE *fp, char* charPtr);我在做什么错char *LoadFileData(FILE *fp, char* charPtr); that I am not able to access or modify it back in main? 我无法在主目录中访问或修改它?

Thanks for help on this. 感谢您的帮助。

    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;
  }

Instead of passing in a char * whose value you never use, assign the return value of the function to tempFileData . 而不是传递一个您永远不会使用的char * ,而是将函数的返回值分配给tempFileData

So change the function like this: 因此,像这样更改功能:

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

Then call it like this: 然后这样称呼它:

tempFileData = LoadFileData(fSource);  

One of the problems is the combination of the following lines: 问题之一是以下几行的组合:

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

    charPtr[i++] = ch;

You are allocating memory for just one char but proceeding to use it as though it can hold lots characters. 您只为一个char分配了内存,但是好像可以容纳很多字符一样继续使用它。

You need to: 你需要:

  1. Find the number of characters present in the file. 查找文件中存在的字符数。
  2. Allocate memory for all the characters ( +1 if you need to null terminate the array). 为所有字符分配内存(如果您需要为null终止数组,则为+1)。
  3. Read the contents of the file to the allocated memory. 将文件的内容读取到分配的内存中。

返回一个string怎么样?

string LoadFileData(FILE *fp, char* charPtr)

based on everyone's feedback this is the modification that worked. 根据每个人的反馈,这是有效的修改。 Thank you! 谢谢!

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; 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM