Question: This is for C programming: Professor provided this code to understand and review. First: it has the following error and i have no clue how

This is for C programming: Professor provided this code to understand and review.

First: it has the following error and i have no clue how to fix it.

Second: Please explain the code for me so I may understand.

Thank you so much.

ERROR:

This is for C programming: Professor provided this code to understand and

CODE:

#include

#include

#include

int readInt(FILE *fp)

{

int x, result;

result = fscanf(fp, "%d", &x);

if(result == EOF)

{

return 0;

}

if(result == 0)

{

fprintf(stderr,"SCAN ERROR: attempt to read an integer failed ");

fprintf(stderr,"Offending character was ", fgetc(fp));

exit(1);

}

return x;

}

char readChar(FILE *fp)

{

int result;

char x;

result = fscanf(fp, " %c", &x);

if(result == EOF)

{

return EOF;

}

if(result == 0)

{

fprintf(stderr,"SCAN ERROR: attempt to read an integer failed ");

fprintf(stderr,"Offending character was ", fgetc(fp));

exit(1);

}

return x;

}

char readRawChar(FILE *fp)

{

int result;

char x;

result = fscanf(fp, "%c", &x);

if(result == EOF)

{

return EOF;

}

if(result == 0)

{

fprintf(stderr,"SCAN ERROR: attempt to read an integer failed ");

fprintf(stderr,"Offending character was ", fgetc(fp));

exit(1);

}

return x;

}

void skipWhitespace(FILE *fp)

{

int ch;

while((ch = fgetc(fp)) != EOF && isspace(ch))

continue;

if(ch!=EOF)

ungetc(ch, fp);

}

char *readAlphaString(FILE *fp)

{

int ch, index;

char *buffer;

int size = 30;

skipWhitespace(fp);

ch = fgetc(fp);

if(ch == EOF) return 0;

buffer = malloc(sizeof(char) * size);

index = 0;

while(isalpha(ch))

{

if(index > size - 2)

{

++size;

buffer = realloc(buffer, size*sizeof(char));

}

buffer[index] = ch;

++index;

ch = fgetc(fp);

}

ungetc(ch, fp);

if(index > 0)

clearerr(fp);

buffer[index] = '\0';

return buffer;

}

int main()

{

FILE *fp = fopen("test","r");

char charValue;

char *alphaValue;

int integerValue;

while(!feof(fp))

{

charValue = readChar(fp);

while(isspace(charValue))

{

charValue = readChar(fp);

}

if(charValue == '[')

{

alphaValue = readAlphaString(fp);

charValue = readChar(fp);

}

if(charValue == ',')

{

integerValue = readInt(fp);

charValue = readChar(fp);

}

if(charValue == ']')

{

}

printf("%s %d ", alphaValue, integerValue);

}

while(!feof(fp))

{

alphaValue = readAlphaString(fp);

if(!feof(fp))

printf("%s ", alphaValue);

}

fclose(fp);

return 0;

}

n.c -o main /main.c: line 5: syntax error near unexpected token ( /main . c: line 5: int read!nt(FILE *fp)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!