Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

discuss the value of thinking in images.

Answered: 1 week ago