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

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
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
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Write a C++ program that reads a file consisting of students’ test scores in the range...
Write a C++ program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT