C Programming Read and Write using Array - c

I have a program in which I am supposed to add in two functions that will read elements from an array and write them to a file, and to read from a file and put the information into the array.
Questions:
saveFile(char *fileName); The saveFile function will read each element in the array (the person directory) and save the data into a disk file.
loadFile(char *fileName); The loadFile function will read each item in the disk file and load it into the array (the person directory). The loadFile must generate exactly the same data structure and same data that the saveFile function reads.
Add necessary code to call the safeFile and loadFile functions in your program, including: Declare a global file name myDatabase; Call the saveFile(myDatabase) before existing the main function to save the data; Call the loadFile(myDatabase) at the beginning of the main function to load the data back into the array of person directory
So far this is what I have come up with:
void saveFile(char *fileName) {
FILE *fPointer;
fPointer = fopen(fileName, "w");
fprintf(fPointer, directory);
fclose(fPointer);
}
void loadFile(char *fileName) {
FILE *fPointer;
fPointer = fopen(fileName, "r");
while (!feof(fPointer)) {
fgets(directory, MAX, fPointer);
puts(directory);
fclose(fPointer);
}
}
I only have a basic understanding of reading and writing files. My program complies fine, but whenever I run it a file that should contain the values in the array is not created. If someone can explain how I could use my above code to properly implement it with the code that is given to me that would be great!
Here is the entire code:
/**
Course: CSE240
Instructor: Dr. Chen
Assignment Name: Homework 4 Solution
Solved by: Garrett Gutierrez 2/7/2015
**/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable: 4996)
#define MAX 100
// Changed: deploma to diploma.
typedef enum { diploma = 0, bachelor, master, doctor } education;
// A struct to hold attributes of a person
struct person {
char name[30];
char email[30];
int phone;
education degree;
};
/******************* Global Variable Section *******************/
struct person directory[MAX]; // an array of structures, 100 entries
int tail = 0; // global variable
/******************* Foward Declaration Section *******************/
void branching(char c);
int delete_person();
void flush();
int insertion();
int print_person(int i);
int print_all();
int search_person();
void shift_data(char* name, char* email, int phone, education educationLevel);
int main()
{
// Print a menu for selection
char ch = 'i';
ungetc('\n', stdin); // Inject the newline character into input buffer
do {
printf("Enter your selection\n");
printf("\ti: insert a new entry\n");
printf("\td: delete an entry\n");
printf("\ts: search an entry\n");
printf("\tp: print all entries\n");
printf("\tq: quit \n");
flush(); // Flush the input buffer. To be discussed later
ch = tolower(getchar()); // Convert any uppercase char to lowercase.
branching(ch);
} while (ch != 113); // 113 is 'q' in ASCII
return 0;
};
// Flush the input buffer. To be discussed later
void flush()
{
int c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
};
// Branch to different tasks: insert a person, search for a person, delete a person
// print all added persons.
void branching(char c)
{
switch (c) {
case 'i':
insertion();
break;
case 's':
search_person();
break;
case 'd':
delete_person();
break;
case 'p':
print_all();
break;
case 'q':
break;
default:
printf("Invalid input\n");
}
};
// Inserts the person lexigraphically. Note: A < a so all capital letters will be ordered first.
int insertion()
{
education educationLevel = 0;
char name[MAX], email[MAX];
int i = 0, phone;
// Case 1: The structure is filled.
if (tail == MAX) {
printf("There are no more places to insert.\n");
return -1;
}
// Case 2: The structure still has unfilled slots.
else
{
printf("Enter the name:\n");
scanf("%s", name);
printf("Enter the phone number:\n");
scanf("%d", &phone, sizeof(directory[tail].phone));
printf("Enter the e-mail:.\n");
scanf("%s", email);
//********** Question 1 ************
do {
printf("Enter the degree: select 0 for diploma, select 1 for bachelor, select 2 for master, or select 3 for doctor:\n");
scanf("%d", &educationLevel);
if (educationLevel < diploma || educationLevel > doctor)
{
printf("Please enter a value from 0 to 3.\n");
}
} while (educationLevel < diploma || educationLevel > doctor);
//**********************************************
//********* Question 2 ************
shift_data(name, email, phone, educationLevel);
//*****************************************
tail++;
printf("The number of entries = %d\n", tail);
}
return 0;
};
// Print the name, e-mail, phone, and education level of one person in the directory
int print_person(int i)
{
printf("\n\nname = %s\n", directory[i].name);
printf("email = %s\n", directory[i].email);
printf("phone = %d\n", directory[i].phone);
//************ Question 1 ******************
switch (directory[i].degree)
{
case diploma:
printf("degree = diploma\n");
break;
case bachelor:
printf("degree = bachelor\n");
break;
case master:
printf("degree = master\n");
break;
case doctor:
printf("degree = doctor\n");
break;
default:
printf("System Error: degree information corruption.\n");
break;
}
//****************************************
return 0;
}
// Print the name, e-mail, phone, and education level of each person in the directory
int print_all()
{
int i;
//Case 1: The structure is empty
if (tail == 0)
{
printf("No entries found.");
}
// Case 2: The structure has at least one item in it
else
{
for (i = 0; i < tail; i++) {
print_person(i);
}
printf("\n");
}
return 0;
};
//********** Question 3 **************
//Find a person by comparing names.
int search_person()
{
char sname[30];
int i = 0;
struct person* iterator = directory;
printf("Please enter the name to be searched for:\n");
scanf("%s", sname); //sname is an array, no & needed
while (i < tail)
{
if (strcmp(sname, iterator->name) == 0)
{
print_person(i);
return i;
}
iterator++;
i++;
}
printf("The name does not exist.\n");
return -1;
};
//***************************************
// Delete a person after finding that person via their name.
int delete_person()
{
int i, k;
k = search_person();
// Case 1: The person is not in the directory
if (k == -1)
{
printf("The name does not exist.\n");
return -1;
}
// Case 2: The person was found in the directory
else {
for (i = k; i<tail; i++)
{
strcpy(directory[i].name, directory[i + 1].name);
directory[i].phone = directory[i + 1].phone;
strcpy(directory[i].email, directory[i + 1].email);
printf("The index deleted is: %d\n", k);
}
tail--;
return k;
}
};
void shift_data(char* name, char* email, int phone, education educationLevel)
{
int i = 0, j = 0;
// Case 1: Empty List
if (tail == 0)
{
strcpy(directory[tail].name, name);
strcpy(directory[tail].email, email);
directory[tail].phone = phone;
directory[tail].degree = educationLevel;
return;
}
while (i < tail)
{
// Case 2: Beginning or middle of list
if (strcmp(name, directory[i].name) < 0)
{
j = tail;
while (j > i)
{
strcpy(directory[j].name, directory[j - 1].name);
strcpy(directory[j].email, directory[j - 1].email);
directory[j].phone = directory[j - 1].phone;
directory[j].degree = directory[j - 1].degree;
j--;
}
strcpy(directory[i].name, name);
strcpy(directory[i].email, email);
directory[i].phone = phone;
directory[i].degree = educationLevel;
return;
}
i++;
}
// Case 3: End of list
strcpy(directory[tail].name, name);
strcpy(directory[tail].email, email);
directory[tail].phone = phone;
directory[tail].degree = educationLevel;
};
void saveFile(char *fileName) {
FILE *fPointer;
fPointer = fopen(fileName, "w");
fprintf(fPointer, directory);
fclose(fPointer);
}
void loadFile(char *fileName) {
FILE *fPointer;
fPointer = fopen(fileName, "r");
while (!feof(fPointer)) {
fgets(directory, MAX, fPointer);
puts(directory);
fclose(fPointer);
}
}

fprintf is not the correct function to be calling to output structures to a file, and you're using it wrongly anyway.
It requires a format string after the file handle which decides how the items in the rest of the call will be formatted.
The function you want is fwrite (and fread to get them back). These read and write the underlying data in full.
For example, to write the entire directory structure array, you would use something like:
size_t written = fwrite (directory, sizeof (directory), 1, fPointer);
if (written != 1) puts ("Something went horribly wrong");
And reading it back is pretty much the same except using fread instead of fwrite.
That's probably the easiest approach but, since it reads and writes the entire array, it's not really the most efficient in terms of space taken on disk. You could do it piecemeal, one record at a time but that introduces code complexity and is probably not really necessary for a 100-element array.
If you opt for storing the entire array regardless, you'll probably want to save away tail as well so you can remember how many items in the array are in use:
size_t written = fwrite (&tail, sizeof (tail), 1, fPointer);
if (written != 1) puts ("Something went horribly wrong");

void loadFile(char *myDatabase)
{
FILE *fileBuffer = fopen(myDatabase, "rb");
if (fileBuffer != NULL)
{
fread(&tail, sizeof(tail), 1, fileBuffer);
for (int i = 0; i < tail; i++)
{
fread(directory[i].name, sizeof(directory[i].name), 1, fileBuffer);
fread(&directory[i].phone, sizeof(&directory[i].phone), 1, fileBuffer);
fread(directory[i].email, sizeof(directory[i].email), 1, fileBuffer);
fread(&directory[i].degree, sizeof(&directory[i].degree), 1, fileBuffer);
}
fclose(fileBuffer);
}
This is what I have for the same code..

Related

How to stop reading a file once a certain string appears in c

I'm having trouble figuring out how to get my program to stop reading a file once the character string endOfFileMarker "***" is read given a file called "studentRecords.txt" with sample input as shown:
23456770,Mina,Porter,3,ENEE,114,CMSC,412,ENME,515
23456790,Alex,Simpson,1,CMSC,412
***
I'm reading the file using a while loop indicating that as long the file is not equal to the end of the file and if the first input from which I read is not equivalent to the endOfFileMarker. Right now the output doesn't stop reading at the endOfFileMarker and takes it as a new record in the structure with the given output of a display function (I realize the error with the 2nd record but that appears to be a problem with the display function and not the way I'm storing it):
23456770 Mina Porter 3 ENEE 114 CMSC 412 ENME 515
23456Alex Alex Simpson 1 CMSC 412
*** Alex Simpson 1 CMSC 412
I've tried using fgets earlier and creating an input buffer to read each line. But since there will be variable number of course names and course codes for each student, I found fscanf and using a while loop with control condition of !feof to work better. Kind of at a loss right now of how to stop storing into the structure once I hit the endOfFileMarker. If someone can please help me out with this, that would be very appreciated. My full code is written below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define filename "studentRecords.txt"
typedef struct courseInfo
{//structure defining courseInfo elements
int courseID;
char courseName[30];
}crsInfo;
typedef struct studentInfo
{//structure defining studentInfo elements
char studentID[9];
char firstName[20];
char lastName[25];
int coursesAttended;
crsInfo cInfo[10];
struct studentInfo * next;
}stdInfo;
stdInfo * firstStdNodePointer = NULL;
stdInfo * currentStdNodePointer = NULL;
void addStudentInfo(stdInfo newStd)
{
if (firstStdNodePointer == NULL) //Create the first course node
{
firstStdNodePointer = (stdInfo *) malloc(sizeof(stdInfo));
strcpy(firstStdNodePointer->studentID, newStd.studentID);
strcpy(firstStdNodePointer->firstName, newStd.firstName);
strcpy(firstStdNodePointer->lastName, newStd.lastName);
firstStdNodePointer->coursesAttended = newStd.coursesAttended;
for(int i = 0; i < newStd.coursesAttended; i++)
{
firstStdNodePointer->cInfo[i].courseID = newStd.cInfo[i].courseID;
strcpy(firstStdNodePointer->cInfo[i].courseName, newStd.cInfo[i].courseName);
}
firstStdNodePointer->next = NULL;
currentStdNodePointer = firstStdNodePointer;
}
else // add next course to the end of the course linked list.
{
// Go to the last Course in the list to get the course ID
stdInfo * newStdNodePointer = (stdInfo *) malloc(sizeof(stdInfo));
strcpy(newStdNodePointer->studentID, newStd.studentID);
strcpy(newStdNodePointer->firstName, newStd.firstName);
strcpy(newStdNodePointer->lastName, newStd.lastName);
newStdNodePointer->coursesAttended = newStd.coursesAttended;
for(int j = 0; j < newStd.coursesAttended; j++)
{
newStdNodePointer->cInfo[j].courseID = newStd.cInfo[j].courseID;
strcpy(newStdNodePointer->cInfo[j].courseName, newStd.cInfo[j].courseName);
}
newStdNodePointer->next = NULL;
currentStdNodePointer->next = newStdNodePointer; // Link previous node with newNode
currentStdNodePointer = currentStdNodePointer->next; // Make current node as previous node
}
}
void loadStudentInfo()
{
FILE * fptr = NULL;
fptr = fopen(filename, "r+");
const char endOfFileMarker[] = "***"; //marks the end of the student record list
if(fptr == NULL)
{
printf("File can not be opened\n");
}
stdInfo newStd;//defining a new struct studentInfo variable so I can pass to the addStudent function
//char line[100] = "";
//char * strPtr;
while (!feof(fptr) && strcmp(newStd.studentID, endOfFileMarker) != 0 )
{
fscanf(fptr, "%[^,],", newStd.studentID);
printf("%s\n", newStd.studentID);
fscanf(fptr, "%[^,],", newStd.firstName);
printf("%s\n", newStd.firstName);
fscanf(fptr, "%[^,],", newStd.lastName);
fscanf(fptr, "%i,", &newStd.coursesAttended);
for(int j = 0; j < newStd.coursesAttended; j++)
{//To read each courseName and ID, you need to go according to how many courses they entered
//because the amount of records in cInfo should correspond with how many pairs of courseName
//are entered into the file
fscanf(fptr, "%[^,],", newStd.cInfo[j].courseName);
fscanf(fptr, "%i,", &newStd.cInfo[j].courseID);
}
addStudentInfo(newStd);
}
fclose(fptr);
}
void displayCourseInfo()
{
printf("------------------------------------------------\n");
stdInfo * stdListPointer = firstStdNodePointer;
//start from the beginning
while(stdListPointer != NULL) {
printf("%s %s %s\t%i\t", stdListPointer->studentID, stdListPointer->firstName, stdListPointer->lastName, stdListPointer->coursesAttended);
for(int i = 0; i < stdListPointer->coursesAttended; i++)
{
printf(" %s %i ", stdListPointer->cInfo[i].courseName, stdListPointer->cInfo[i].courseID);
}
printf("\n");
stdListPointer = stdListPointer->next;
}
printf("------------------------------------------------\n");
}
void switchCaseMenu()
{
int selection;
int menuActive = 1;
while(menuActive)
{
printf("60-141 Bonus Assignment - Ben John\n");
printf("------------\n");
printf("1. Add a new student\n");
printf("2. Delete a student\n");
printf("3. Search for a student\n");
printf("4. Display current students\n");
printf("5. Save student information to file\n");
printf("6. Exit\n");
printf("Please enter a selection: ");
scanf("%i", &selection);
switch(selection)
{
case 1:
printf("~Selected - Add a new student~\n");
break;
case 2:
printf("~Selected - Delete a student~\n");
break;
case 3:
printf("~Selected - Search for s student~\n");
break;
case 4:
printf("~Selected - Display current students~\n");
displayCourseInfo();
break;
case 5:
printf("~Selected - Save student information to file~\n");
break;
case 6:
printf("~Selected - Exit~\n");
menuActive = 0;
break;
default:
printf("Invalid Input!\n");
}
}
printf("Goodbye!\n");
}
int main(void)
{
loadStudentInfo();
switchCaseMenu();
return 0;
}
I'll suggest that you read the file line by line using fgets and use sscanf to do the scanning. Then you can use strcmp to break the loop. Something like:
while(fgets(buffer, SIZE_OF_BUFFER, fileptr))
{
size_t len = strlen(buffer);
if (len > 0 && buffer[len-1] == '\n') buffer[len - 1] = '\0'; // Strip \n if present
if (strcmp(buffer, "***") == 0) break; // Stop reading
// use sscanf on buffer to find the individual fields in the line
}
Note that fgets also stores the \n character (aka newline) into the buffer so before doing the string compare, the \n is stripped off (if present).
For your use case you don't really need to test whether the last character in the string is actually a \n. Just make the buffer sufficiently large and always strip off the last character. In this way the code can be simplified to:
while(fgets(buffer, SIZE_OF_BUFFER, fileptr))
{
size_t len = strlen(buffer);
if (len) buffer[len - 1] = '\0'; // Strip last character
if (strcmp(buffer, "***") == 0) break; // Stop reading
// use sscanf on buffer to find the individual fields in the line
}
or an even more compact way (thanks to #melpomene):
while(fgets(buffer, SIZE_OF_BUFFER, fileptr))
{
buffer[strcspn(buffer, "\n")] = '\0'; // Strip \n character if present
if (strcmp(buffer, "***") == 0) break; // Stop reading
// use sscanf on buffer to find the individual fields in the line
}

Reading a binary file and show the entries [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I may need your help to solve this issue...
I'm learning about C and find a problem, how to properly read a binary file.
I have a struct array filled, then wrote on binary file and try to read it, but this isn't show nothing.
The code is in pastebin
My code
#include <stdio.h>
#include <ctype.h>
#include <math.h>
typedef struct {
int id;
char name[100];
char document[14];
float testGrade;
} Candidate;
typedef struct {
int id;
float testGrade;
} Grade;
FILE * openFile(char filename[100], char filemode[3]) {
FILE *p = fopen(filename, filemode);
if (!p) {
printf("Error to open %s file. \nThe program will be closed.",filename);
exit(1);
}
}
void addCandidate(int newId, int numbersOfNewCandidate, char candidateFilename[100], char gradeFilename[100]) {
int counter = 0;
float testGrade = 0;
Candidate*candidate;
candidate= malloc(sizeof(Candidate) * numbersOfNewCandidate);
Grade *grade;
grade= malloc(sizeof(Grade) * numbersOfNewCandidate);
for(;counter< numbersOfNewCandidate; counter++) {
system("#cls||clear");
printf("Adding #%d:",newId);
printf("\n---------------\n");
printf("\nName of candidate: ");
gets(&candidate[counter].name);
printf("\nDocument: ");
gets(&candidate[counter].document);
do {
printf("\nTest grade (between 0 and 10): ");
scanf("%f",&testGrade);
printf("\n---------------\n");
if (testGrade < 0 || testGrade > 10) {
printf("\nERROR!\nTest grade %.2f invalid, please try again with a grade between 0 and 10.\n",testGrade);
}
} while(testGrade < 0 || testGrade > 10);
candidate[counter].id = newId;
grade[counter].id = newId;
candidate[counter].testGrade = testGrade;
grade[counter].testGrate = testGrade;
newId++;
fflush(stdin);
}
FILE *candidateFile = openFile(candidateFilename, "a+b");
fwrite(candidate, sizeof(candidate),numbersOfNewCandidate, candidateFile );
FILE *gradeFile = openFile(gradeFilename, "a+b");
fwrite(grade, sizeof(grade),numbersOfNewCandidate, gradeFile );
fclose(candidateFile);
fclose(gradeFile);
free(candidate);
free(grade);
}
void showCandidate(int typeOfSearch, char valueToSearch[100]) {}
void listAllCandidates(char candid[100]) {
FILE *fp = openFile(candidateFilename, "rb");
//fseek(fp,0,SEEK_SET);
Candidate *candidate = NULL;
candidate = malloc(sizeof(Candidate) + 1);
while(fread(&candidate,sizeof(Candidate),1,fp) == 1) {
printf("\n\nId: %d \nName: %s \nDocument: %s \nGrade: %.2f",candidate->id,candidate->name,candidate->document, candidate->testGrade);
}
getche();
free(candidate);
}
void main(){
int lastId = 0;
char candidateFilename[100] = "candidates.bin";
char gradeFilename[100] = "classificationList.bin";
char option;
do {
system("#cls||clear");
printf("Menu: \n");
printf("1 - Add candidates \n");
// printf("2 - Search by name \n");
// printf("3 - Search by document \n");
// printf("---------------------------\n");
// printf("4 - Show Max Grade, Minimum, Avg \n");
printf("5 - List candidates \n");
printf("6 - Erase files \n");
printf("---------------------------\n");
printf("S - Exit \n");
printf("\n\n");
option = toupper(getche());
switch(option) {
case '1':
system("#cls||clear");
int numbersOfNewCandidate = 0;
int newId = 0;
printf("Home > Add candidates\n\n");
printf("Please give the number of new candidates: ");
scanf("%d",&numbersOfNewCandidate);
newId = lastId;
lastId += numbersOfNewCandidate;
fflush(stdin);
addCandidate(newId + 1, numbersOfNewCandidate, candidateFilename, gradeFilename);
printf("\n\nAdding new candidates: Finished \n");
break;
// case '2':
// printf("\noption %c#\n",option);
// break;
// case '3':
// printf("\noption %c#\n",option);
// break;
// case '4':
// printf("\noption %c?\n",option);
// break;
case '5':
listAllCandidates(candidateFilename);
break;
case '6':
remove(candidateFilename);
remove(gradeFilename);
printf("\nRemoved!!\n");
break;
case 'S':
printf("\noption %c, the program will be ended...\n",option);
break;
default:
printf("\nWrong option!!\n");
break;
}
} while (option != 'S');
}
Please, if you find other issues about my code, try to elucidate me about it..
I already tryed it too, but nothing yet.
while(!feof(fp)) {
fread(&candidate,sizeof(Candidate),1,fp);
printf("\n\nId: %d \nName: %s \nDocument: %s \nGrade: %.2f",candidate->id,candidate->name,candidate->document, candidate->testGrade);
}
Candidate *candidate = NULL;
candidate = malloc(sizeof(Candidate) + 1);
while(fread(&candidate,sizeof(Candidate),1,fp) == 1) {...}
free(candidate);
The first parameter in fread should be a pointer fread(void*,...), and candidate is already declared as a pointer, it should not have a reference operator. The correct usage is:
while(fread(candidate,sizeof(Candidate),1,fp) == 1) {...}
Note that there is no reference & operator in front of candidate
Sometime you see a reference operator, but that's a different case like this:
Candidate cand;
while(fread(&cand,sizeof(cand),1,fp) == 1) {...}
This is an easier method because cand does not need to be allocated with malloc and it does not need free
Unnecessary functions introduce more errors:
FILE * openFile(char filename[100], char filemode[3]) {
FILE *p = fopen(filename, filemode);
if (!p) {
printf("Error to open %s file. \nThe program will be closed.",filename);
exit(1);
}
}
This function is supposed to return a file pointer. Also function parameter can be simply written as const char* filename and const char *filemode. Example:
FILE * openFile(const char* filename, const char* filemode) {
FILE *p = fopen(filename, filemode);
if (!p) printf("Error to open %s file. \nThe program will be closed.",filename);
return p;
}
I would get rid of this function altogether because it's basically useless. Just use fopen. Make sure to close the handle with fclose when you are done.
grade[counter].testGrate = testGrade;
^
This is a typo, it should be grade[counter].testGrade. It is recommended to compile the program with warning level set to maximum, or at least set to level 4. The compiler will tell about about the typos, errors and warnings. You have to be able to compile the program with zero errors and zero warnings. Here is a simple version:
void listAllCandidates(char* candidateFilename)
{
FILE *fin = fopen(candidateFilename, "rb");
Candidate cand;
while(fread(&cand, sizeof(cand), 1, fin))
printf("%s, %s, %d, %f\n", cand.name, cand.document, cand.id, cand.testGrade);
fclose(fin);
}
int main()
{
char candidateFilename[] = "file.bin";
FILE *fout = fopen(candidateFilename, "ab");
Candidate cand;
strcpy(cand.name, "name1");
strcpy(cand.document, "document1");
cand.id = 1;
cand.testGrade = 1.1f;
fwrite(&cand, sizeof(cand), 1, fout);
strcpy(cand.name, "name2");
strcpy(cand.document, "document2");
cand.id = 2;
cand.testGrade = 2.2f;
fwrite(&cand, sizeof(cand), 1, fout);
fclose(fout);
listAllCandidates(candidateFilename);
printf("\n");
return 0;
}

How do I add a contact to a phonebook program in C?

For my intro to programming class, we have to code a phonebook in C that lets users add contacts, as well as delete and display them. It also has to allocate and free memory as necessary (I tried to do this, but I honestly don't really know what I'm doing).
Anyway, I cannot figure out how to add a contact to the phonebook. I've pasted the relevant part of the program so far. It compiles, but it crashes every time I try to add a contact. Once I get this figured out, I think I can get the rest of the functions without too much trouble. If anyone could help me out, I'd really appreciate it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct entry {
char fname[20];
char lname[20];
char pnumber[20];
} entry;
// function prototypes
void addentry(int, entry*, char addfname[20], char addlname[20], char addpnumber[20]);
main() {
int selection = 0;
int inputtest = 1;
int pnum = 0; // keeps track of number of contacts
char addfname[20] = { '\0' };
char addlname[20] = { '\0' };
char addpnumber[20] = { '\0' };
entry *pcontacts;
pcontacts = (entry*)calloc(1, (sizeof(entry)));
if (pcontacts == NULL) {
printf("No memory is available.");
free(pcontacts);
return 0;
}
while (1) {
do {
printf("\nPhonebook Menu\n\n");
printf("1:\tAdd contact\n");
printf("2:\tDelete contact\n");
printf("3:\tDisplay contacts\n");
printf("4:\tExit\n");
printf("\nChoose an action (1-4): ");
scanf("%d", &selection);
if (selection < 1 || selection > 4) {
printf("Invalid input. Please enter an integer between 1 and 4.\n");
inputtest = 0;
}
if (selection == 4) {
free(pcontacts);
printf("\nThank you for using this phonebook.");
return 0;
}
switch (selection) {
case 1:
pnum++;
printf("\nEnter first name: ");
scanf("%s", addfname);
printf("Enter last name: ");
scanf("%s", addlname);
printf("Enter phone number (no spaces): ");
scanf("%s", addpnumber);
addentry(pnum, pcontacts, addfname[20], addlname[20], addpnumber[20]);
break;
}
} while (inputtest == 1);
}
}
void addentry(int pnum, entry *pcontacts, char addfname[20], char addlname[20], char pnumber[20]) {
pcontacts = (entry*)malloc(pnum * (sizeof(entry)));
if (pcontacts != NULL) {
strcpy(*pcontacts[pnum - 1].fname, addfname);
printf("\nContact has been added.");
} else {
printf ("No memory is available.\n");
}
}
You get strings from standard input with scanf, but you should tell scanf the maximum number of bytes to store to the destination arrays to avoid buffer overruns:
scanf("%19s", addfname);
...
scanf("%19s", addlname);
...
scanf("%19s", addpnumber);
The way you call addentry is incorrect:
addentry(pnum, pcontacts, addfname[20], addlname[20], addpnumber[20]);
You actually try to read the byte just after the end of addfname, addlname and addpnumber. You should instead pass the arrays themselves, that will be passed to the function addentry as pointers to their first bytes:
addentry(pnum, pcontacts, addfname, addlname, addpnumber);
addentry should reallocate the array with realloc. It should be passed a pointer to the array pointer to it can update the pointer in main.
addentry does not copy the strings correctly: it only copies one, but with a syntax error.
Here is a corrected version:
void addentry(int, entry**, char addfname[20], char addlname[20], char addpnumber[20]);
int main(void) {
int selection = 0;
int inputtest = 1;
int pnum = 0; // keeps track of number of contacts
char addfname[20];
char addlname[20];
char addpnumber[20];
entry *pcontacts = NULL;
for (;;) {
do {
printf("\nPhonebook Menu\n\n");
printf("1:\tAdd contact\n");
printf("2:\tDelete contact\n");
printf("3:\tDisplay contacts\n");
printf("4:\tExit\n");
printf("\nChoose an action (1-4): ");
scanf("%d", &selection);
if (selection < 1 || selection > 4) {
printf("Invalid input. Please enter an integer between 1 and 4.\n");
inputtest = 0;
}
if (selection == 4) {
free(pcontacts); /* OK for NULL */
printf("\nThank you for using this phonebook.");
return 0;
}
switch (selection) {
case 1:
printf("\nEnter first name: ");
scanf("%19s", addfname);
printf("Enter last name: ");
scanf("%19s", addlname);
printf("Enter phone number (no spaces): ");
scanf("%19s", addpnumber);
addentry(pnum, &pcontacts, addfname, addlname, addpnumber);
pnum++;
break;
}
} while (inputtest == 1);
}
}
/* add an entry at position pnum */
void addentry(int pnum, entry **pp, char addfname[20], char addlname[20], char pnumber[20]) {
entry *pcontact = *pp;
pcontacts = realloc(pcontacts, (pnum + 1) * sizeof(entry));
if (pcontacts != NULL) {
*pp = pcontacts; /* update pointer in main */
strcpy(pcontacts[pnum].fname, addfname);
strcpy(pcontacts[pnum].lname, addlname);
strcpy(pcontacts[pnum].pnumber, addpnumber);
printf("\nContact has been added.");
} else {
printf ("No memory is available.\n");
}
}

Loading a text file into a struct using C

I try to load the contents of a text file into a structure.
My idea looks like this:
I have two files, struct.h , main.c and a list.txt file .
in file struct.h :
struct analg {
char word[6];
char signature[6];
};
struct analg h[106];
FILE *fp;
In file main.c :
#include<stdio.h>
#include "struct.h"
void load() {
fp = fopen("list.txt", "r");
if(fp == NULL) {
printf("fail");
return 1;
}
else {
printf("file loaded!\n");
}
fclose(fp);
return;
}
void print() {
int i;
for(i=0; i<1000; i++) {
while(fgets(h[i].word, 6, fp)) {
printf("%s", h[i].word);
}
}
return;
}
int main () {
int choice;
do {
printf("choose L or P: ");
scanf("%s", &choice);
switch(choice) {
case 'l':
load();
printf("\n[l]oad - [p]rint\n");
break;
case 'p':
print();
printf("\n[l]oad - [p]rint\n");
break;
default:
break;
}
} while(choice!='q');
return;
}
In file list.txt :
throw
timer
tones
tower
trace
trade
tread
So I try to load the text file by pressing the "L" to the structure, and then when I press the 'p' will be displayed, but it is not!
In your code, I see there are 2 potential issues. The choice has to be a character to be switched based on l or p. You may have to add cases to handle the upper case also.
Another issue is that in load function, you are closing the file pointer. Hence, when you enter the print function fgets may not work as the fp is already closed.
To load your file into structure, the load has to be modified as
void load() {
fp = fopen("list.txt", "r");
if(fp == NULL) {
printf("fail");
return; // There was an error in original code as this was returning 1
}
do{
fgets(h[count++].word, 6, fp); // Count is a global variable - no. of elements read
}while(!feof(fp));
printf("file loaded!\n");
fclose(fp);
return;
}
The corresponding print function would become
void print(){
int i;
printf("Inside print\n");
for(i=0; i < count; i++) {
printf("%s", h[i].word);
}
return;
}
the main function would be,
int main (){
char choice;
do{
printf("choose L or P: ");
scanf("%c", &choice); //Only character is read and hence, %s is not required
switch(choice){
case 'l':
load();
printf("\n[l]oad - [p]rint\n");
break;
case 'p':
print();
printf("\n[l]oad - [p]rint\n");
break;
default:
case 'q':
break;
}
} while(choice !='q');
return 0;
}
One last point. In the scanf statement if scanf("%s", &choice); is employed, then a runtime check error is generated when main exits, with a message that stack is corrupted around the variable choice.
I'll comment up what your code is doing:
void load() {
fp = fopen("list.txt", "r"); // opens the file for reading
if(fp == NULL) {
printf("fail"); // if the file couldn't be opened, return an error
return 1; // (aside: a void function can't return an int)
}
else {
printf("file loaded!\n"); // tell the user that the file was opened
}
fclose(fp); // close the file, having read nothing from it
return;
}
At no point do you read anything from the file. What you have in memory will therefore have no relation to whatever you have on disk.
C has no built-in means for serialising and deserialising structs so what you need to do is define a formal grammar for your file on disk and write code that can parse that grammar into your structs.

Reading and Writing to a file in C

I am slowly learning C, but not very well. I have been reading over the countless topics and questions on reading and writing, but I have yet to be able to find anything that makes this all click for me.
I was given the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
struct YouTubeVideo {
char video_name[1024]; // YouTube video name
int ranking; // Number of viewer hits
char url[1024]; // YouTube URL
};
struct YouTubeVideo Collection[MAX];
int tail = 0;
//-- Forward Declaration --//
void printall();
void insertion();
void branching(char option);
void menu();
int main()
{
char ch;
// TODO: Add code to load save data from file
printf("\n\nWelcome to CSE240: YouTube Classic Hits\n");
do {
menu();
fflush(stdin); // Flush the standard input buffer
ch = tolower(getchar()); // read a char, convert to lower case
branching(ch);
} while (ch != 'q');
return 0;
}
void menu()
{
printf("\nMenu Options\n");
printf("------------------------------------------------------\n");
printf("i: Insert a new favorite\n");
printf("p: Review your list\n");
printf("q: Save and quit\n");
printf("\n\nPlease enter a choice (i, p, or q) ---> ");
}
void branching(char option)
{
switch(option)
{
case 'i':
insertion();
break;
case 'p':
printall();
break;
case 'q':
// TODO: Add code to save data into a file
break;
default:
printf("\nError: Invalid Input. Please try again...");
break;
}
}
void insertion()
{
if(tail < MAX)
{
printf("\nWhat is the name of the video? (No spaces characters allowed)\n");
scanf("%s", Collection[tail].video_name);
printf("\nHow many viewer hits does this video have?\n");
scanf("%d", &Collection[tail].ranking);
printf("\nPlease enter the URL: ");
scanf("%s", &Collection[tail].url);
tail++;
}
else
{
printf("\nERROR: Your collection is full. Cannot add new entries.\n");
}
}
void printall()
{
int i;
printf("\nCollections: \n");
for(i = 0; i < tail; i++)
{
printf("\nVideo Name: %s", Collection[i].video_name);
printf("\nRanking (Hits): %d", Collection[i].ranking);
printf("\nURL: %s", Collection[i].url);
printf("\n");
}
}
I am suppose to write the code that will store the collection into a file and the likewise right the code that will load the file and read from it.
Thanks to a fairly helpful TA I was able to formulate the following code for each
void store()
{
FILE * fileName;
fileName = fopen ( "Ranking.dbm" , "wb" );
fwrite ( Collection, sizeof(struct YouTubeVideo), MAX, fileName);
fclose (fileName);
}
and
void read()
{
FILE *fileName;
fileName = fopen("ranking.dbm", "rb");
if (fileName != NULL){
fread ( Collection, sizeof(struct YouTubeVideo), MAX, fileName);
}
else {
printf("ERROR");
}
}
I believe these each to function but the real problem is I don't think I quite understand how and I believe that since I dont even know how they function, I dont know how to use them in the code.
I added both methods to the given code and came up with this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
struct YouTubeVideo {
char video_name[1024]; // YouTube video name
int ranking; // Number of viewer hits
char url[1024]; // YouTube URL
};
struct YouTubeVideo Collection[MAX];
int tail = 0;
//-- Forward Declaration --//
void printall();
void insertion();
void branching(char option);
void menu();
void store();
void read();
int main()
{
char ch;
read();
printf("\n\nWelcome to CSE240: YouTube Classic Hits\n");
do {
menu();
fpurge(stdin); // Flush the standard input buffer
ch = tolower(getchar()); // read a char, convert to lower case
branching(ch);
} while (ch != 'q');
return 0;
}
void menu()
{
printf("\nMenu Options\n");
printf("------------------------------------------------------\n");
printf("i: Insert a new favorite\n");
printf("p: Review your list\n");
printf("q: Save and quit\n");
printf("\n\nPlease enter a choice (i, p, or q) ---> ");
}
void branching(char option)
{
switch(option)
{
case 'i':
insertion();
break;
case 'p':
printall();
break;
case 'q':
store();
break;
default:
printf("\nError: Invalid Input. Please try again...");
break;
}
}
void insertion()
{
if(tail < MAX)
{
printf("\nWhat is the name of the video? (No spaces characters allowed)\n");
scanf("%s", Collection[tail].video_name);
printf("\nHow many viewer hits does this video have?\n");
scanf("%d", &Collection[tail].ranking);
printf("\nPlease enter the URL: ");
scanf("%s", &Collection[tail].url);
tail++;
}
else
{
printf("\nERROR: Your collection is full. Cannot add new entries.\n");
}
}
void printall()
{
int i;
printf("\nCollections: \n");
for(i = 0; i < tail; i++)
{
printf("\nVideo Name: %s", Collection[i].video_name);
printf("\nRanking (Hits): %d", Collection[i].ranking);
printf("\nURL: %s", Collection[i].url);
printf("\n");
}
}
void store()
{
FILE * fileName;
fileName = fopen ( "Ranking.dbm" , "wb" );
if (fileName != NULL)
{
fwrite ( Collection, sizeof(struct YouTubeVideo), MAX, fileName);
fclose (fileName);
}
else {
perror("Following error occurred(): ");
}
}
void read()
{
FILE *fileName;
fileName = fopen("Ranking.dbm", "rb");
if (fileName != NULL)
{
fread ( Collection, sizeof(struct YouTubeVideo), MAX, fileName);
fclose(fileName);
}
else {
perror("Following error occurred with fopen(): ");
}
}
Now I am sure anyone who has read this has probably already face palmed themselves cause they see the problem, but I do not. The code does not create the file to write to and likewise it has nothing to read from so I cant even begin to see what is wrong with that.
Now I am not looking for a given answer, but I would really like to know what it is I am doing incorrectly, what concepts I appear to not understand, and how I can go about fixing these. I have done a few hours of research on this already and I realize it is elementary, but I really would like a hand in learning. Its frustrating spending hours on a topic that the professor said should only take a couple of hours to complete at most.
You really should check the return value of fopen() against NULL - if there is a problem opening the file, it will return NULL and set errno. This is probably a permissions mistake, and by checking the return value and printing the error if one is set, you'll get more information on what went wrong.
Minor: Make sure to check the return values for things like fopen, fread, fwrite etc. more often.
Mild: You've got a potential typo in the filename (some operating systems have case-sensitive file names)
Severe: read() doesn't set up a value for tail... :)
I believe you may have a problem with the read function, which does not call fclose on the file handle (by the way, calling it fileName is a little misleading).
Because you leave the file open, it's entirely likely that you won't be able to overwrite it when you eventually call store. You haven't output any error messages if the file cannot be opened in that function, so it's quite easy to slip under the radar... At least until you wonder why your file date doesn't change.
Otherwise the code looks okay. All it does is dump the entire contents of your array out of memory and read it back in again. What you'll probably want to do is also write out the value of tail, since it is keeping track of how many elements you are keeping. So with minimal code changes, do this:
fwrite ( &tail, sizeof(int), 1, fileName);
fwrite ( Collection, sizeof(struct YouTubeVideo), MAX, fileName);
And of course the corresponding calls to fread in your read method.
fread ( &tail, sizeof(int), 1, fileName);
fread ( Collection, sizeof(struct YouTubeVideo), MAX, fileName);
To reiterate: don't forget to close your file!!!!
fclose(fileName);

Resources