#Linux中的GCC编程# 20170731 C培训作业

C培训作业

20170731

1、结构体,员工信息输入与输出。并保存到txt文件中。

/*-------------------------------------------
定义一个结构体:
       Structemployee{
              Int ID;
              Char name[20];
              Float salary;
}
保存三个员工的信息到文件中,然后显示出来
--------------------------------------------*/

#include"stdio.h"
#include"string.h"
typedef struct employee{
	int id;
	char name[20];
	float salary;
}Emp;

int main(void)
{
	Emp emp[3];
	memset(emp,0,3*sizeof(Emp));   //定义结构体数组,并初始化。
	//@@@打开文件@@@//
	FILE *fp=fopen("test33.txt","wt");  //wt表示本次工作模式是:写入文本文件
	if(fp==NULL)	
	{
		printf("打开文件失败\n");
		return 0;
	}
	int i=0;
	for(;i<3;i++)
	{
                                          //输入信息,传入结构体数组
		printf("请输入员工信息(ID、姓名、工资):");
		scanf("%d%s%f",&emp[i].id,emp[i].name,&emp[i].salary);
	//@@@写入文件@@@//每次循环 都会写入一个员工的信息。
            	fprintf(fp,"id  :%d\t",emp[i].id);
		fprintf(fp,"姓名:%s\t",emp[i].name);
		fprintf(fp,"工资:%.2f\n",emp[i].salary);
	}

	printf("写入成功!\n");


	//@@@关闭文件@@@//
	fclose(fp);
	fp=NULL;//置空文件指针

	//---------------------------信息已经写入文件test33.txt-----------------------------//
	memset(emp,0,3*sizeof(Emp));//清空结构体数组
					//准备读取文件的内容
	
	//@@@再次打开文件@@@//
	fp=fopen("test33.txt","rt"); 
	if(fp==NULL)
	{
		printf("第二次打开文件失败!\n");
		return 0;
	}
	
	
	for(i=0;;i++)   		//使用死循环 读取文件中所有的信息
	{
		int res=fscanf(fp,"id  :%d\t姓名:%s\t工资:%f\n",&emp[i].id,emp[i].name,&emp[i].salary); //这里的%f不能写成%.2f
				//这里的res用于存储 fscanf(...)的返回值。参数的个数,失败时返回-1
		if(res<0)break;
	}
	printf("文件信息已经读入程序!\n");

	//--------------------------信息已经读取成功---------------------------------//
	for(i=0;i<3;i++)
	{
		printf("id :%d\tname:%s\tsalary:%f\n",emp[i].id,emp[i].name,emp[i].salary);
	}
	//@@@关闭文件@@@//
	fclose(fp);
	fp=NULL;
	return 0;
}

运行结果:

[root@localhost Kshine]# gcc test33.c -Wall
[root@localhost Kshine]# ./a.out 
请输入员工信息(ID、姓名、工资):33201 金鱼 10000
请输入员工信息(ID、姓名、工资):22202 留学方 12345
请输入员工信息(ID、姓名、工资):55203 煮雨文 34567
写入成功!
文件信息已经读入程序!
id :33201       name:金鱼       salary:10000.000000
id :22202       name:留学方     salary:12345.000000
id :55203       name:煮雨文     salary:34567.000000

在txt文件里,存放里上述的信息

[root@localhost Kshine]# vi test33.txt 
id  :33201     姓名:金鱼      工资:10000.00
id  :22202     姓名:留学方    工资:12345.00
id  :55203     姓名:煮雨文    工资:34567.00
~
~
(删除一些行)
~
"test33.txt" 3L, 147C

2、读取文件中的信息,并提取出指向的路径文件的名称和路径文件的类型。

/*
编写一个C语言函数实现如下功能:
打开配置文件,获取文件一行内容,将获得的内容进行分离如:/xxx/yy/yesterday.mp3
char *get_file_name(char *file_path); //”yesterday”
char *get_file_extesion(char *file_path); // “mp3”
*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* get_file_name(char *file_path); //”yesterday”
char* get_file_extesion(char *file_path); //用于获取文件
int main()
{
	char str[100]="";//成员全是0

	//打开文件,准备读取数据,保存在数组里
	FILE *fp=fopen("test34.txt","rt");//读数据
	if(fp==NULL)
	{
		printf("文件打开失败!\n");
		return 0;
	}
	
	fgets(str,100,fp);              //char *fgets(char *buf, int bufsize, FILE *stream);
	                                //存储的地址, 空间大小,文件指针
					//fgets()的特点是,每次读取一行,最后一位是\n,需要手动置、0
	
	int length =strlen(str);	//计算出读取的字符串实际长度
	str[length-1]='\0';		//手动增加字符串的结尾
	//关闭文件
	fclose(fp);
	fp=NULL;

	//-----------------------------------------------
	//对得到的字符串进行处理
	char *p1=get_file_name(str);
	printf("name     is :  %s\n",p1);

	char *p2=get_file_extesion(str);
	printf("extesion is :  %s\n",p2);
	
	free(p1);   //因为函数传过来的是动态数组
	free(p2);   //因为函数传过来的是动态数组
	p1=p2=NULL;                    //等于null的时候,可以连着赋值

	return 0;
}


//将字符串中的名字返回出来   /ssss/yyyyy/123.mp3'\0'

char* get_file_name(char *file_path)
{
	//char str[20]="";   //用于存储
	char *str=(char*)calloc(20,sizeof(char)); //动态数组在堆里面,全局

	int i=strlen(file_path)-1;  //用于逆序操作字符串数组
	int x1=0,x2=0;
	//-----------------------------
	for(;i>=0;i--)
	{
		//逆序,记录下第一个“.”
		if(file_path[i]=='.')
			x2=i;
		//逆序,记录下第一个“/”
		if(file_path[i]=='/')
		{
			x1=i;
			break;
		}
	}
	//------- 0  x1 name x2 extension   \0----------------------
	i=x1+1;  //name的第一个字母	
	for(;i<x2;i++)
	{
		str[i-x1-1]=file_path[i];
	}
	return str;
}

char* get_file_extesion(char *file_path)
{
	//char str[20]="";
	char *str=(char*)malloc(20*sizeof(char));
	int i=strlen(file_path)-1;
	int x2=0;  //记录'.'的位
	for(;i>=0;i--)
	{
		if(file_path[i]=='.')
		{
			x2=i;
			break;
		}
	}
	//--------0 ... x2 ... '\0'  ----------------
	i=x2+1;
	for(;i<strlen(file_path);i++)
	{
		str[i-x2-1]=file_path[i];
	}
	return str;

}

文件内容

[root@localhost Kshine]# vi test34.txt 
//2018/12/26//test34//Kshine.soCOOL
~
~
~
(删除一些行)
~
~
"test34.txt" 1L, 36C written

运行结果:

[root@localhost Kshine]# gcc test34.c -Wall
[root@localhost Kshine]# ./a.out 
name     is :  Kshine
extesion is :  soCOOL

3、copy 复制文件

/*
使用文件操作函数实现:
文件的复制功能
(将一个文件中的信息复制到另外一个文件中)。
用到带参主函数
*/

#include"stdio.h"
#include"stdlib.h"

int main(int argc,char *argv[]) //argc表示参数的个数
{				//argv[]表示数组(存储的元素是指针,也就是很多字符串的地址)
				//在定义这个形参的时候,要用到二级指针。
/*
这两个变量 的名字没有特别要求。
你完全可以把argc改成m等其他可用的名字。
但一般是这两个:argc(=args count)和argv(args value)。
*/

	if(argc<3)			    //命令行,需要有 ./main 1.txt 2.txt 
	{				    //至少要有3个字符串
		printf("input error!\n");
		exit(1);                    //如果少于3,结束进程,返回1给系统
	}

	//打开文件1
	FILE *fp1=fopen(argv[1],"rt");	    //数组元素1,是一个字符串的首地址
	FILE *fp2=fopen(argv[2],"wt");	
	if(fp1==NULL||fp2==NULL)
	{
		printf("打开文件失败\n");
		return 0;
	}

	char ch=0;//'\0'  用于一个一个字母搬运
	while(1)
	{
		int res=fscanf(fp1,"%c",&ch); //读取
		if(res<0)break;
		fprintf(fp2,"%c",ch);//写入
	}
	fclose(fp1);
	fclose(fp2);
	fp1=fp2=NULL;


	return 0;
}

运行结果:

[root@localhost copy]# gcc test.c -Wall -o test
[root@localhost copy]# ls
1.txt  test  test.c
[root@localhost copy]# ./test 1.txt 1_1.txt
[root@localhost copy]# ls
1_1.txt  1.txt  test  test.c

猜你喜欢

转载自blog.csdn.net/Kshine2017/article/details/85270817
今日推荐