【问题标题】:Using data stored in calloc buffer使用存储在 calloc 缓冲区中的数据
【发布时间】:2019-01-27 17:16:02
【问题描述】:

我想知道是否有人可以解释我如何使用存储在动态分配的内存缓冲区中的数据。

我的目标是读取一个文本文件,将其存储在所述缓冲区中(有效)。然后循环遍历这个缓冲区以收集所需的信息并将其存储在数组中。

我的程序的目的是从文本文件中获取所需的信息,并将信息、坐标存储在各自的 x 和 y 数组中。

该程序与 const char 数组完美配合,如下所示:

const char buffer[] =
"I have(60438.0,63493.0)loads of(-2100.0,63493.0) stuff "
"(87659.0,55553.0)to copy (-70048.0,-61111.0) to strings.";

我现在想跳过这个。

我可以看到缓冲区指向内存中的位置。我该如何使用这些数据?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *buffer;

#define buffer_begin (buffer)
#define buffer_end (buffer + sizeof(buffer))

/*global variables*/
const char a = '(';
const char b = ')';
int i = 1;
long pos = 0;
FILE* file;
long numbytes;

char xdest[sizeof(buffer)];
char ydest[sizeof(buffer)];

static char* copy_itx(const char* base, size_t len, char* out) {
    memcpy(out, base, len); // copy from 'base' of length 'last-first' to xptr
    out[len] = '\0';        // set everything after 
    return out + len + 1;
}

static char* copy_ity(const char* base, size_t len, char* out) {
    memcpy(out, base, len);
    out[len] = '\0';
    return out + len + 1;
}

/*main function*/
int main() {

    // read txt file and store in calloc buffer

    file = fopen("instance_info.txt","r"); /*open file*/

        if(file == NULL)/* quit if the file does not exist */
            return -1;

    fseek(file, 0L, SEEK_END); /* Get the number of bytes */
    numbytes = ftell(file);     

    fseek(file, 0L, SEEK_SET);  /* reset the file position indicator to 
                                the beginning of the file */

    buffer = (char*)calloc(numbytes, sizeof(char)); /* grab sufficient memory for the 
                                                       buffer to hold the text, 
                                                       calloc clears all rubbish memory values */
if(buffer == NULL){ /* memory error */
    perror("%s");
    return -1;
}

fread(buffer, sizeof(char), numbytes, file); /* copy all the text into the buffer, 
                                                reads 'numbytes' number of chars  */        
fclose(file); //close the file

printf("%s", buffer); 

i=0;
char* xptr = xdest;
char* yptr = ydest;
const char* first = NULL;
const char* last = NULL;

// GET X CORDS HERE

printf("\n");
printf("x cords:\n");

for (const char* it = buffer_begin; it != buffer_end; it++) { // for loop to iterate through buffer

    if (*it == '(') {   // if char pointer *it is '('

        first = it + 1; // set first char equal to open bracket
        last  = NULL;   // ensure last char still equal to a NULL char

    }

    if (*it == ',' && first != NULL) {  // if const char pointer *it is equal to ',' 
                                        //and first char isnt pointing to NULL

      last = it;        //set last char equal to current position
      char* next = copy_itx(first, last-first, xptr);   // call copy_itx function
      printf("%d: %s\n", i++, xptr);    // print char stored at xptr
      first = NULL;     // set first char back to NULL
      xptr = next;      // assign char pointer next to xptr

    }

}


// GET Y CORDS HERE

printf("\n");
printf("y cords:\n");
i=0;

for (const char* it = buffer_begin; it != buffer_end; it++) {
    if (*it == ',') {
        first = it + 1;
        last  = NULL;
    }
    if (*it == ')' && first != NULL) {
        last = it;
        char* next = copy_ity(first, last-first, yptr);
        printf("%d: %s\n", i++, yptr);
        first = NULL;
        yptr = next;
    }
}
return 0;
}

非常感谢任何帮助。

谢谢

【问题讨论】:

    标签: c file text copy calloc


    【解决方案1】:

    在我看来,先尝试重新组织一下代码。

    1. 使用 strstr() 查找指向 '(' 的指针。
    2. 现在寻找指向')'的下一个指针
    3. '(' 和 ')' 之间的字符串是一个 XY 点。解析它以填充您的数组。
    4. 将')'的指针复制到'('

    继续循环 1,2,3,4 直到输入中不再引用 '('。

    如果你想使用相同的结构,那么使用不同的迭代计数器

    char* it = buffer;
    for (int i = 0; i < numbytes ; i++) 
    {
      if (it[i] == ',') 
      {
        ...
    

    【讨论】:

      【解决方案2】:

      在正常情况下,sizeof 是编译时评估。它不能用作某种运行时函数来获取分配的内存大小。

      当您执行char xdest[sizeof(buffer)]; 时,您会得到一个大小与char* 指针相同的缓冲区(可能是 4 个字节),而不是您使用 malloc 分配的大小的缓冲区。

      您需要使用变量来跟踪分配的内存。如果多个缓冲区应具有相同的大小(在运行时确定),则 malloc 全部。

      【讨论】:

      • "当您执行 char xdest[sizeof(buffer)]; 时,您会得到一个 char* 指针大小的缓冲区(可能是 4 个字节),而不是您使用 malloc 分配的大小的缓冲区。 "我试过:xdest[numbytes]; ydest[numbytes];这没有用。然后我尝试在 xptr 和 yptr 上使用 malloc ,但这也不起作用。我做错了吗?
      猜你喜欢
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      相关资源
      最近更新 更多