简体   繁体   English

C:从文件读取int并存储在指针数组中

[英]C: Read ints from file and store in pointer array

I am trying to read integers from a file and store them in an array. 我试图从文件中读取整数并将其存储在数组中。 Then, I want to write those values to a different file. 然后,我想将这些值写入另一个文件。 Reading an writing are done in a separate file from the main file, where the array was created. 读取写作是在与创建数组的主文件不同的文件中进行的。 I cannot change the function's arguments (this is for an assignment, so the array argument must be int** ppPerm). 我无法更改函数的参数(这是分配的,因此数组参数必须为int ** ppPerm)。 The function is called, and the array originally created, in another file's main function. 该函数在另一个文件的主函数中被调用,并且最初创建了数组。 The file I am reading from looks like this: 我正在读取的文件如下所示:

15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Where 15 is how many numbers will follow. 15是多少个数字。 So the chronological order of functions is: 因此,功能的时间顺序为:

Array Perm is created in the main function of the main file (int* Perm = NULL). Array Perm在主文件的主函数中创建(int * Perm = NULL)。 It is passed into readP() 它被传递到readP()

readP(In, &Perm);

The numbers from the file are read and stored in Perm. 读取文件中的数字并将其存储在彼尔姆中。 The same variable Perm is then passed into writeP(). 然后将相同的变量Perm传递到writeP()中。

writeP(Out, Perm, permLength);

Perm is read and written to a different file. 彼尔姆被读取并写入另一个文件。 I cannot change either of those lines. 我不能更改任何一条线。 Somewhere along the road the array is screwed up. 沿路的某个地方将阵列弄坏了。 Here is readP(). 这是readP()。

int readP(FILE* In, int** ppPerm) {
   int numElements = 0;

   fscanf(In, "%d", &numElements);

   *ppPerm = (int*)calloc(numElements, sizeof(int));

   int i;
   for (i = 0; i < numElements; i++) {
       fscanf(In, "%p", &ppPerm[i]);
   }

   return numElements;
}

Right now, the array is completely unreadable. 现在,该数组是完全不可读的。 For whatever reason, the numbers stored are something like 0x0 then a random jumble of hexadecimals. 无论出于何种原因,存储的数字都类似于0x0,然后是十六进制的随机混杂。 The array is then used in writeP() to write the values to a different file: 然后在writeP()中使用该数组将值写入另一个文件:

void writeP(FILE* Out, const int* pPerm, int permLength) {

    int i = 2;
    for (i = 0; i < permLength; i++) {
        fprintf(Out, "%d ", pPerm[i]);
    }

    return;
}

int* pPerm is the same array that was passed into readP(). int * pPerm是传递给readP()的同一数组。 For some reason, using debugging, I see that pPerm contains completely different values than ppPerm did, and in some cases it seems half empty. 出于某种原因,通过调试,我发现pPerm包含的值与ppPerm完全不同,在某些情况下,它似乎是空的。 What exactly is wrong with my functions? 我的功能到底出了什么问题? Why can I not store the numbers in the array correctly? 为什么不能将数字正确存储在数组中? And why does the array keep messing up between readP() and writeP()? 为何数组在readP()和writeP()之间不断混乱?

Don't cast the result of calloc() or malloc() in C! 不要在C中calloc()malloc()的结果! It can hide errors. 它可以隐藏错误。

 int i; for (i = 0; i < numElements; i++) { fscanf(In, "%p", &ppPerm[i]); } 

Since you want to read integers the format string should be "%i" or "%d" . 由于要读取整数,因此格式字符串应为"%i""%d" To get to the Pointer to the memory you have allocated use *ppPerm : 要获得指向已分配内存的指针,请使用*ppPerm

size_t i;  // variables that hold an index into or the size
           // of objects in memory should be of type size_t.
for (i = 0; i < numElements; i++) {
    fscanf(In, "%p", &(*ppPerm)[i]);
}

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

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