Question
Program 2 #include #include #include #include #include #include #include #include struct exa { char x; short int y; }; int randomchar() // This is a
Program 2
#include
#include
#include
#include
#include
#include
#include
#include
struct exa
{
char x;
short int y;
};
int randomchar() // This is a random character function that g
{
int upper = 122;
int lower = 97;
for (int i = 0; i < 31; i++)
{
int num = (rand() % (upper - lower + 1)) + lower;
char c = num;
}
}
int randomnum()
{
int upper = 20;
int lower = 5;
for (int i = 0; i < 31; i++)
{
int num = (rand() % (upper - lower + 1)) + lower;
char c = num;
}
}
int checkError(int val, const char *msg)
{
if (val == -1)
{
perror(msg);
exit(EXIT_FAILURE);
}
return val;
}
int writeFile(const char *file, int localtotalpair1)
{
int fd;
int i,cnt;
int localtotalpair = localtotalpair1;
struct exa val[localtotalpair];
fd = checkError(open(file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR), "open");
for (i = 0; i < localtotalpair; i++)
{
val[i].x = randomchar();
val[i].y = randomnum();
//printf(" ",val[i].x, ": ", val[i].y, ":");
}
cnt = checkError(write(fd, val, 60*sizeof(struct exa)), "write");
close(fd);
return 1;
}
int readFile(const char *file)
{
int fd;
int i, cnt;
struct exa val;
fd = checkError(open(file, O_RDONLY), "open");
while ((cnt = checkError(read(fd, &val, sizeof(struct exa)), "read")) > 0)
{
printf("%c %1u ", val.x, val.y);
}
close(fd);
return 1;
}
int main(int argc, char *argv[])
{
int totalpair;
char file[256];
srand(time(NULL));
if (argc > 1)
{
strcpy(file, argv[1]);
}
else
{
strcpy(file, "image.dat");
}
if (argv != NULL)
{
totalpair = argc;
}
else
{
totalpair = 60;
}
writeFile(file, totalpair);
readFile(file);
exit(EXIT_SUCCESS);
return 0;
}
This program will use the file created in Program 2.
The program should read the integer pairs from the "image.dat" file and display the data read in two forms. The first form is the character - integer pairing and should be displayed as (character, integer). The other form can be thought of as the pairings decoded form; the character - integer pairing ('b',6) would be decoded as 'bbbbbb'. (C-Programming)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started