Question

In: Computer Science

Write a c program that reads a .img file by rows and columns and prints out...

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

Solutions

Expert Solution

// 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


Related Solutions

Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into a vector and loop through that vector and find the max number.
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT