Question

In: Computer Science

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

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
long lSize;
char *buffer;

fp = fopen ( "a.txt" , "rb" );
if( !fp ) perror("a.txt"),exit(1);

fseek( fp , 0L , SEEK_END);
lSize = ftell( fp );
rewind( fp );

buffer = calloc( 1, lSize+1 );
if( !buffer ) fclose(fp),fputs("memory alloc fails",stderr),exit(1);

if( 1!=fread( buffer , lSize, 1 , fp) )
fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1);
unsigned char* message =( unsigned char*) buffer;
for (long k= 0; k <lSize; k++)
{
printf("%c",message[k]);
}

fclose(fp);
free(buffer);

return 0;
}

 
 

Related Solutions

C++ Language Write a program that reads the numbers.txt file and stores it into an array...
C++ Language Write a program that reads the numbers.txt file and stores it into an array of integers. Use the sample functions in the text and in the notes from chapter 8 to sort the array in ascending order (low to high). You can use either method (bubble or selection sort) to accomplish this. Then store the sorted array into an output file sorted Numbers.txt. There are 100 values both positive and negative integers in this file.
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt and determines the following: The number of uppercase letters in the file The number of lowercase letters in the file The number of digits in the file Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output, preceded by the following...
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
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
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...
Programming lang C++ Given a file of 100,000 unsorted integers, write a program that reads those...
Programming lang C++ Given a file of 100,000 unsorted integers, write a program that reads those integers into an array and sorts the integers in the array. The program should then prompt the user for an index, and then display the integer at that index. For example, if the array contains ten integers and the contents are 79 4 42 51 12 22 33 17 91 10 after sorting, the integer at index 6 is 42 You are encouraged to...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
In c++ please. Write a program that reads the contents of two text files and compares...
In c++ please. Write a program that reads the contents of two text files and compares them in the following ways: It should display a list of all the unique words contained in both files. It should display a list of the words that appears in both files. It should display a list of the words that appears in the first file, but not the second. It should display a list of the words that appears in the second file,...
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT