C PROGRAM: 1. Write code to open a file called adata.out, and write the contents
ID: 3667789 • Letter: C
Question
C PROGRAM:
1. Write code to open a file called adata.out, and write the contents of the following array out to it in binary form (with no data conversion).
int a[5];
2. What would be the best file mode for each of the following cases?
A. reading binary data in an existing file
B. adding new text data to the end of an existing file
C. replacing the current contents of an existing file with new text data, then modifying the new data
3. You are writing a function that has to keep track of the number of times it is called. What storage classes could be used for a variable that could retain this information? Which one would you recommend?
4. Describe how the const qualifier was used to "improve," that is, make more readable and reliable, the program in Listing 14.8, pages pages 554-555.
Explanation / Answer
1)
include <stdio.h>
int main() {
int a[5] = {1,2,3,4,5};
FILE *pfile; /* File pointer to desired file */
int i = 0;
pfile = fopen("adata.out", "ab"); /* opens file append-binary */
/* the following chunk is used to to see if the file opened is empty or not */
if(pfile==NULL)
{
printf("Error: File doesn't exist");
}
else
for(i =0;i<5;i++)
putc(a[i], pfile);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.