Question

In: Computer Science

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

Solutions

Expert Solution

C Program:

#include <stdio.h>

int main(int argc, char **argv)
{
int n=0; //Variable that holds the number of elements in the file
FILE *fptr; //File Pointer
int val; //For holding value
int *arr; //Pointer array
int i=0;
  
//Opening file for reading
fptr = fopen("C:\\Users\\P.MANUSHA\\Documents\\DMA\\DMA\\ip.txt", "r");
  
//Counting number of elements in the file
while( fscanf(fptr, "%d", &val) == 1 )
{
//Incrementing number of elements
n = n + 1;
}
  
//Allocating Memory
arr = (int*)malloc(sizeof(int)*n);
  
fseek(fptr, 0, SEEK_SET);
  
//Opening file for reading and storing data in file
//fptr = fopen("ip.txt", "r");
  
//Counting number of elements in the file
while( fscanf(fptr, "%d", &val) == 1 )
{
//Storing in array
*(arr+i) = val;
  
//Incrementing array index
i = i + 1;
}
  
//Closing file
fclose(fptr);
  
//Printing array elements
printf("\nElements present in array: ");
  
//Iterating over array
for(i=0; i<n; i++)
{
printf(" %d ", *(arr+i));
}
  
//Releasing memory
free(arr);
  
printf("\n");
return 0;
}

__________________________________________________________________________________________________________________________

Sample Run:


Related Solutions

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
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
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++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
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.
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
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,...
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. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph 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 not do that Paragraph 2 We...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT