C Programming Write a program that reads descriptions of 2D shapes from a text f
ID: 3675016 • Letter: C
Question
C Programming
Write a program that reads descriptions of 2D shapes from a text file that is obtained from the user.
Allow up to 20 characters for the filename. Print the information about the shapes in a tabular format
as shown below.
Your code MUST use two structs for this assignment. The first struct will represent a point in a 2D
pixel coordinate space, and will thus hold two integers: one each for the x and y coordinates. The
second struct will represent a shape, and should contain fields for the following information:
type: A char representing the type of shape, valid values are L, R, T, O, and S, representing a
Line, Rectangle, Triangle, Oval, and String, respectively.
color: An int representing the color. You do not need to check for invalid values and can assume
for output that these are no more than 2 digits.
points: An array of 3 of your point structs. A String shape will specify only the first point. Lines,
Rectangles, Triangles, and Ovals will use the first two points. Triangles will use all three
points. You can assume for output that the x and y values are no more than 3 digits
thickness: An int representing the line thickness for the shape. This field is not (presently) used by
String shapes, but is used by others. You may assume for output that this is a single digit.
size: An int representing the font size. This field is only used by String shapes. Don’t try to make
double-duty out of the thickness field in order to skip this field. You may assume 1-2 digits.
text: An array that holds the zero-terminated string for String shapes, for up to 20 chars.Use a
#define for the max size. This field is not be used by other shape types.
If the file fails to open, print an error message and exit. Otherwise, read the shapes from the file,
formatted as follows:
L 0 0 10 20 30 1
R 1 10 20 30 40 2
T 2 20 30 40 50 60 70 3
O 3 30 40 50 60 4
S 4 40 50 12 Hello Mommy! Are you glad to see me?
L 5 50 60 70 80 1
Each line in the above example defines a shape. Each shape is described by entries in the same order as the struct fields listed above, and each shape, except for the last, will end with a linefeed. Your program should allow empty text for strings and should simply ignore the text of any string beyond the maximum 20 chars. The string example above is longer than 20 chars, and is thus truncated (at the end of “you”) in order to fit in the struct field. Unless you pursue extra credit, your program need not deal gracefully with a blank line at the bottom of the file, but it should not require one. Nor will I test the behavior of your program with incorrectly formatted shapes unless you pursue the extra credit. Print the filename and shapes you encounter in a tabular form formatted as follows. Make sure you produce clean column alignments, and print only the values that are used by that shape.
Reading file: shapes.txt
---------------------------------------------------
Type Color x0 y0 x1 y1 x2 y2 thick size text
---------------------------------------------------
L 0 0 10 20 30 1
R 1 10 20 30 40 2
T 2 20 30 40 50 60 70 3
O 3 30 40 50 60 4
S 4 40 50 12 Hello Mommy! Are you
L 5 50 60 70 80 1
Modify your code so that it exits with an error message if it encounters an incorrectly specified shape.
The error message should mention the shape type that it failed on. So something like “Bad Shape
Found (Type X)” where X is the shape code it was trying to parse. A strategy for accomplishing this is
to test whether the count of items scanned matches the shape type.
Modify your code so that it allows, but does not require, any number of blank lines at the end of the
file, without producing any incorrect output or error messages.
Modify your code so that it allows, but does not require, blank lines between shapes and even between
the fields of a shape, in order to allow the human reader some flexibility. For example:
L 0
0 10 20 30
1
S 4
40 50
12
Hello Mommy
That may already be working, except for the possibility of a linefeed between the font size and the text
of a string. The problem is that fgets (see hint below) will grab that newline as an empty string. You’ll
thus want to skip all whitespace, including any ‘ ’, before calling fgets. That can be done by scanning
up to the first non-whitespace character, and then calling ungetc(char, FILE*) to return that character
to the stream.
Explanation / Answer
Comments added ...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//structs declared
struct point{
int x0;
int y0;
}
struct Shape {
char type[50];
int color;
point[3];
int thickness;
int size;
char text[200];
};
struct shape shapes[10];
int main(void)
{
//variables declared
FILE * fp;
char * text = NULL;
char *type;
int color;
int x0,y0,x1,y1,x2,y2,thick,size;
size_t len = 0;
ssize_t read;
int index = 0;
char fileName[20];
printf("Enter file name");
scanf("%s",fileName);
//reading text file
fp = fopen(fileName, "r");
if (fp == NULL)
exit(EXIT_FAILURE);
//reading file ine by line
while ((read = getline(&type,&color, &x0,&y0, &x1,&y1, &x2,&y2,&thick, &size, %text, fp)) != -1) {
//creating struct point array
struct point points[3];
points[0].x0 = x0;
points[0].x1 = y0;
points[1].x0 = x1;
points[1].x1 = y1;
points[2].x0 = x2;
points[2].x1 = y2;
//creating shapes struct
strcpy(shapes[index].type,type);
shapes[index].point = points;
shapes[index].color = color;
shapes[index].thickness = thick;
shapes[index].size = size;
strcpy(shapes[index].text,text);
index++;
}
fclose(fp);
if (line)
free(line);
exit(EXIT_SUCCESS);
//prinitng results.. tabular format
int i;
for(i=0;i<index;i++){
printf(" type , color ,thickness , size , text ");
printf(" %s, %d, %d, %d, %s", shapes[i].type,shapes[i].color,shapes[i].thick,shapes[i].size,shapes[i].text);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.