#include <stdio.h>
#include <errno.h>
#include <string.h>
#include<stdlib.h>
#include<stddef.h>
//extern int errno;
#if 0
int countSpace(char* s){
int i = 0;
int count = 0;
while(s[i] != '\0'){
if(s[i] == ' '){
count++;
}
i++;
}
return count;
}
char* replaceSpace(char* s,char* array){
char *t = s;
int count = countSpace(t);
const int length = strlen(t) + count*2 + 1;
array = (char*)malloc(length);
int i, j = 0;
for(i = 0,j = 0;i <= strlen(s);i++,j++){
if(s[i] == ' '){
array[j] = '%';
array[j + 1] = '2';
array[j + 2] = '0';
j = j + 2;
}
else{
array[j] = s[i];
}
}
return array;
}
#endif
//char* callocateSpace(char*);
//char* searchStr(char*,char*);
struct person{
char name[5];
int age;
double score;
};
//传入函数指针,完成函数回调
#pragma region
void printAllArray(void *data,int length,int size,void(*printValue)(void*)){
char *Arr = (char*)data;
for(int i = 0;i < length;i++){
char *record = Arr + size*i;
printValue(record);
}
}
void printfint(void *data){
int *value = data;
printf("%d\t",*value);
}
void printfchar(void *data){
char *value = data;
printf("%c\t",*value);
}
void printfdouble(void *data){
double *value = data;
printf("%f\t",*value);
}
void printfperson(void *data){
struct person *p = (struct person*)data;
printf("\n%s\t%d\t%f",p->name,p->age,p->score);
}
#pragma endregion
int main ()
{
#pragma region 不同指针初始化为相同的常量字符串,在内存中只有一个字符串
// char *p1 = "hello world!";
// char *p2 = "hello world!";
// char *p3 = "hello world!";
// printf("%p %p %p",p1,p2,p3);
#pragma endregion
#pragma region 高地址对数据高位,小端对齐模式
// int num = 0x11223344;
// char *p1 = #
// char *p2 = p1 + 1;
// printf("%d %d",*p1,*p2);
#pragma endregion
#pragma region 空指针作为参数传递,要用二级指针修饰
// char *p = NULL;
// p = callocateSpace(p);
// printf("%s\n",p);
// free(p);
#pragma endregion
#pragma region 以字符数组作为字符串时,要么加上结尾字符'\0',
//要么指定字符数组大小,空出的位置会自动补零
//否则就会造成字符串越界
// char str[] = {'w','e',' ','c','a','n'};
// char str1[] = {'a','s','d','g','h'};
// char str2[] = {'w','e',' ','c','a','n'};
// printf("%s %s %s\n",str,str1,str2);
// printf("%p %p %p",str,str1,str2);
// char str[] = {'w','e',' ','c','a','n'};
// char str1[15] = {'a','s','d','g','h'};
// char str2[] = {'w','e',' ','c','a','n','\0'};
// printf("%s %s %s\n",str,str1,str2);
// printf("%p %p %p",str,str1,str2);
#pragma endregion
#pragma region 输出结果 hello 12 5
//"hello\012world" \012 时转义换行符
// char str[] = "hello\0world";
// printf("%s %d %d",str,sizeof(str),strlen(str));
#pragma endregion
int number[] = {2,5,7,9,12,6,42,91,56};
char sign[] = {'w','d','h','u','e','t','\0'};
double grade[] = {23.45,6.675,12.92,68.63,29.73};
struct person group[3] = {
{"韩信",28,89.43},
{"李白",27,92.45},
{"悟空",300,94.345}
};
//函数指针和回调函数 提高代码复用性
#pragma region
printAllArray((void*)number,sizeof(number)/sizeof(int),sizeof(int),printfint);
printAllArray((void*)sign,sizeof(sign)/sizeof(char),sizeof(char),printfchar);
printAllArray((void*)grade,sizeof(grade)/sizeof(double),sizeof(double),printfdouble);
printAllArray((void*)group,sizeof(group)/sizeof(struct person),sizeof(struct person),printfperson);
#pragma endregion
FILE * pf = NULL;
int errnum;
pf = fopen ("fileoperatewrite.txt", "a");
if (pf == NULL)
{
errnum = errno;
fprintf(stderr, "错误号: %d\n", errno);
perror("通过 perror 输出错误");
fprintf(stderr, "打开文件错误: %s\n", strerror( errnum ));
}
else
{
#pragma region 文件读取操作
// int value = fgetc(pf);
// char buf[255] = {0};
// char *ch = fgets(buf,200,pf);
// char buff[255] = {0};
// int a = fscanf(pf, "%s", buff);
// printf("%c\n %s\n %s\n",value,buf,buff);
// fclose (pf);
#pragma endregion
#pragma region 文件写入操作
// int returnvalue = fputc(47,pf);
// char writevalue[255] = "sgfoeghneo wofghowfgowij oehgfjowehnow";
// int result = fputs(writevalue,pf);
// int re = fprintf(pf, "This is testing for fprintf...\n");
#pragma endregion
}
return 0;
}
//动态分配内存
// char* callocateSpace(char* x)
// {
// //动态分配的内存不会主动释放
// x = (char*)malloc(sizeof(char)*64);
// memset(x,0,64);
// strcpy(x,"hello world!");
// return x;
// }
//在源字符串中查找特定字串
#if 0
char* searchStr(char *scoure,char *dest){
if(strlen(scoure) < strlen(dest)){
return NULL;
}
int i;
for(i = 0;i < strlen(scoure);i++){
if(scoure[i] == dest[0]){
int j;
for(j = 1;j < strlen(dest)&&(i + j) < strlen(scoure);j++){
if(scoure[i + j] != dest[j]){
break;
}
}
if(j == strlen(dest)){
return scoure + i;
}
else if((i + j) == strlen(scoure)){
return NULL;
}
}
}
return NULL;
}
#endif
#pragma region 获取文件行数
// int getFileLow(FILE *file){
// char buf[1024];
// int low = 0;
// while(fgets(buf,1024,file) != NULL){
// low++;
// }
// return low;
// }
#pragma endregion
标签:str,记录,int,C语言,char,pf,pragma,字符串,sizeof
From: https://www.cnblogs.com/x47083/p/17555815.html