L
Longfellow
Newbie here...
Trying to wrap my head around what's going on here. Read through the
FAQ until thoroughly confused. Got some insight from K&R2, no help from
D&D, H&S, CU or CT&P, at least that I could understand. So have done
due diligence, I think.
What I'm trying to do is extract information from one file and insert
that information into other files. The first file has a string of
records, each of which have an ID and for each, there exists another
file. For each record, information is extracted and inserted into the
relevant file. There can be any number of pieces of information for
each record, and the information in each case is extracted as a string.
So I figured that I need an array of pointers to each string that can be
created dynamically for each record as it is read. For the insertion,
the relevant file is read into a temp file, where the extracted strings
are inserted appropriately, and then the relevant file is removed and
replaced (rename()) by the temp file. This is completed for each record
before moving on to the next.
Works like a charm, but handling the array was a problem.
The following code is an example of my undoubtedly very ugly solution.
I can't figure out why it works, nor can I comprehend what the code
should be. I was paying attention to freeing allocated memory in a
timely manner, which affected the solution somehow.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *line1 = "This is line one.";
const char *line2 = "This is line two.";
const char *line3 = "This is line three.";
const char *line4 = "This is line four.";
char *array[4]
char *ptr;
printf("\nThis is the individual print-outs:\n");
ptr = malloc(sizeof line1);
strcpy(ptr, line1);
array[0] = malloc(sizeof line1);
array[0] = ptr;
free(ptr);
array[0] = malloc(sizeof line1);
strcpy(array[0], line1);
printf("array[0] is: %s\n", array[0]);
ptr = malloc(sizeof line2);
strcpy(ptr, line2);
array[1] = malloc(sizeof line2);
array[1] = ptr;
free(ptr);
array[1] = malloc(sizeof line2);
strcpy(array[1], line2);
printf("array[1] is: %s\n", array[1]);
ptr = malloc(sizeof line3);
strcpy(ptr, line3);
array[2] = malloc(sizeof line3);
array[2] = ptr;
free(ptr);
array[2] == malloc(sizeof line3);
strcpy(array[2], line3);
printf("array[2] is: %s\n", array[2]);
ptr = malloc(sizeof line4);
strcpy(ptr, line4);
array[3] = malloc(sizeof line4);
array[3] = ptr;
free(ptr);
array[3] == malloc(sizeof line4);
strcpy(array[3], line4);
printf("array[3] is: %s\n", array[3]);
printf("\nThis is the main body print loop:\n");
for (i=0; i<4; ++i)
printf("array[%d] is %s\n", i, array);
printit();
for (i=0; i<4; ++i)
free(array);
free(ptr);
return 0;
}
void printit() {
int i;
printf("\nThis is the 'printit()' function print loop:\n");
for(i=0; i<4; ++i)
printf("array[%d] is: %s\n", i, array);
}
Running this produces:
This is the individual print-outs:
array[0] is: This is line one.
array[1] is: This is line two.
array[2] is: This is line three.
array[3] is: This is line four.
This is the main body print loop:
array[0] is This is line one.
array[1] is This is line two.
array[2] is This is line three.
array[3] is This is line four.
This is the 'printit()' function print loop:
array[0] is: This is line one.
array[1] is: This is line two.
array[2] is: This is line three.
array[3] is: This is line four.
Delete or change any part of the array handling sections and something
doesn't work correctly.
What should the code be to produce the above results?
Thanks all,
Longfellow
Trying to wrap my head around what's going on here. Read through the
FAQ until thoroughly confused. Got some insight from K&R2, no help from
D&D, H&S, CU or CT&P, at least that I could understand. So have done
due diligence, I think.
What I'm trying to do is extract information from one file and insert
that information into other files. The first file has a string of
records, each of which have an ID and for each, there exists another
file. For each record, information is extracted and inserted into the
relevant file. There can be any number of pieces of information for
each record, and the information in each case is extracted as a string.
So I figured that I need an array of pointers to each string that can be
created dynamically for each record as it is read. For the insertion,
the relevant file is read into a temp file, where the extracted strings
are inserted appropriately, and then the relevant file is removed and
replaced (rename()) by the temp file. This is completed for each record
before moving on to the next.
Works like a charm, but handling the array was a problem.
The following code is an example of my undoubtedly very ugly solution.
I can't figure out why it works, nor can I comprehend what the code
should be. I was paying attention to freeing allocated memory in a
timely manner, which affected the solution somehow.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *line1 = "This is line one.";
const char *line2 = "This is line two.";
const char *line3 = "This is line three.";
const char *line4 = "This is line four.";
char *array[4]
char *ptr;
printf("\nThis is the individual print-outs:\n");
ptr = malloc(sizeof line1);
strcpy(ptr, line1);
array[0] = malloc(sizeof line1);
array[0] = ptr;
free(ptr);
array[0] = malloc(sizeof line1);
strcpy(array[0], line1);
printf("array[0] is: %s\n", array[0]);
ptr = malloc(sizeof line2);
strcpy(ptr, line2);
array[1] = malloc(sizeof line2);
array[1] = ptr;
free(ptr);
array[1] = malloc(sizeof line2);
strcpy(array[1], line2);
printf("array[1] is: %s\n", array[1]);
ptr = malloc(sizeof line3);
strcpy(ptr, line3);
array[2] = malloc(sizeof line3);
array[2] = ptr;
free(ptr);
array[2] == malloc(sizeof line3);
strcpy(array[2], line3);
printf("array[2] is: %s\n", array[2]);
ptr = malloc(sizeof line4);
strcpy(ptr, line4);
array[3] = malloc(sizeof line4);
array[3] = ptr;
free(ptr);
array[3] == malloc(sizeof line4);
strcpy(array[3], line4);
printf("array[3] is: %s\n", array[3]);
printf("\nThis is the main body print loop:\n");
for (i=0; i<4; ++i)
printf("array[%d] is %s\n", i, array);
printit();
for (i=0; i<4; ++i)
free(array);
free(ptr);
return 0;
}
void printit() {
int i;
printf("\nThis is the 'printit()' function print loop:\n");
for(i=0; i<4; ++i)
printf("array[%d] is: %s\n", i, array);
}
Running this produces:
This is the individual print-outs:
array[0] is: This is line one.
array[1] is: This is line two.
array[2] is: This is line three.
array[3] is: This is line four.
This is the main body print loop:
array[0] is This is line one.
array[1] is This is line two.
array[2] is This is line three.
array[3] is This is line four.
This is the 'printit()' function print loop:
array[0] is: This is line one.
array[1] is: This is line two.
array[2] is: This is line three.
array[3] is: This is line four.
Delete or change any part of the array handling sections and something
doesn't work correctly.
What should the code be to produce the above results?
Thanks all,
Longfellow