In: Computer Science
Using the C language:
In the old days, data was stored on punched cards of 80 or 96 columns.
Presume your job is to update a database to include the newly
found data in the file punchcard.txt
into a currently used data base. In order to do that, you must read
in the data in columnar format
and convert it to comma separated fields so it can be imported.
Using buffered I/O functions (e.g. fopen, fclose, fprintf,
fscanf, fread or fwrite, fgets, fgets, etc),
read the columnar data in punchcard.txt into your program. It has
the following fields:
unsigned int user_number 6 columns
char name 32 columns
char address 32 columns
Write the data out to a file called import.txt which is of the following CSV format for each line:
user_number,name,address<carriage return>
If the data in name or address contains a comma, it must be
delimited with double quotes around
it. Make sure you remove all extraneous spaces in front of, or
behind the data so that the entry in
the import.txt file has no extraneous spaces which is not part of
the data. This is often called
"trimming" the data.
punchcard.txt contains...
289383Estefana Lewey 9940 Ohio Drv, 85021
930886Burl Livermore 226 Amherst, 08330
692777Lannie Crisler 8143 Woods Drv, 20901
636915Zena Hoke 82 Roehampton St, 47905
747793Vicente Clevenger 9954 San Carlos St., 55016
238335Lidia Janes 348 Depot Ave, 29576
885386Claire Paladino 587 Front Ave, 32703
760492Leland Stillson 9793 Boston Lane, 08610
516649Wes Althouse 8597 Annadale Drive, 06514
641421Nadia Gard 218 George Street, 29150
Thank you in advance.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/*typedef struct
{
unsigned int user_no;
char name[32];
char address[32];
}record;*/
int main(void)
{
FILE *fp, *fp1;
char str[70], buff[7];
char name[32];
char address[32];
unsigned int user_no;
int a, b;
fp = fopen("punchcard.txt", "r");
fp1 = fopen("import.txt", "w");
while (fgets(str, 70, fp) != NULL)
{
/* reading emp no.*/
printf("%s\n", str);
for(a = 0; a <6; a++)
{
buff[a] = str[a];
}
buff[7] = '\0';
user_no = atoi(buff);
printf("Emp No.:%u ", user_no);
/* reading name*/
a = 6;
b = 0;
while(str[a] != ' ')
{
name[b] = str[a];
b++;
a++;
}
name[b] = str[a];
b++;
a++;
while(str[a] != ' ')
{
name[b] = str[a];
b++;
a++;
}
name[b] = '\0';
printf("Emp name:%s ", name);
/* reading address*/
address[0]='"';
a++;
b = 1;
while(str[a] != ',')
{
address[b] = str[a];
b++;
a++;
}
address[b] = '"';
b++;
a++;
while(str[a] != '\0')
{
address[b] = str[a];
b++;
a++;
}
address[b] = '\0';
printf("Address:%s\n", address);
fprintf(fp1, "%u,%s,%s\n",user_no, name, address);
}
fclose(fp);
fclose(fp1);
return 0;
}
The import.txt file attached: