I was wondering if someone could explain how I can use the data stored in a dynamically allocated memory buffer.
My aim is to read a text file, store this in said buffer (which works). Then loop through this buffer to gather and store the desired info in arrays.
The aim of my program is to take the desired info from a text file and store the information, coordinates, in respective x and y arrays.
The program worked perfectly fine with a const char array, shown below:
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.";
I wish to move past this now.
I can see that buffer is pointing to the location in memory.. how do I use this data?
#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;
}
Any help is greatly appreciated.
Thanks