先创建一个子目录:mkdir 文件名;再在子目录下创建一个.c文件一个.txt文件:touch student.txt touch task41.c;向文件中写入内容:cat>文件名 ;查看文件内容:cat 文件名
#include<stdio.h>
#include<string.h>
#define MAXSIZE 200
void main()
{
FILE* fp1, * fp2;
char buf[MAXSIZE];
char* a[6];
char buffer[100];
char check[] = "计算机与网络安全学院";
char s[2]=":";
if ((fp1 = fopen("student.txt", "r")) == NULL)
{
printf(" Open Failed!");
//return 0;
}
if ((fp2 = fopen("csStudent.txt", "a+")) == NULL)
{
printf(" Open Failed!");
//return 0;
}
while (!feof(fp1))//文本结束时退出循环
{
fgets(buf, MAXSIZE, fp1);//一行行读入
if (strstr(buf, check))//判断是否属于“计算机与网络安全学院”
{
a[0] = strtok(buf, s);//切割字符串成五部分
a[1] = strtok(NULL, s);
a[2] = strtok(NULL, s);
a[3] = strtok(NULL, s);
a[4] = strtok(NULL, s);
a[4][3] = 0;//将换行符去掉
sprintf(buffer, "%s:%s:%s:%s:%s\n", a[1], a[0], a[3], a[4], a[2]);//格式化写入buffer
fputs(buffer, fp2);//将buffer写入文本csStudent
}
}
fclose(fp1);
fclose(fp2);
//return 0;
}
编译过程:
gcc task41.c -o task41
./task41
查看csStudent.txt文件内容:cat csStudent.txt
测试数据:
运行结果:
放截图
运行结果:
放截图
(2)、
源代码:
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
typedef struct _subject {
char sno[20]; //学号
char name[20]; //姓名
float chinese; //语文成绩
float math; //数学成绩
float english; //英语成绩
}subject;
void main(){
subject su1[5];
subject as;
int i;
int fd;
fd=open("data",O_WRONLY|O_CREAT|O_TRUNC,0777);
for(i = 0;i < 5; i ++){
printf("该学生的学号为:\n");
scanf("%s", as.sno);
printf("该学生的姓名为:\n");
scanf("%s", as.name);
printf("该学生的语文成绩为:\n");
scanf("%f", &as.chinese);
printf("该学生的数学成绩为:\n");
scanf("%f", &as.math);
printf("该学生的英语成绩为:\n");
scanf("%f", &as.english);
write(fd,(void*)&as,sizeof(subject));
}
close(fd);
fd=open("data",O_RDONLY,0);
for(i = 0;i < 5;i ++)
read(fd,(void*)&su1[i],sizeof(subject));
for(i = 0;i < 5; i+=2){
printf("%s %s %.1f %.1f %.1f\n",su1[i].sno,su1[i].name,su1[i].chinese,su1[i].math,su1[i].english);
}
close(fd);
}
编译过程:
gcc task42.c -o task42
./task42
源代码:
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
void wewrite(int fd)
{
for (int i = 0; i < 10000000; i++)
write(fd, "a", 1);
}
void weread(int fd, char a[4])
{
for (int i = 0; i < 100000000; i++)
read(fd, a, 1);
}
void wefread(FILE *fp, char a[4])
{
for (int i = 0; i < 100000000; i++)
fread(a, sizeof(char), 1, fp);
}
void wefwrite(FILE *fp)
{
for (int i = 0; i < 100000000; i++)
fwrite("a", sizeof(char), 1, fp);
}
int main()
{
struct timeval tp;
struct timeval tp1;
double g_time_start;
double g_time_end;
//测量write函数
int fd = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0777);
gettimeofday(&tp, NULL);
g_time_start = tp.tv_sec * 1000000 + tp.tv_usec;
for (int i = 0; i < 10000; i++)
write(fd, "a", 1);
gettimeofday(&tp1, NULL);
g_time_end = tp1.tv_sec * 1000000 + tp1.tv_usec;
printf("write函数的运行时间为:%.4f微秒\n", (g_time_end - g_time_start) / 10000);
close(fd);
//测量read函数
fd = open("test.txt", O_RDONLY, 0);
char a[4];
gettimeofday(&tp, NULL);
g_time_start = tp.tv_sec * 1000000 + tp.tv_usec;
for (int i = 0; i < 10000; i++)
read(fd, a, 1);
gettimeofday(&tp1, NULL);
g_time_end = tp1.tv_sec * 1000000 + tp1.tv_usec;
printf("read函数的运行时间为:%.4f微秒\n", (g_time_end - g_time_start) / 10000);
//使用gprof工具测量write函数
close(fd);
fd = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0777);
wewrite(fd);
close(fd);
//使用gprof工具测量read函数
fd = open("test.txt", O_RDONLY, 0);
weread(fd, a);
close(fd);
//测量fwrite函数
FILE *fp = fopen("test.txt", "w");
gettimeofday(&tp, NULL);
g_time_start = tp.tv_sec * 1000000 + tp.tv_usec;
for (int i = 0; i < 10000; i++)
fwrite("a", sizeof(char), 1, fp);
gettimeofday(&tp1, NULL);
g_time_end = tp1.tv_sec * 1000000 + tp1.tv_usec;
printf("fwrite函数的运行时间为:%.4f微秒\n", (g_time_end - g_time_start) / 10000);
fclose(fp);
//测量fread函数
fp = fopen("test.txt", "r");
gettimeofday(&tp, NULL);
g_time_start = tp.tv_sec * 1000000 + tp.tv_usec;
for (int i = 0; i < 10000; i++)
fread(a, sizeof(char), 1, fp);
gettimeofday(&tp1, NULL);
g_time_end = tp1.tv_sec * 1000000 + tp1.tv_usec;
printf("fread函数的运行时间为:%.4f微秒\n", (g_time_end - g_time_start) / 10000);
fclose(fp);
//使用gprof工具测量fwrite函数
fp = fopen("test.txt", "w");
wefwrite(fp);
fclose(fp);
//使用gprof工具测量fwrite函数
fp = fopen("test.txt", "r");
wefread(fp, a);
fclose(fp);
}
编译过程:
gcc task43.c -o task43
./task43
运行结果:
放截图
源代码:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd, i, j, y;
char cap[500][20], temp[20];
int num[500], count = 0, flag, isEmpty;
char c;
fd = open("English.txt", O_RDONLY, 0);
for (i = 0; i < 500; i++)
{
isEmpty = 0;
for (j = 0; j < 20; j++)
{
read(fd, &c, sizeof(char));
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122))
{
temp[j] = c;
}
else if (c == '\0')
{
isEmpty = 2;
break;
}
else
{
temp[j] = '\0';
isEmpty = 1;
break;
}
}
if (isEmpty == 2)
{
break;
}
if (isEmpty == 1)
{
flag = 0;
for (y = 0; y < count; y++)
{
if (!strcmp(cap[y], temp))
{
num[y]++;
flag = 1;
break;
}
}
if (flag == 0)
{
strcpy(cap[count], temp);
num[count] = 1;
count++;
}
}
}
for (i = 0; i < count; i++)
{
for (j = i + 1; j < count; j++)
{
if (strcmp(cap[i], cap[j]) > 0)
{
strcpy(temp, cap[i]);
strcpy(cap[i], cap[j]);
strcpy(cap[j], temp);
y = num[i];
num[i] = num[j];
num[j] = y;
}
}
}
printf("按词典序输出各单词的词频:\n");
for (i = 1; i < count; i++)
printf("%s:%d\n", cap[i], num[i]);
for (i = 0; i < count; i++)
{
for (j = i + 1; j < count; j++)
{
if (num[i] < num[j])
{
strcpy(temp, cap[i]);
strcpy(cap[i], cap[j]);
strcpy(cap[j], temp);
y = num[i];
num[i] = num[j];
num[j] = y;
}
}
}
printf("输出出现频度最高的10个单词的词频:\n");
for (i = 1; i < 11 && i < count; i++)
printf("%s:%d\n", cap[i], num[i]);
}
编译过程:
gcc tasks44.c -o task44
./task44
测试数据:
放截图
运行结果:
放截图