<span style="font-size:14px;">#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<fcntl.h>
//创建文件子函数
void creat_file(char *filename)
{
int fd; //系统调用描述文件的文件描述符(为非负数)
if(fd=creat(filename,0775)<0)
{
perror("创建文件失败");
exit(1);//非正常跳出
}
else
{
printf("创建 %s 文件成功\n",filename);
}
}
/*可传入参数的主函数入口*/
int main(int argc,char **argv)
{
int i;
if(argc<2) //说明没有传入文件名,创建文件失败
{
perror("您没有输入文件名,创建文件失败!");
exit(1);
}
for(i=1;i<argc;i++)
{
creat_file(argv[i]); //调用子函数依次创建给定文件名的文件
}
exit(0);
return 0;
}
//编译:gcc test1 -c test1.c
//运行:./test1 hello1 hello2,创建两个文件hello1和hello2.</span>二、写入一个字符到文件中/*Filename:test2.c
*功能:终端输入一些字符,直到用户输入一个"#"为止结束输入,最后依次将他们保存到磁盘一个文件中。
*Date:2014.11.21
*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *stream; //声明一个FILE类型指针变量,用于保存打开文件的相关信息
char ch,filename[10];
printf("请输入要创建文件名称:\n");
scanf("%s",filename); //输入即将创建文件名称,其中数组名为数组首地址(可包含绝对地址)
/*1.(创建)以只写方式打开一个文件*/
if(stream=fopen(filename,"w")==NULL)
{
printf("Can not open %s.",filename);
exit(1);
}
ch=getchar(); //接收回车字符
/*2.输入需要写入文件的字符*/
printf("请输入要依次写入的字符,每个字符都需以回车键确认.\n");
ch=getchar(); //从键盘接收第一个写入的字符
while(ch!='#') //依次写入一个字符到文件中
{
fputc(ch,stream); //将一个字符写入到指针变量stream指定的文件中
putchar(ch); //打印一下字符到中断显示,以便判定
ch=getchar(); //接收终端输入的下一个字符,直到用户输入'#'后结束输入
}
fclose(fp); //关闭文件
putchar(10); //换行字符
}
//编译 gcc test2 -c test2.c
//运行 ./test2,然后依次输入文件名,写入的字符。如果是想写入字符串,只需将fget()、fput()更改为fgets()、fputs()即可三、从一个文件写一个字符串到另一个文件/*功能:两个文件之间的读写
*实现:从键盘终端输入字符到file1 , 再将一个磁盘文件中的信息复制到另一个磁盘文件file2中。
*思路:利用fgetc()从file1读取信息,用fputc向file2中写入信息。定义两个文件指针变量分别指向file1,file2.
*Date:2014.11.21
*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *stream1,*stream2; //文件指针
char ch,filename1[10],filename2[10];
printf("请输入文件1名称:\n");
scanf("%s",filename1);
printf("请输入文件2名称:\n");
scanf("%s",filename2);
/*1.创建(打开)file1文件,并向其写入数据*/
if((stream1=fopen(filename1,"w+")==NULL)) //以可读写方式打开文件
{
printf("打开文件1失败");
exit(1);
}
ch=getchar(); //接收最后输入的回车符,防止回车符被当作第一个字符被写入到文件1中
printf("请输入要写入的字符数据:\n");
ch=getchar(); //接收第一个字符
while(ch!='#')
{
fputc(ch,stream1);//写入一个字符到文件1中
ch=getchar(); //依次从终端获取一个字符保存到ch中,直到输入'#'字符结束
}
fclose(stream1); //关闭文件1
/*2.分别打开文件1和文件2*/
if((stream1=fopen(filename1,"w+")==NULL)) //以可读写方式打开文件
{
printf("打开文件1失败");
exit(1);
}
if((stream1=fopen(filename1,"r+")==NULL)) //以可读写方式打开文件
{
printf("打开文件2失败");
exit(1);
}
putchar(10); //输入一个换行
/*3.将文件1数据复制到文件2中*/
while(feof(stream1)!=0) //判定是否已经达到文件1尾部
{
ch=fgetc(stream1); //调用fgetc()函数从stream1所指的文件1读取一个字符
fputc(ch,stream2); //调用fputc()函数将一个字符写入到stream2所指的文件2中
}
putchar(10); //回车符
fclose(stream1);
fclose(stream2);//关闭文件1、文件2,释放其所占的资源
}
//编译 gcc test3 -c test3.c
//运行 ./test3
四、读写字符串/*功能:读写字符串
*实现:从键盘读入若干个字符串,对他们按字母大小的顺序排序,然后将排序后的结果送到磁盘文件中保存。
*思路:利用二维数组输入字符串,将排序好的字符串利用fputs(str ,fp)函数将数组中的字符串送到文件中。
*Date:2014.11.21
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
FILE *stream; //声明文件指针,用于保存打开文件信息
int N;
char str[N][10],temp[10]; //声明二维数据存储N个长度为10的字符串,temp为临时变量
int i,j;
printf("请输入要写入的若干个字符串\n");
/*1.向数组str中输入若干个字符串*/
for(i=0;i<N;i++)
{
scanf("%s",&str[i]); //等价与gets(str[i])
}
/*2.对输入的一系列字符串进行由小到大排序:字符串逐一对比*/
for(i=0;i<N-1;i++)
{
for(j=0;i<N;j++)
{
if(strcmp(str[i],str[j])>0) //当i=0时,将第一个字符串逐一与其他字符串对比:如果上一个字符串大于下一个字符串,则调换顺序
{
strcpy(temp,str[i]); //先将大号放在临时数组
strcpy(str[i],str[j]); //再将小号存放前一个数组
strcpy(str[j],temp); //最后将存放大号的临时数据字符串存放到后面的数组
}
}
}
/*3.打开文件已有文件*/
if((stream=fopen("C:\\test4.txt","w"))==NULL)
{
printf("打开C:\\test4.txt失败");
exit(1);
}
printf("字符串排序结果为:\n");
/*4.在终端显示排序结果,并将其写入文件中*/
for(i=0;i<N;i++)
{
fputs(str[i],stream); //将字符串逐一写入到文件中,并且每写入一个字符串回车
fputs("\n",stream);
puts(str[i]); //将字符串逐一显示在终端
}
putchar(10); //换行
fclose(stream); //关闭文件,释放资源
}
//编译 gcc test4 -c test4.c
//允许 ./test4五、文件读写(以二进制形式)/*目标:文件读写(使用二进制方式)
*功能:从键盘输入10个学生有关数据,然把它们转存到磁盘文件1上去,再将文件1内容读出到终端上
*思路:1 size_t fread(void *buf,size_t size,size_t count,FILE *stream):从文件中读取内容到缓冲区
* size_t fwrite(const void *buf,size_t size,size_t count,FILE *stream):将缓冲区内容写到文件中
* 2.利用结构体存储数据,save函数完成转存数据到文件
*Date:2014.11.21
*/
#include<stdio.h>
#include<stdlib.h>
#define N 10 //10个学生
int i;
/*1.结构体:用于存储学生数据组*/
struct Student{
char name[10];
int age;
char sex;
char addr[30]
}stu1[N],stu2[N]; //定义两个结构体对象,前者用于存储输入的数据,后者用于保存从文件1中读出的数据
/*2.save子函数:用于处理输入的数据
*/
void save()
{
//a)先打开一个文件
FILE *stream1; //文件指针:保存一个文件的信息
char filename[15];
if(stream1=fopen(filename,"wb")==NULL)
{
perror("打开文件失败,Sorry!");
exit(1);
}
//b)将10组数据逐一文写入文件1中,依次写一个学生的数据
for(i=0;i<N;i++)
{
if((fwrite(&stu1[i],sizeof(struct Student),1,stream1))!=1)
{
perror("Write file1 failed");
}
}
fclose(stream1); //关闭文件1
//c)从file1读取数据到缓存stu2[],并显示读到的每组数据
if(stream1=fopen(filename,"rb")==NULL) //重新打开文件1
{
perror("打开文件失败,Sorry!");
exit(1);
}
for(i=0;i<N;i++)
{
fread(&stu2[i],sizeof(struct Student),1,stream1);
printf("从文件1读到的数据为:%s%d%s%s\n",stu2[i].name,stu2[i].age,stu2[i].sex,stu2[i].addr);
}
fclose(stream1);
}
/*3.主函数:只负责输入数据并调用save子函数*/
void main()
{
printf("请输入10组学生的数据:\n");
for(i=0;i<N;i++) //依次输入10组数据,注数组名即数组元素的起始地址
{
scanf("%s%d%s%s",stu1[i].name,&stu1[i].age,stu1[i].sex,stu1[i].addr);
}
save(); //处理数据
}
//编译:gcc test5 -c test5.c
//运行:./test5
六、时间编程
格林威治时间GTC、日历时间
头文件 #include<time.h>
1.时间获取
函数:
time_t time(time_t *tloc)
功能:获取日历时间,即从1970年1月1日0点到现在所经历的秒数。其中time_t为长整型
返回:返回时间
2.时间转化
函数:
struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
功能:将日历时间(字符串形式)转化为格林时间,并保存到TM结构体中
将日历时间转化为本地时间,并保存到TM结构体中
其中
sruct tm{
int tm_sec; //秒值
int tm_min; //分值
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
注释1:type time_t long; 这个是用户自定义类型,是类库提供的头文件,为长整形。
注释2:*gmtime为tm结构体类型指针变量,用于指向结构体。
源代码
**********************************************************************
实现:获取日历时间,并将日历时间转化为本地时间,格林威时间.
思路:time(NULL)-返回time_t类型时间、localtime(&t)/gmtime(&t)-返回时间信息到结构体;
***********************************************************************
#include time.h>
#include stdio.h>
int main()
{
struct tm *local; //定义一个结构体指针变量local,用于存放时间信息(转化的时间信息)
time_t t; //定义一个time_t类型长整型变量t,用于保存当前日历信息
*获取时间*
t=time(NULL); //调用time()函数,获取当前日历时间信息(即清空历史信息,自动保存当前时间)
*转化时间*
local=localtime(&t); //调用localtime()函数,将转化的本地时间保存到local指向的(struct tm)结构体中
printf("local time is:%d\n",local->tm_hour); //打印小时
local=gmtime(&t); //调用gmtime()函数,转化为UTC时间并保存到local指向的结构体中
printf("UTC time is:%d\n",local->tm_hour); //打印小时信息
return 0; //程序结束
}3.时间显示
(2)功能:将tm格式时间转化为字符串,如sat
源代码
**********************************************************************
实现:时间显示.
思路:asctime(display)将tm格式时间转化为字符串输出,ctime(&t)将日历时间转化为字符串输出;
***********************************************************************
#include time.h>
#include stdio.h>
int main(void)
{
struct tm *display; //定义一个结构体指针变量display指向struct tm结构体类型
time_t t; //定义一个time_t长整型变量t,用于存放当前日历时间
t=time(NULL); //获取当前日历时间
display=gmtime(&t); //将日历时间转化为UTC时间,并将时间信息保存到dispaly所指向的结构体中
*时间显示*
printf(asctime(display)); //将display所指向的tm格式时间转化为字符串显示()
printf(ctime(&t)); //将日历时间转为本地时间的字符串显示
return 0; //程序结束
}
4.获取时间
(1)函数:
(2)功能:获取今日凌晨到现在的时间差,常用语计算时间的耗时
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
源代码
*********************************************************************
实现:计算执行function()函数所消耗的时间.
思路:gettimeofday(struct timeval *tv,struct timezone *tz),获取凌晨到当前时间的时间差;
***********************************************************************
#include stdio.h>
#include stdlib.h>
#include math.h>
#include sys/timeb.h>
#include unistd.h>
*将要计算的function函数*
void function()
{
unsigned int i,j;
double y;
for(i=0;i<1000;i++)
for(j=0;j<1000;j++)
y++;
}
struct timeval
{
float _sec;
float _usec;
}
main()
{
struct timeval tpstart,tpend;//定义两个struct timeval类型变量tpstart、tpend,用于存放第一次和第二次时间差(秒 微秒)
float timeuse;//timeuse为计算消耗时间值
*function函数所消耗的时间*
gettimeofday(&tpstart,NULL);//计算当前时间到凌晨的时间差,将获得的时间差存放到结构体struct timeval中
function();
gettimeofday(&tpend,NULL);//计算运行function函数后的当前时间到凌晨的时间差,将获得的时间差存放到结构体struct timeval中
*总时间计算*
timeuse=(tpend._sec-tpstart._sec)+1000000*(tpend._usec-tpstart._usec);//将微秒化为秒
printf("The use time:%f\n",timeuse);
exit(0); //正常退出
}
</pre><pre name="code" class="csharp">
原文地址:http://blog.csdn.net/u012637501/article/details/41350465