Computers and Technology
Computers and Technology, 19.03.2021 18:00, o0w0o

The programming language Python has some really nice functions for dealing with files. One of them is called readlines and it reads the lines of the files into an array. For this projcet you will be implementing readlines in C. Additional Details
For this assignment you will be creating a function and not an entire program.
The function your create should have the following signature:
void read_lines(FILE* fp, char*** lines, int* num_lines)​
This function should read all of the lines in the file pointed to by fp and
Set each row of lines to contain one line of the file
Set num_lines to be equal to the number of lines that were in the file
If the file is empty lines should be set to NULL and num_lines to 0
You should only submit read_lines. c and read_lines. h
I will provide main. c and the Makefile
Your code must compile using this Makefile and main. c
You cannot edit main. c
While you only have to write read_lines you can write as many other functions as you want
Hints
I highly recommend making more functions than just read_lines for solving this problem.
For example a function that reads a single line from the file
Examples
User input has been underlined to help you differentiate what is user input and what is program output.
Example 1
./read_lines. out Makefile
read_lines. out: read_lines. o main. o
gcc -g -Wall -Werror -o read_lines. out read_lines. o main. o
main. o: main. c read_lines. h
gcc -g -Wall -Werror -c -o main. o main. c
read_lines. o: read_lines. c read_lines. c
gcc -g -Wall -Werror -c -o read_lines. o read_lines. c
clean:
rm -f *.out *.o
main. c content:
#include
#include
#include "read_lines. h"
void print_lines(char** lines, int num_lines){
int i;
for(i = 0 ; i < num_lines; ++i){
printf("%d. %s", i+1, lines[i]);
}
}
void free_lines(char** lines, int num_lines){
int i;
for(i = 0 ; i < num_lines; ++i){
free(lines[i]);
}
if(lines != NULL && num_lines > 0){
free(lines);
}
}
FILE* validate_input(int argc, char* argv[]){
FILE* fp = NULL;
if(argc < 2){
printf("Not enough arguments entered.\nEnding program.\n");
exit(0);
}
else if(argc > 2){
printf("Too many arguments entered.\nEnding program.\n");
exit(0);
}
fp = fopen(argv[1], "r");
if(fp == NULL){
printf("Unable to open file: %s\nEnding program.\n", argv[1]);
exit(0);
}
return fp;
}
int main(int argc, char* argv[]){
char** lines = NULL;
int num_lines = 0;
FILE* fp = validate_input(argc, argv);
read_lines(fp, &lines, &num_lines);
print_lines(lines, num_lines);
free_lines(lines, num_lines);
fclose(fp);
return 0;
}

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 09:00, jgrable5175
Designing a mobile web page is a little different from designing a regular web page. name at least three features that should be considered when designing a website that is mobile phone-friendly, and briefly explain why they are important.
Answers: 1
image
Computers and Technology, 22.06.2019 14:30, camerondillonn
If the polar bear were taken out of the food chain what would happen to the seal population the seal population would diminish the seal population would grow dramatically the seal population would stay the same the seal population would decrease slightly
Answers: 1
image
Computers and Technology, 23.06.2019 03:00, tay9122
Jason, samantha, ravi, sheila, and ankit are preparing for an upcoming marathon. each day of the week, they run a certain number of miles and write them into a notebook. at the end of the week, they would like to know the number of miles run each day, the total miles for the week, and average miles run each day. write a program to them analyze their data. your program must contain parallel arrays: an array to store the names of the runners and a two-dimensional array of five rows and seven columns to store the number of miles run by each runner each day. furthermore, your program must contain at least the following functions: a function to read and store the runners’ names and the numbers of miles run each day; a function to find the total miles run by each runner and the average number of miles run each day; and a function to output the results. (you may assume that the input data is stored in a file and each line of data is in the following form: runnername milesday1 milesday2 milesday3 milesday4 milesday5 milesday6 milesday7.)
Answers: 3
image
Computers and Technology, 23.06.2019 22:30, cuki96
Lakendra finished working on her monthly report. in looking it over, she saw that it had large blocks of white space. what steps could lakendra take to reduce the amount of white space?
Answers: 3
Do you know the correct answer?
The programming language Python has some really nice functions for dealing with files. One of them i...

Questions in other subjects:

Konu
Social Studies, 21.05.2021 03:00
Konu
Mathematics, 21.05.2021 03:00
Konu
Mathematics, 21.05.2021 03:00