通过指针来引用内容,修改内容
打印每一行:
打印每一行的第四列 :
#include<stdio.h>
//函数:就是描述功能的一次
//返回值 函数名(形参)
//{
// 函数体;
//}
void fun(char** pb);
//main是系统来调用的
void main()
{
/*
printf("main被执行\n");
//整型数组:
// int buf[100];
char buf[]="abzdefg";
printf("%s\n","abcde");
printf("%s\n",buf);
char* pa=buf;
char* pb="hella";
//通过指针来引用内容
printf("%c\n", *(pa+3));
printf("%c\n",*(pb+3));
//通过指针修改内容
*(pa+2)='c';
printf("%s\n",pa);
//*(pb+4)='o'; 不能修改常空间,
*/
//指针数组:专门用于存放指针的数组
/*
char* pa="abcde"
char* pb="12345"
char* pc="hello"
*/
//定义:
char* buf[4]={"abcde","12345","hello",NULL};
//引用:
printf("%c\n",*buf[1]);
printf("%s\n",buf[1]); //%s只需要给一个字符串的首地址 buf[1]引用数组第2个元素的内容
printf("%s\n",*(buf+1));
//调用:
fun(buf);
}
void fun(char** pb)
{
printf("%s\n",*(pb+1));//pb引用内容=&buf[0] *(pb+1)
//打印每一行:
//打印每一行的第四列
while(*pb!=NULL)
{
printf("%s %c\n",*pb,*(*pb+3));
pb++;
}
}
系统来调用 (传递值:用户与程序间接的交互)
#include<stdio.h>
//主函数:系统来调用 (传递值:用户与程序间接的交互)
void main(int argc,char* argv[])//argc传递值的个数 char* buf[10]...
{
//打印传递参数
int i=0;
for(i=0;i<argc;i++)
{
printf("%s\n",argv[i]);
}
}
习题 :
求整型的二进制 :
#include<stdio.h>
//转换为整型
int atoi(char* pn)
{
int num=0;
while(*pn!='\0' && *pn>='0' && *pn<='9')//只有数字符才转换
{
num*=10;//
num+=(*pn-'0');
pn++;
}
return num;
}
//求二进制:
void binary(int num)
{
if(0==num)
return;
binary(num/2);
printf("%d",num%2);//得到余数
}
//主函数:由系统调用,并接收系统传递过来的值 //123
void main(int argc,char* argv[])
{
//判断传递值的个数
if(argc<2)
{
printf("binary:参数不中,需要输入值\n");
return ;
}
// binary( atoi(argv[1]) );
int num=atoi(argv[1]);
binary(num);
printf("\n");
}
操作文件:
一切设备皆文件:获取设备信息或写入设备信息时,本质是对设备对应的文件进行操作 :
错误输出的函数 :
#include<stdio.h>
#include<errno.h>
//操作文件:
//一切设备皆文件:获取设备信息或写入设备信息时,本质是对设备对应的文件进行操作
void main()
{
//操作屏幕(设备:一切设备皆文件):
//1打开
//2操作
/*
//写入屏幕
char buf[100]="12345abcdef\n";
fwrite(buf,1,11,stdout);//输出到屏幕文件
//从键盘(文件)上获取
*/
/*
char buf1[10]="";
fread(buf1,1,9,stdin);//从指定的文件读取内容 stdin标准输入设
fwrite(buf1,1,9,stdout); //write---屏幕
*/
//系统将常用的错误保存成为一个文件1001
FILE* fp=fopen("./aaaaaaaaa","rb");//以rb:只读
if(NULL==fp)//打开失败
{
//需要将stderr输出
fprintf(stdout,"stdout:文件打开失败\n");//stdout-->屏幕--->文件
fprintf(stderr,"stderr:文件打开失败\n");
}
//3关闭
fclose(fp);
}
习题 :
copy拷贝:将源文件 拷贝 目标地
cp 源文件 目标文件
#include<stdio.h>
#include<errno.h>
#include<string.h>
//copy拷贝:将源文件 拷贝 目标地
//cp 源文件 目标文件
void main(int argc,char* argv[])
{
if(argc<3)
{
printf("cp:缺少文件操作数\n");
return ;
}
//1打开文件
FILE* rfp=fopen(argv[1],"rb");//打开文件:以只读打开
//判断
if(NULL==rfp)
{
//没有定义sys_errlist的指针数组 和errno ,在定义在errno.h中
fprintf(stderr,"copy无法获取%s的文件状态(stat):%s\n",argv[1],strerror(errno));
return;
}
//创建目标文件:如果存在,则截断,不存在则创建
FILE* wfp=fopen(argv[2],"wb");//打开文件:文件不存在,则创建,否则截断
if(NULL==wfp)
{
fprintf(stderr,"copy:无法创建普通文件%s:%s\n",argv[2],strerror(errno));
return;
}
//2操作文件----拷贝文件 将argv[1]的文件 拷贝给argv[2]
int ilen=0;
char buf[1000]="";//定义漏斗(存储从源文件中读取的内容)
while((ilen=fread(buf,1,1000,rfp))>0) //读取文件
{
//printf("%d\n",ilen);
fwrite(buf,1,ilen,wfp);//存放时,必须存放的容量与取出来的容量一致
}
//3关闭文件
fclose(rfp);
fclose(wfp);
}
习题 :
通过链表的方式 , 将文件读取的数据存放到链表中 , 实现增删遍历功能 .
首先创建一个写入数据的文件 .
#include<stdio.h>
void main()
{
FILE* fp=fopen("./test.txt","wb+");
if(NULL==fp)
{
printf("打开失败\n");
return;
}
int a[10]={0,1,-2,3,4,5,6,7,8,9};
fwrite(&a,1,sizeof(int)*10,fp);
fclose(fp);
}
链表的文件内容 :
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct nd;
typedef int dtype;
typedef struct nd node;
typedef struct nd* pnode;
struct nd
{
dtype data;
pnode next;
};
bool insert(node** pph,dtype d)
{
pnode pnew=malloc(sizeof(node));
if(NULL==pnew)
return false;
pnew->data=d;
pnew->next=*pph;
*pph=pnew;
return true;
}
void list(pnode loc)
{
while(loc!=NULL)
{
printf("%d\n",loc->data);
loc=loc->next;
}
}
bool delete(pnode* ph,dtype d)
{
pnode front=*ph,rear=*ph;
while(rear!=NULL && rear->data!=d)
{
front=rear;
rear=rear->next;
}
if(rear!=NULL)
{
if(rear==*ph)
*ph=rear->next;
else
front->next=rear->next;
free(rear);
return true;
}
return false;
}
void main()
{
FILE* fp=fopen("./test.txt","rb+");
if(NULL==fp)
{
printf("打开失败\n");
return ;
}
int a[10]={}; //这里可以不通过数组来做,可以直接使用while的循环操作实现.
fread(&a,1,sizeof(a),fp);
//printf("%d\n",sizeof(a));
fclose(fp);
pnode head = NULL;
int i=0;
for(i=0;i<10;i++)
insert(&head,*(a+i));
list(head);
printf("******删除******\n");
delete(&head,5);
list(head);
}