file

/*
文件操作的学习
知识点:
1.操作系统把各种设备都统一作为文件来处理。
	例如:键盘是输入文件,显示屏和打印机是输出文件。
2.	“文件”是指存储在外部介质上数据的集合。
3.操作系统以文件为单位对数据进行管理。
4.C语言中,文件被看做一个一个字符的序列。另外一些语
  言如Pascal中,文件由若干“记录(record)”组成。
5.文件标识包括三部分:a.文件路径 b.文件名主干 c.文件后缀
6.根据数据的组织形式,可分为ASCII文件(文本文件)和二进制文件。

小秘密:
1."putchar()是从fputc函数“派生”而来。"
——谭浩强《C程序设计教程》,2007年7月第1版
 而打开VC6.0,stdio.h中putchar的具体定义如下:

 ……
#define putc(_c,_stream)  (--(_stream)->_cnt >= 0 \
                ? 0xff & (*(_stream)->_ptr++ = (char)(_c)) :  _flsbuf((_c),(_stream)))
#define getchar()         getc(stdin)
#define putchar(_c)       putc((_c),stdout)
……
是不是有点问题?
2.__cdecl 是C Declaration的缩写(declaration,声明),
表示C语言默认的函数调用方法:所有参数从右到左依次入栈,
这些参数由调用者清除,称为手动清栈。被调用函数不会要求
调用者传递多少参数,调用者传递过多或者过少的参数,甚至
完全不同的参数都不会产生编译阶段的错误。
3.
*/

//program1
/*
#include<stdio.h>
#include<stdlib.h>
int main()
{
//	char a='c';
//	putchar(a);
//	fputc(a,stdout);

	FILE *fp;
	char ch,filename[10];
	scanf("%s",filename);
	if((fp=fopen(filename,"a+")) == NULL)
	{
		printf("cannot open file\n");
		exit(0);
	}

	ch = getchar();//吃掉回车

	ch = getchar();
	while(ch!='#')
	{
		fputc(ch,fp);
		putchar(ch);
		ch = getchar();
	}

	fclose(fp);		//关闭文件
	putchar(10);//10是换行
	return 0;
}
*/
/*
//program2
#include<stdio.h>
#include<stdlib.h>
int main()
{
	FILE *in,*out;
	char infile[10],outfile[10];		//两个字符数组存储文件名
	printf("Enter the infile name:\n");
	scanf("%s",infile);
	printf("Enter the outfile name:\n");
	scanf("%s",outfile);

	if((in = fopen(infile,"r")) == NULL)//打开输入文件
	{
		printf("cannot open infile\n");
		exit(0);
	}

	if((out = fopen(outfile,"w")) == NULL)//打开输入文件
	{
		printf("cannot open infile\n");
		exit(0);
	}

	while(!feof(in))
	{
		fputc(fgetc(in),out);
	}


	fclose(in);
	fclose(out);
	return 0;
}
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
	FILE *fp;
	char str[3][10],temp[10];
	int i,j,k,n=3;
	
	printf("Enter strings:\n");
	for(i=0;i<n;i++)
	{
		gets(str[i]);				//输入字符串
	}
	
	for(i=0;i<n-1;i++)			//用选择法对字符串排序
	{
		k=i;
		for(j=i+1;j<n;j++)
		{
			
			if(strcmp(str[k],str[j])>0)
			{
				k=j;
			}
		}

		if(k!=i)
		{
			strcpy(temp,str[i]);
			strcpy(str[i],str[k]);
			strcpy(str[k],temp);
		}
	}

	if((fp=fopen("file1.dat","w"))==NULL)
	{
		printf("cannot open file!\n");
		exit(0);
	}

	printf("\nThe new sequence:\n");
	for(i=0;i<n;i++)
	{
		fputs(str[i],fp);
		fputs("\n",fp);
		printf("%s\n",str[i]);
	}

	
}

以上代码来自谭浩强《C程序设计教程》,2007年7月第1版
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值