In: Computer Science
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be:
2 4
DFJSK
HJ5JF
HFDY5
// PLEASE LIKE THE SOLUTION
// FEEL FREE TO DISCUSS IN COMMENT SECTION
// C PROGRAM
#include<stdio.h>
#include<stdlib.h>
int main(){
// now create file pointer
FILE* fp;
fp = fopen("input.img","r");
// READ H AND W
int h,w;
fscanf(fp,"%d",&h);
fscanf(fp,"%d",&w);
// to store all the array counts
//now find min max and avg
// now read all characters
// and store in array
char array[(h+1)*(w+1)];
// store in array
char ch;
// now row and col
int row=0,col=0;
// index
int ind = 0;
while(1){
ch = fgetc(fp);
if(ch == EOF)
break;
if(ch == '\n')
continue;
array[ind++] = ch;
}
// now 2d array
char array2[h+1][w+1];
row = 0;
for(int i=0;i<=h;i++){
for(int j=0;j<=w;j++){
array2[i][j] =
array[row++];
}
}
// now print the array
for(int i=0;i<=h;i++){
for(int j=0;j<=w;j++){
printf("%c",array2[i][j]);
}
printf("\n");
}
// close file
fclose(fp);
return 0;
}
// SAMPLE OUTPUT