C/C++ 编程代写
当前位置:以往案例 > >案例CS之C语言buffer file system and file structure poin
2019-05-13

Lab15File

1. Purpose

1) Grasp the concept of file, buffer file system and file structure pointer.

2) Grasp the specific steps of document operation.

3) Learning how to use files to open, close, read, write and other file operation functions.

4) Learninghow to use the buffer file system to do some simple operations on files.


2. Required Tasks

1) Read the following program, analyze the contents ,what is the content of the file filetest.txt?


#include
#include
void fun(char *fname,char *st)
{
FILE *myf;
int i;
myf=fopen(fname,"w");
if (st==NULL)
return;
for(i=0;i<(strlen(st));i++) fputc(st[i],myf); fclose(myf); } void main() { fun("filetest","new world"); fun("filetest","hello!"); }



2) The following program in the user from the keyboard input a file name, then enter a string of characters (input with #) Store this file to form a text file, and the number of characters written to the file`s tail, please fill in the blanks.


#include
void main()
{
FILE *fp;
char ch,fname[32];
int count=0;
printf("Input the filename:");
scanf("%s",fname);
if((fp=fopen( ⑴ ,"w+"))==NULL)
{
printf("Can't open file:%s\n",fname);
exit(0);
}
printf("Enter data:\n");
while((ch=getchar())!='#')
{
fputc(ch,fp);
count++;
}
fprintf( ⑵ ,"\n%d\n",count);
fcolse(fp);
}



3) Input 10 character strings from the keyboard, change the lowercase to uppercase, and then output to a disk file "test. Dat" , then reading the string from the file and displaying.

Use fputc, fprintf,fputs to write to file respectively;

Use fgetc, fscanf, fgets to read from file respectively.

4) Write a program, Test writing 10 floats into a text file (w a r+ w+ modes and fputs()/fprintf() ) and a binary file(b mode and fwrite()) respectively. Open the two files again, display the floats.

5) Define a structure to store a row of data in the following table, and store the data of the entire table within a linked list.


Anthony

A.J.

10031

7.82

62/12/18

Burrows

W.K.

10067

9.14

63/6/9

Fain

B.D.

10083

8.79

59/5/18

Janney

P.

10095

10.57

62/9/28

Smith

G.J.

10105

8.50

91/12/20


Write a program, enter the above data to the structure list (linked list), and store data into a text file. Then read out the contents of the above file into a linked list(define the corresponding structure type), display the list on the screen.

Use fwrite() and fread() functions.


3. Optional Tasks

1) Read the following procedures and C program, and fill in the blanks.

This program finds all the integers in the text file "st.dat". In the text file, the integers are separated by space characters, Tab characters (tab characters) and carriage returns. The program uses array b[] to store different integers, the variable K is the number of different integers stored in the array B, and assumes that the number of different integers in the file is not more than 1000. Procedures are as follows:


#include
#define N 1000
void main()
{
FILE  ⑴ ;
int b[N],d,i,k;
if ((fpt=  ⑵  ==NULL){
printf("Can not open file st.dat.\n");
return;
}
k=0;
while(fscanf(  ⑶  )==1)
{
b[k]=d;
for(i=0;b[i]!=d;i++);
if(  ⑷  )  k++;
}
(5)  ;
for(i=0;i



2) Read the following program instructions and C program and fill in the blanks.

From this program reads the data with the product name and quantity of file, according to the product category identifier (name of the first English letters) belongs to block scope, output the number of various categories of products list.

The categories of categories of product categories are as follows:

A-E,F-I,J-M,N-Q,R-U,V-Z

The program is as follows: Please complete the program:


#include
#include
#include
char class_list[]="EIMQUZ";
int q_list[26],q;
FILE *fopen(), *fp;
void main()
{
char file_name[120],pname[20];
int i,sp;
char sch;
printf("Input data file name.");
scanf("%s",file_name);
if((fp=fopen(file_name,"r"))==NULL)
{
printf("Can not open file %s\n",file_name);
exit⑴;
}
for(i=0;i<26;i++) q_list[i]=0; fscanf(fp,"%20s%s",pname,&q); while(!feof(fp)) { sp=⑴; while(class_list[sp]!='\0') if((pname[0]>='a'&&pname[0]<='z'&&⑵)||⑶) break; else ⑷; q_list[(5)]+=q; fscanf(fp,"%20s%d",pname,&q); } fclose(fp); sch='A'; for(i=0;i0) printf("%c--%c:%d\n",sch,class_list[i],q_list[i]);
sch=class_list[i]+1;
}
}



3) There are five students, each of them has three courses and input data (including the number of students, name, three courses grades) from the keyboard and make a linked list to store them. Calculate the average score, and save the 5 students’ data (5 structure variables) and their calculated average scores in the disk file "stud. Dat".  Write another program to open the file, read the data in file and store them in a linked list (it’s to restore the linked list) , display the 5 students’ data and their average score.


4) Write a program to save the following data to a text file.

5 96 87 78 93 21 4 92 82 85 87 6 72 69 85 75 81 73

At the same time, the average number of each group in the above data is calculated and displayed in the program. The data is arranged in a file: the number that represents each group is placed in front of the group. For ple, the first number 5 in the document indicates that the next 5 numbers will be grouped together. The number 4 indicates the following 4 numbers are a group, and the 6 represents the last 6 numbers as a group.


5) Write a program, create a binary file of grades.bin, and write the following 5 rows of data to the file.

90.3 92.7 90.3 99.8

85.3 90.5 87.3 90.8

93.2 88.4 93.8 75.6

82.4 95.6 78.2 90.0

93.5 80.2 92.9 94.4

At the same time, read the four data (scores) of each line in the program, calculate and display the average of each group (average score).


4. Experimental Result

Write an experiment report, it includes:
1) The running results and source programs of required subjects.
2) The running results and source programs of chosen subjects.
3) The error information when debugging(both in English and Chinese).
4) If there are some programs can not compile/link/run, please analysis the reasons.



在线提交订单