Question

In: Computer Science

C Programming file.c takes in one input argument that denotes a file to be read. It...

C Programming

file.c takes in one input argument that denotes a file to be read. It needs to convert the contents of that file into a character array (char *) and then into a an unsigned character array (unsigned char *).

Please fix and or complete the program, and explain any of the changes too:

----

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
FILE *f; f = fopen(argv[1], "r");
if( !f ) { exit(1); }
char *tok;
char buf[256];
char *save; // saved data
while(fgets(buf, sizeof(buf), f)
{
tok = strtok(buf, "\0");
save = strcat(save, tok);
}
printf("%s", save); // prints contents
// convert save into an unsigned character array (unsigned char *)
}

----

example.txt // sample text to convert, varies in size/length, can have multiple lines

Hello world.
Have a nice day!

----

run the program (in UNIX) as an executable:
file example.txt // takes in one argument only

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

------------main.c-----------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
   if (argc != 2){               //checks if there are 2 command line arguments ./a.out and filename
       printf("Please provide filename as command line argument\n");
       exit(1);
   }

   FILE * f;
   f = fopen(argv[1], "r");  
   if (!f)                       //report if file cannot be opened
   {
       printf("The input file cannot be opened\n");
       exit(1);
   }

   char buf[256];               //buffer to read a line of data from file      
   char *save = malloc(sizeof(char) * 1024);   //allocate space for storing 1024 characters in the array

   while (fgets(buf, sizeof(buf), f))           //read a line into buf
   {
       save = strcat(save, buf);               //append the line into the char array save
   }
   save[strlen(save)] = '\0';                   //set last character as null character
   printf("%s\n", save);   // prints contents

   unsigned char *save2 = malloc(sizeof(unsigned char) * 1024); //allocate space for storing 1024 unsigned characters in the array
   int i;
   for(i = 0; i < strlen(save); i++)       //loop for each index of save array
   {
       save2[i] = save[i];                       //copy the contents from char array save to unsigned char array save2
   }
   save2[i] = '\0';                           //set last character as null character
   printf("\n%s\n", save2);   // prints contents

}

--------------Screenshots-------------------

----------------------Input--------------------------------------

----------------------Output------------------------------------

-------------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Write function words() that takes one input argument—a file name—and returns the list of actual words...
Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input...
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input and returns the product of numbers from 1 to n.
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
In the same module/file, write a function processAnimals that takes one argument, the name of an...
In the same module/file, write a function processAnimals that takes one argument, the name of an input file.   The function returns the list of animals created or an empty list if the file didn't contain any lines. The input file contains zero or more lines with the format: species, language, age where species is a string representing an animal's species, language is a string representing an animal's language, and age is an integer representing an animal's age. The items on...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT IS REQUIRED FOR USE IN THE PROBLEM**** You are to generate a list of customers to serve based on the customer’s priority, i.e. create a priority queue/list for a local company. The company has been receiving request and the request are recorded in a file, in the order the request was made. The company processes each user based on their priority, the highest priority...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT