How can I divide those two functions to more than two?
The functions read the file line by line.
An instruction will appear in a line in the file (at the end of each instruction there will be a newline character). At the start of the running, the program will read the instruction line by line. Then it will decode the required action and parameters and will call to perform the action with the appropriate parameters.
I tried to put the foor loop, array, getc()
to another function but it doesn't work.
void read_line(FILE *fp, char *orders, char *book_name, char *book_name_2, int *num)
{
int i = 0;
char c ;
*num = 0;
c = getc(fp);
while ((c != '\n') && (!feof(fp))) {
for (i = 0; (c != '$') && (c != '\n') && (!feof(fp)); i++) {
orders[i] = c;
c = getc(fp);
}
orders[i] = '\0';
if (c != '\n' && (!feof(fp))) {
fseek(fp, 3, 1);
c = getc(fp);
}
else break;
for (i = 0; (c != '$') && (c != '\n'); i++) {
book_name[i] = c;
c = getc(fp);
}
book_name[i] = '\0';
if (c != '\n' && (!feof(fp))) {
fseek(fp, 3, 1);
c = getc(fp);
}
else break;
if (strcmp(orders, "Rename ") != 0) {
for (i = 0; c != '\n'; i++) {
*num = (*num) * 10 + (c - '0');
c = getc(fp);
}
}
else {
for (i = 0; c != '\n'; i++) {
book_name_2[i] = c;
c = getc(fp);
}
book_name_2[i] = ' ';
book_name_2[i + 1] = '\0';
}
return;
}
}
Book* read_file_books(FILE *fp, Book *head, char *book_name, int *copies)
{
int i = 0;
char c ;
*copies = 0;
c = getc(fp);
while ((c != '\n') && (!feof(fp))) {
for (i = 0; (c != '$') && (c != '\n'); i++) {
book_name[i] = c;
c = getc(fp);
}
book_name[i] = '\0';
if (c != '\n') {
fseek(fp, 3, 1);
c = getc(fp);
}
else break;
for (i = 0; (c != '\n') && (!feof(fp)); i++) {
*copies = (*copies) * 10 + (c - '0');
c = getc(fp);
}
return add(head, book_name, *copies);
}
return head;
}