#虚拟空间初始化
#include<string.h>
void *memset(void *s,int c,size_t n);
void*s是指存放着要初始化的虚拟空间的指针;int c是指要给初始化的虚拟空间所赋的值是一个整形;size_t n是指该虚拟空间所占的字节大小;
eg:
#include<string>
#include<stdlib.h>
int *pa=malloc(sizeof(int)*max);
memset(pa,0,max*4);
#迷宫代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void visited(int (*pa)[9],int i, int j)
{
p[i][j]=1;
int n,m; //重新用变量进行输出
if(i==7&&j==7){
printf("find path:\n");
for(n=0;n<9;n++){
for(m=0;m<9;m++){
if(pa[n][m]==1)
printf("*");
else if(pa[n][m]==2)
printf("2");
else printf("0");
printf("\n");
}
}
if(pa[i-1][j]==0)
visited(pa,i-1,j);
if(pa[i+1][j]==0)
visited(pa,i-1,j);
if(pa[i][j-1])
visted(pa,i,j-1);
if(pa[i][j+1])
visted(pa,i,j+1);
pa[i][j]=0;
}
int main(void)
{
int a[9][9] = {2,2,2,2,2,2,2,2,2,
2,0,0,0,2,2,2,2,2,
2,2,2,0,2,2,2,2,2,
2,2,2,0,2,2,2,2,2,
2,2,2,0,2,2,2,2,2,
2,0,0,0,0,0,0,2,2,
2,0,2,2,2,2,0,2,2,
2,0,0,0,0,0,0,0,2,
2,2,2,2,2,2,2,2,2,};
visited(a,2,2);
return 0;
}
#标准输入输出
在Linux系统中,一切设备皆文件!
打开一个流式文件
头文件:#include<stdio.h>
FILE *fopen(const char *path,const char *mode);
const char *path指文件名,const char *mode指使用文件方式(r【只读】,r+【读写】,a【追加】,a+【读写追加】,w【写建】)
如果成功返回file类型指针,如果错误返回NULL;
r+无创建功能,w有创建功能
eg:FILE *fp=fopen("a.txt","w"); //若有a.txt则清除原内容,重新写入;若无则新建一个a.txt
写入一个流式文件
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
头文件:#include<stdio.h>
fread函数是从sream指针所指的文件中读入nmemb次,每次读入SIZE字节,读入信息存放在ptr指针所指的存储空间。
eg:char buf[30];
memset(buf, 0, sizeof(buf));
int re = fread(buf, 1, sizeof(buf)-1, fp);
if(re <= 0){
printf("fread err . \n");
return 1;
}
printf("buf: %s\n", buf);
fwrite函数是将ptr指针所指的地址开始的信息输入nmemb次,每次写size字节到fp所指的文件中。
eg:char buf[30]="hello westos";
int ret = fwrite(buf,1,30,fp);
if(ret< strlen(buf)){
printf("fwrite err.\n");
return 1;
}
printf("success\n");
fclose(fp); //关闭fp所指文件
查看error number文件内容
vim /usr/include/asm-generic/errno.h
FILE *fp=fopen("a.txt","r");
if(fp==NULL){
perror("fopen:\n");
exit(EXIT_FAILURE);
}
正常退出运行程序
exit与return的区别:空间清理exit
头文件:#include<stdlib.h>
void exit(int status);
exit(EXIT_FALURE); --exit(0); 错误退出
exit(EXIT_SUCCESS); --exit(1); 正常退出
文件IO操作: 标准IO 系统IO
标准库中的IO操作 针对于当前系统提供的IO操作
fopen fread fwrite open read
fgets fputs ...(f)
stdin 标准输入流
stdout 标准输出流
stderr 标准错误流
eg:int ch=fgetc(stdin);
printf("ch:%c\n",ch);
putchar(ch);
ch = getchar();
printf("getchat:%c\n",ch);
缓冲区
标准输入库中基本上都带缓冲区
行缓冲:当前行满的时候,才刷新缓冲区的内容。
全缓冲:当缓冲区满的时候,才对缓冲区刷新一次。
不缓冲:不带缓冲区(系统IO)
int fflush(FILE *stream); //强制刷新缓冲区
eg:fflush(stdout);
fseek函数
int fseek(FILE *stream, long offset, int whence);
whence包括 SEEK_SET(0)初始位置, SEEK_CUR(1)当前位置, or SEEK_END(2)结束位置,
fseek(fp,-10L,0); //文件往前偏移不能超过其开头的初始位在,err
系统IO的标准输入输出
定义在/usr/inlcude/unistd.h
STDIN_FILENO 0
STDOUT_FILENO 1
STDERR_FILENO 2
作业:
cat
cp -r 使用strtok:char *strtok(char *str,const char *delim);
#include<string.h>
void *memset(void *s,int c,size_t n);
void*s是指存放着要初始化的虚拟空间的指针;int c是指要给初始化的虚拟空间所赋的值是一个整形;size_t n是指该虚拟空间所占的字节大小;
eg:
#include<string>
#include<stdlib.h>
int *pa=malloc(sizeof(int)*max);
memset(pa,0,max*4);
#迷宫代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void visited(int (*pa)[9],int i, int j)
{
p[i][j]=1;
int n,m; //重新用变量进行输出
if(i==7&&j==7){
printf("find path:\n");
for(n=0;n<9;n++){
for(m=0;m<9;m++){
if(pa[n][m]==1)
printf("*");
else if(pa[n][m]==2)
printf("2");
else printf("0");
printf("\n");
}
}
if(pa[i-1][j]==0)
visited(pa,i-1,j);
if(pa[i+1][j]==0)
visited(pa,i-1,j);
if(pa[i][j-1])
visted(pa,i,j-1);
if(pa[i][j+1])
visted(pa,i,j+1);
pa[i][j]=0;
}
int main(void)
{
int a[9][9] = {2,2,2,2,2,2,2,2,2,
2,0,0,0,2,2,2,2,2,
2,2,2,0,2,2,2,2,2,
2,2,2,0,2,2,2,2,2,
2,2,2,0,2,2,2,2,2,
2,0,0,0,0,0,0,2,2,
2,0,2,2,2,2,0,2,2,
2,0,0,0,0,0,0,0,2,
2,2,2,2,2,2,2,2,2,};
visited(a,2,2);
return 0;
}
#标准输入输出
在Linux系统中,一切设备皆文件!
打开一个流式文件
头文件:#include<stdio.h>
FILE *fopen(const char *path,const char *mode);
const char *path指文件名,const char *mode指使用文件方式(r【只读】,r+【读写】,a【追加】,a+【读写追加】,w【写建】)
如果成功返回file类型指针,如果错误返回NULL;
r+无创建功能,w有创建功能
eg:FILE *fp=fopen("a.txt","w"); //若有a.txt则清除原内容,重新写入;若无则新建一个a.txt
写入一个流式文件
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
头文件:#include<stdio.h>
fread函数是从sream指针所指的文件中读入nmemb次,每次读入SIZE字节,读入信息存放在ptr指针所指的存储空间。
eg:char buf[30];
memset(buf, 0, sizeof(buf));
int re = fread(buf, 1, sizeof(buf)-1, fp);
if(re <= 0){
printf("fread err . \n");
return 1;
}
printf("buf: %s\n", buf);
fwrite函数是将ptr指针所指的地址开始的信息输入nmemb次,每次写size字节到fp所指的文件中。
eg:char buf[30]="hello westos";
int ret = fwrite(buf,1,30,fp);
if(ret< strlen(buf)){
printf("fwrite err.\n");
return 1;
}
printf("success\n");
fclose(fp); //关闭fp所指文件
查看error number文件内容
vim /usr/include/asm-generic/errno.h
FILE *fp=fopen("a.txt","r");
if(fp==NULL){
perror("fopen:\n");
exit(EXIT_FAILURE);
}
正常退出运行程序
exit与return的区别:空间清理exit
头文件:#include<stdlib.h>
void exit(int status);
exit(EXIT_FALURE); --exit(0); 错误退出
exit(EXIT_SUCCESS); --exit(1); 正常退出
文件IO操作: 标准IO 系统IO
标准库中的IO操作 针对于当前系统提供的IO操作
fopen fread fwrite open read
fgets fputs ...(f)
stdin 标准输入流
stdout 标准输出流
stderr 标准错误流
eg:int ch=fgetc(stdin);
printf("ch:%c\n",ch);
putchar(ch);
ch = getchar();
printf("getchat:%c\n",ch);
缓冲区
标准输入库中基本上都带缓冲区
行缓冲:当前行满的时候,才刷新缓冲区的内容。
全缓冲:当缓冲区满的时候,才对缓冲区刷新一次。
不缓冲:不带缓冲区(系统IO)
int fflush(FILE *stream); //强制刷新缓冲区
eg:fflush(stdout);
fseek函数
int fseek(FILE *stream, long offset, int whence);
whence包括 SEEK_SET(0)初始位置, SEEK_CUR(1)当前位置, or SEEK_END(2)结束位置,
fseek(fp,-10L,0); //文件往前偏移不能超过其开头的初始位在,err
系统IO的标准输入输出
定义在/usr/inlcude/unistd.h
STDIN_FILENO 0
STDOUT_FILENO 1
STDERR_FILENO 2
作业:
cat
cp -r 使用strtok:char *strtok(char *str,const char *delim);