求助C语言高手!BT学校居然布置英文作业~
[bold][underline]Week 10 Exercises – [/underline][/bold][bold][underline][font=TimesNewRoman]File Input/Output[/underline][/bold][/font][underline]Objectives[/underline][font=Times New Roman]Design and write programs that read and write data to/from files.[/font]
[underline]Tutorial Assessment[/underline]The students are required to do [bold][underline]ALL[/underline][/bold] exercises. [font=TimesNewRoman]All assessed exercises need to be submitted in the Assessment section of this module under ICE. Include comment lines to explain the code in detail (no design document is needed).[/font]
[underline]Deadlines[/underline][font=TimesNewRoman]The deadline for submitting the exercises is: [bold][italic]Thursday 6th December 2007 at 4pm.[/italic][/bold][bold][/bold][/font]
[bold] [/bold]
[align=center][bold][font=Wingdings]F[/font][/bold][bold][underline]Assessed exercises: 1, 2 & 3[/underline][/bold]
[/align][font=Times New Roman] [/font]
[underline]Exercise 1 (3 points)[/underline]
[font=TimesNewRoman]Write a file copy program that copies an existing file into another file. The program should ask the user to enter the source file name and destination file name. The file copy should be performed in [italic]binary[/italic] mode. Use [italic]fseek()[/italic]
to move within the source file and [italic]ftell()[/italic]
to determine the size of the file (if needed). Read the data from the source file and write the values read in the destination file in a [italic]loop[/italic]. Read and write data in blocks of [italic]1024 bytes[/italic].[/font]
[font=Times New Roman] [/font]
[underline]Exercise 2 (3 points)[/underline]
[font=TimesNewRoman]The following program reads from a [bold]text file [/bold]containing numeric data stored in columns separated by one blank space. Write the code and compile it on your computer. Create a text file called [bold]myfile.txt [/bold]containing a few columns (3[/font][font=Symbol]?[/font][font=Times New Roman]5 for example) of numeric data (real numbers) and test the program.[/font]
[bold][font=Times New Roman]#include <stdio.h>[/font][/bold]
[bold][font=Times New Roman]int main(void)[/font][/bold]
[bold][font=Times New Roman]{[/font][/bold]
[bold][font=Times New Roman]FILE *fp;[/font][/bold]
[bold][font=Times New Roman]double val;[/font][/bold]
[bold][font=Times New Roman]char ch;[/font][/bold]
[bold][font=Times New Roman] [/font][/bold]
[bold][font=Times New Roman]fp = fopen("myfile.txt","r");[/font][/bold]
[bold][font=Times New Roman]do{[/font][/bold]
[bold][font=Times New Roman]fscanf( fp, "%lf", &val );[/font][/bold]
[bold][font=Times New Roman]ch = getc(fp);[/font][/bold]
[bold][font=Times New Roman]printf(" %lf",val);[/font][/bold]
[bold][font=Times New Roman]if ( ch=='\n' || ch==EOF )[/font][/bold]
[bold][font=Times New Roman]printf("\n");[/font][/bold]
[bold][font=Times New Roman]} while ( ch != EOF );[/font][/bold]
[bold][font=Times New Roman]fclose(fp);[/font][/bold]
[bold][font=Times New Roman]return 0;[/font][/bold]
[bold][font=CourierNewPS-BoldMT]}[/bold][bold][/bold][/font]
[font=TimesNewRoman]Analyse the code and include comment lines explaining how the program achieves its task. Modify the code so that the program additionally creates a new text file where it stores only the integer part (discard the factionary part) of the real numbers stored in [bold]myfile.txt[/bold]. The new file should write data in the same format (3[/font][font=Symbol]?[/font][font=Times New Roman]5 with columns separated by space) as the original file. Include statements to check if the file was opened and closed successfully. Submit only the final, commented code.[/font]
[font=Times New Roman] [/font]
[font=Times New Roman] [/font]
[underline]Exercise 3 (4 points)[/underline]
[font=Times New Roman]Create a text file containing 20 rows of 20 integers separated by spaces. The integers should be in the range 0-9 and separated by spaces. Write a program that reads the contents into a 20[/font][font=Symbol]?[/font][font=TimesNewRoman]20 (two dimensional) array of int[/font]. [font=TimesNewRoman]The program then should use this array to initialize a 20x21 array of char by associating each integer from 0 to 9, with a character from ‘A’ to ‘J’. For example, 0 could be associated with ‘A’, while 9 with ‘J’. The 21st character on each line in the array of char should be the null character ‘\0’, making the array an array of 20 strings. Have the program display the resulting (picture) array of char (printing row-by-row the strings) on [italic]screen[/italic], and also write it in into a [italic]text file[/italic].[/font]