Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A CSV file, Comma Separated Vector, is a file format normally used to implement

ID: 3607382 • Letter: A

Question

A CSV file, Comma Separated Vector, is a file format normally used to implement log files on simple database applications. It is a text file that uses the .csv file extension. Every row of the file is a series of fields separated by commas and terminating with a carrage return. For example, we have a phone book of friends: Bob is 19 his number is 514-123-4567, and Mary is 20 her number is 450-345-7890. This would be stored in the CSV file as: Bob, 19, 514-123-4567 Mary, 20, 450-345-7890 The comma and the carrage return are reserved words and cannot be used in the file to mean anything else. In CSV2, escape characters are used to overcome this limitation, but for this assignment we are not implmenting CSV2. It is optional to include a space after the comma A CSV record is one row of information. For example: Bob, 19, 514-123-4567 is a record. In this case, it is populated by three fields, deliniated by the comma and carrage return: field #1 is Bob, field #2 is 19, and field #3 is 514-123-4567. It is standard to refer to the contents of a CSV file as records and fields Create a program that does the following 1) Ask for a name 2) Ask for a replacement name 3) Search for the record in the CSV file that belongs to name 1 4) Load that record into an array of size 1000 characters 5) Using pointers and no library functions and no array indexes, properly replace the name in the record with the replacement name 6) Then write this updated record back into the CSV file replacing the old record 7) Program terminates In the above, steps 1-2 are in the main function Steps 3-4 are in a function called void FindRecord(char *filename, char name, char recordI), it is invoked from the main function. FindRecord opens the file and reads each record looking for the matching record name. It then returns the matching record in the array record] and closes the file Step 5 is in a function called void Replace(char *name, char *newname, char record]), it is invoked from the main function. It replaces the name in the array recordl with newname. The updated information is returned through the array record]. You must use pointers Step 6 is in a function called void SaveRecord(char filename, char *name, char recordl) and it is invoked from the main function. SaveRecord replaces the record in CSV that matches name

Explanation / Answer

// C Program solution : The function mentioned in the problem has been implemented successfully. The code is working fine . Please compile the code and run it . Don't forget to add phonebook.csv file before compilation . The logic is pretty simple , please go through the code line by line for a better understanding.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// this function first search the name in file and store the paticular row in the array of characters

void findRecord(char *filename,char *name, char record[])
{
FILE* stream = fopen(filename, "r");
char line[1000];
while (fgets(line, 1000, stream))
{
char* tmp = strdup(line);

if(strstr(tmp,name)!=NULL)
{

strcpy(record,tmp);
free(tmp);
break;
}
free(tmp);
}
fclose(stream);
}

// this function replace the oldW to newW in array of charcters , the function returns the pointer to char that contains the record with replacement of oldW to newW

char *replaceName( const char *oldW,
const char *newW, char s[])
{
char *result;
int i, cnt = 0;
int newWlen = strlen(newW);
int oldWlen = strlen(oldW);

// Counting the number of times old word
// occur in the string
for (i = 0; s[i] != ''; i++)
{
if (strstr(&s[i], oldW) == &s[i])
{
cnt++;

// Jumping to index after the old word.
i += oldWlen - 1;
}
}

// Making new string of enough length
result = (char *)malloc(i + cnt * (newWlen - oldWlen) + 1);

i = 0;
while (*s)
{
// compare the substring with the result
if (strstr(s, oldW) == s)
{
strcpy(&result[i], newW);
i += newWlen;
s += oldWlen;
}
else
result[i++] = *s++;
}

result[i] = '';
return result;
}

// this function search the field with given and update the particular with new record

void SaveRecord(char *filename, char *name, char *record){
FILE *fp1 = fopen(filename,"r");
FILE *fp2 = fopen("file.csv", "w");
char line[1000];
while(fgets(line,1000,fp1))
{
if(strstr(line,name)!=NULL)
{
//printf("%s ",record);
fputs(record,fp2);
}
else
fputs(line,fp2);
}
fclose(fp1);
fclose(fp2);
if(remove(filename)==0)
{
int ret = rename("file.csv",filename);
if(ret==0)
printf("Record Updation Done ");
}
}

// Driver Function

int main()
{

char *fileName = "phonebook.csv";
char name[100];
char repName[100];
scanf("%s %s",&name,&repName);
printf("%s %s",name,repName);
char record[1000];
findRecord(fileName,name,record);
char* res = replaceName(name,repName,record);
SaveRecord(fileName,name,res);
return 0;
}

// Please let me know if you have any doubts.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote