You may be making things way harder on yourself than you need to. With your struct Person
filled, in order to obtain the output you need, you simply need to output the struct members separated by a comma
and space
. A simple printf
will do, e.g.
struct Person p1 = { "John", "Doe", "831.563.3642" };
...
printf ("%s, %s, %s", p1.first, p1.last, p1.phone);
The output from above would be:
John, Doe, 831.563.3642
You can never do char new[] = "";
-- which creates the array new
of size 1 holding the '\0'
character. (bad choice of names as well, new
is a kewword in C++, its use as a variable name should be avoided). Once you declare and initialize the array with automatic storage duration of size 1 -- it's size is FIXED -- period, and cannot be expanded. You would need to dynamically allocate storage with malloc
in order to be able to resize the storage later with realloc
.
If you want to create an additional array holding the comma separated values, then you need to declare the array with storage to hold the largest first_name, last_name
and phone
initially, e.g. char buf[100 + 100 + 13 + 4];
(the +4 for the pair of ", "
you want to insert as well) Now you can use sprintf
to simply write to the new array.
Below is a short example which shows both the direct output with printf
and the creation of a new array (buf
) using sprintf
. The results is the same regardless of whether you directly output the comma space separated values, or store them in a buffer first. The function csvperson()
takes the destination array and a pointer to a filled struct as parameter to write the members of the struct separated by a comma and space to array, e.g.
#include <stdio.h>
#define MAXNM 100 /* if you need a constant, #define one (or mroe) */
#define MAXPH 13
typedef struct { /* using a typedef allows you to later omit the */
char first[MAXNM], /* 'struct' prefix in your declarations and */
last [MAXNM], /* parameter lists */
phone [MAXPH];
} person;
char *csvperson (char *dest, person *p)
{
if (!dest)
return NULL;
sprintf (dest, "%s, %s, %s", p->first, p->last, p->phone);
return dest;
}
int main (void) {
char buf[2 * MAXNM + MAXPH + 4];
person p1 = { "John", "Doe", "831.563.3642" };
printf ("direct output : %s, %s, %s\nbuffer output : %s\n",
p1.first, p1.last, p1.phone, csvperson (buf, &p1));
}
Example Use/Output
$ ./bin/csvperson
direct output : John, Doe, 831.563.3642
buffer output : John, Doe, 831.563.3642
Look things over and let me know if you have further questions.
Array Example
To use the same methods above with an array, you would do:
int main (void) {
char buf[2 * MAXNM + MAXPH + 4];
person p1[] = { {"John", "Doe", "831.563.3642"},
{"Jane", "Doe", "832.563.3643"},
{"Mary", "Jane", "303.331.3333"} };
int n = sizeof p1 / sizeof *p1;
for (int i = 0; i < n; i++)
printf ("\ndirect output : %s, %s, %s\nbuffer output : %s\n",
p1[i].first, p1[i].last, p1[i].phone, csvperson (buf, p1+i));
}
You can simply use fprintf
to output to a file without buffering the output first, e.g.
fprintf (fileptr, "%s, %s, %s\n", p1[i].first, p1[i].last, p1[i].phone);
In general, don't worry about combining/concatenating/etc.. things you can simply write out (wherever be it a file, the terminal, etc..)