先添加 一个 头文件 string.h 用于字符串
字符串输入输出 fgets 与 puts
C 库函数 char fgets(char str, int n, FILE *stream) 从指定的流 stream 读取一行,并把它存储在 str 所指向的字符串内。当读取 (n-1) 个字符时,或者读取到换行符时,或者到达文件末尾时,它会停止,具体视情况而定。
声明 :char fgets(char str, int n, FILE *stream)
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
strlen 计算字符长度
strcat 字符串拼接
strcmp 比较字符串
strcpy 拷贝 // 加n 函数 安全函数 strncpy(a,b, 5) 5 =字符长度
sprintf() 格式化字符串拼接到缓冲区
块 {} 出块就生命周期就没了—相当于局部 , 全局不限制
register 申请使用寄存器
#include <stdio.h>#include <stdlib.h>#include "test.h"int main(int argc, char *argv[]) {char strA[]={'a','b','c','d','e','f','\0'};char strB[]={"abcdef"};char *strC={"abcdef"};char strD[100]={0};// scanf("%s",strD);// printf("%s",strD);// fgets(strD,100,stdin); // fgets 需要三个参数// puts(strD); //puts 自带换行 只有一个参数// int changdu = strlen(strA); //stelen 计算字符串长度// printf("%d",changdu);// strcat(strA,strB); 把strB 拼接给 strA// printf("%s",strA);// 返回字符串相等 = 0// 返回1 或者其他 就是不相等// int nflag =strcmp(strA,strB);// printf("%d",nflag);// int n1= 100;// short n2=10;// sprintf(strD,"%s %d %hd",strB,n1,n2);// strD 可以看成拼接的数组 他可以装下 那些字符串// 后面就是打印格式化字符串类型 ,// printf("%s",strD);return 0;}
申请内存 释放内存
#include <stdio.h>#include <stdlib.h>#include "test.h"int main(int argc, char *argv[]) {//申请内存 释放内存// 长度 字节//50char *p =(char *) malloc(50 *sizeof(char));memset(p,0,50*sizeof(char)); //不用memset 内存里面是乱的 可以使用这个函使内存平稳// p 是指针需要刷的块, 0 是刷成0 可替换 50 长度 52*sizeof(char)scanf("%s",p);printf("%s",p);free(p); //释放return 0;}
读取文件
详细
#include <stdio.h>#include <stdlib.h>#include "test.h"char * MyReadFile(char * szFilePath){FILE *pFile; //文件指针// r w a 读 写 追加//rb wb ab b 二进制pFile = fopen(szFilePath,'rb'); // f FILE poen 打开if(pFile == NULL) //判断有无获取到文件指针{printf(" open file failed! \n ");system("pause");fclose(pFile); // 把文件指针释放掉exit(0);}fseek(pFile,0,SEEK_END); //文件指针 偏移 宏 这里只有 最后一个因为要到最后一个数/*int fseek( FILE *stream, long offset, int origin );第一个参数stream为文件指针第二个参数offset为偏移量,整数表示正向偏移,负数表示负向偏移第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SETSEEK_SET: 文件开头SEEK_CUR: 当前位置SEEK_END: 文件结尾*/int nReadFileSize = ftell(pFile);rewind(pFile); //读取回来 rewind 参数只有一个文件指针//需要一个缓冲区 char 类型 的 buffer 文件多大的长度char * szReadTextBuffer =(char *)malloc((nReadFileSize +1) *sizeof(char));if(szReadTextBuffer == NULL) // 判申请成功没{printf("malloc memory failed!\n");system("pause");fclose(pFile);exit(0);}memset(szReadTextBuffer,0,(nReadFileSize +1)*sizeof(char)); //置空int nReadRet=0;//fread 函数 参数 (缓冲区 1字节方式读 读多长 文件指针)nReadRet=fread(szReadTextBuffer,1,nReadFileSize,pFile);if(nReadRet != nReadFileSize){printf("Read File failed!\n");system("pause");fclose(pFile);exit(0);}fclose(pFile);return szReadTextBuffer;}int main(int argc, char *argv[]) {char *p = MyReadFile("C://");//printf();system("pause");return 0;}
写出文件
#include <stdio.h>#include <stdlib.h>#include "test.h"int MyWriteFile(char *szFilePath, char * szStr) // szFailePath 文件地址 szstr 文件内容{FILE *pFile;pFile = fopen(szFilePath,"wb"); // wb 二进制写出if(pFile == NULL) //判断是否写出{printf("Read File failed!\n");system("pause");fclose(pFile);exit(0);}int nRet = fwrite(szStr,sizeof(szStr)+1,1,pFile); // 接收fclose(pFile);return nRet;}int main(int argc, char *argv[]) {MyWriteFile("D:\\aaa.txt","aaaaaa");system("pause");return 0;}
预处理器 指令
