My program reads from two files (FirstNames and Lastnames). With the program i have so far, i print a customer ID, and next to it the first name and last name. Now i need to also print the "login ID", which will be the first letter from the first name and all the last name. Here is the program i have so far. I thought it would be easy to make the login id but it's kinda tricky for my level. (if some people have problems compiling it, try declaring the "i" outside the for loops).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char ** argv)
{
if ( argc != 2 )
{
printf("usage: %s no_of_records\n", argv[0]);
exit(1);
}
int nrecords = atoi(argv[1]);
typedef struct test
{
int num; char *firstName; char *lastName;
} CU;
char buf[256];
char * fname;
char * lname;
CU * cup = malloc ( nrecords * sizeof(CU));
CU * cufirst = cup;
FILE * fpfirst;
FILE * fplast;
if ( (fpfirst = fopen("FirstNames", "r")) == NULL)
{
fprintf(stderr, "Error reading file FirstNames");
abort();
}
if ( (fplast = fopen("LastNames", "r")) == NULL)
{
fprintf(stderr, "Error reading file LastNames");
abort();
}
for (int i = 0; i < nrecords; )
{
cup -> num = ++i;
fgets(buf, sizeof(buf), fpfirst); /* get line */
fname = strndup(buf, strlen(buf)-1); /* omit newline */
cup -> firstName = (char *) strdup(fname);
fgets(buf, sizeof(buf), fplast);
lname = strndup(buf, strlen(buf)-1);
cup -> lastName = (char *) strdup(lname);
cup++;
}
cup = cufirst;
for (int i = 0; i < nrecords; i++)
{
printf("%03d\t%s\t\t%s\n", cup -> num, cup -> firstName, cup -> lastName);
cup++;
}
return 0;
}