Error[8]: Undefined offset: 28, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 114
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

#C语言将记录写入文件中并进行读取记录按关键字搜索例子
##题目

[+++]

##个人体会:
1.利用每条记录中的分隔符,进行拆分并拼接,注意Remove the ‘\n’,at the end of each record.
2.注意拼接时,间隔的字节数,指针很好用,可以精确到每个字节.
3.中文记录运行正常.
4.体会到最简易的搜索方法.
5.C语言的拼接方法不如python中的更易于直接使用,尤其是python进行多级dict的层序遍历时。
6.没有找到类似strcpy中可以按地址加上字节数的方法进行string复制,有待改进.
没有去掉原来注释掉的部分,为了以后有更多的思考.
7.测试环境codelite v16.0.0.

#include 
#include 
#include 

typedef struct product_st
{
    char name[20];
    char place[20];
}Prdct;

int main(int argc, char *argv[])
{
    enum {N = 4};
    Prdct prdt[N];
    int i;
    
    FILE *fp;
    fp = fopen("D:\\prod.txt","w");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    for(i = 0; i < N; i++)
    {
        puts("\nEnter product name and place:\n");
        gets(prdt[i].name);
        fwrite(&prdt[i].name,strlen(prdt[i].name),1,fp); 
        fputc(',',fp);
        gets(prdt[i].place);
        fwrite(&prdt[i].place,strlen(prdt[i].place),1,fp);
        fputc('\n',fp);
    }
    
    fclose(fp);
    puts("Save file successfully!\n");
    
    // Read file
    fp = fopen("D:\prod.txt","r");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    char name[256]= {''};
    
    //Find product name according place.
    // Read one line.
    char pname[20];
    char pplace[20];
    char tmp[20];
    char result[256];
    char *p = result;
    int idx;
    int cnt = 0;
    puts("Enter place like[USA,UK]");
    gets(tmp);
    //tmp[strlen(tmp)+1] = '
'; while(fgets(name,256,fp)) { //read one record to name. //fscanf(fp,"%[^\n]",name); //fscanf(fp,"%[^\n]",name); for(i = 0; i < strlen(name); i++) { if(name[i] == ',') { idx = i; } } //get product name. for(i = 0; i < idx; i++) { *(pname + i) = name[i]; } // Notice. pname[i] = '

'; //get product place. strcpy(pplace, &name[idx+1]); //remove the '\n',at the end. pplace[strlen(pplace)-1]= '

'; printf("pname = %s place = %s\n",pname,pplace); if((strcmp(tmp,pplace)) == 0) { strcpy(p,pname); puts(pname); puts(p); puts("strcpy\n"); //p pointer address increae one pname bytes p = p + strlen(pname); } //puts(tmp); //puts(pplace); // for(i = 0; i < tmp[i] != '<===>'; i++) // { // if(tmp[i] == pplace[i]) // { // cnt++; // } // else // { // break; // } // } // if(cnt == strlen(pplace)) // { // puts(tmp); // puts(pplace); // } } p[strlen(p)+1] = '<===>'; puts("\n"); puts(result); // char tmp[20]= {'<===>'}; // // puts("Enter place like[USA,UK]"); // gets(tmp); // for(i = 0; i < N; i++) // { // if((fread(&prdt[i],sizeof(prdt[i]),1,fp)) > 0) // { // if(strcmp(tmp,prdt[i].place) == 0) // { // strcpy(name,prdt[i].name); // } // } // // } // // printf("%s \n", name); puts("Hello, World!"); fclose(fp); return 0; } <===><===> <===><===>)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 29, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 114
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

#C语言将记录写入文件中并进行读取记录按关键字搜索例子
##题目

[+++]

##个人体会:
1.利用每条记录中的分隔符,进行拆分并拼接,注意Remove the ‘\n’,at the end of each record.
2.注意拼接时,间隔的字节数,指针很好用,可以精确到每个字节.
3.中文记录运行正常.
4.体会到最简易的搜索方法.
5.C语言的拼接方法不如python中的更易于直接使用,尤其是python进行多级dict的层序遍历时。
6.没有找到类似strcpy中可以按地址加上字节数的方法进行string复制,有待改进.
没有去掉原来注释掉的部分,为了以后有更多的思考.
7.测试环境codelite v16.0.0.

#include 
#include 
#include 

typedef struct product_st
{
    char name[20];
    char place[20];
}Prdct;

int main(int argc, char *argv[])
{
    enum {N = 4};
    Prdct prdt[N];
    int i;
    
    FILE *fp;
    fp = fopen("D:\\prod.txt","w");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    for(i = 0; i < N; i++)
    {
        puts("\nEnter product name and place:\n");
        gets(prdt[i].name);
        fwrite(&prdt[i].name,strlen(prdt[i].name),1,fp); 
        fputc(',',fp);
        gets(prdt[i].place);
        fwrite(&prdt[i].place,strlen(prdt[i].place),1,fp);
        fputc('\n',fp);
    }
    
    fclose(fp);
    puts("Save file successfully!\n");
    
    // Read file
    fp = fopen("D:\prod.txt","r");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    char name[256]= {''};
    
    //Find product name according place.
    // Read one line.
    char pname[20];
    char pplace[20];
    char tmp[20];
    char result[256];
    char *p = result;
    int idx;
    int cnt = 0;
    puts("Enter place like[USA,UK]");
    gets(tmp);
    //tmp[strlen(tmp)+1] = '
'; while(fgets(name,256,fp)) { //read one record to name. //fscanf(fp,"%[^\n]",name); //fscanf(fp,"%[^\n]",name); for(i = 0; i < strlen(name); i++) { if(name[i] == ',') { idx = i; } } //get product name. for(i = 0; i < idx; i++) { *(pname + i) = name[i]; } // Notice. pname[i] = '

'; //get product place. strcpy(pplace, &name[idx+1]); //remove the '\n',at the end. pplace[strlen(pplace)-1]= '

'; printf("pname = %s place = %s\n",pname,pplace); if((strcmp(tmp,pplace)) == 0) { strcpy(p,pname); puts(pname); puts(p); puts("strcpy\n"); //p pointer address increae one pname bytes p = p + strlen(pname); } //puts(tmp); //puts(pplace); // for(i = 0; i < tmp[i] != ''; i++) // { // if(tmp[i] == pplace[i]) // { // cnt++; // } // else // { // break; // } // } // if(cnt == strlen(pplace)) // { // puts(tmp); // puts(pplace); // } } p[strlen(p)+1] = '<===>'; puts("\n"); puts(result); // char tmp[20]= {'<===>'}; // // puts("Enter place like[USA,UK]"); // gets(tmp); // for(i = 0; i < N; i++) // { // if((fread(&prdt[i],sizeof(prdt[i]),1,fp)) > 0) // { // if(strcmp(tmp,prdt[i].place) == 0) // { // strcpy(name,prdt[i].name); // } // } // // } // // printf("%s \n", name); puts("Hello, World!"); fclose(fp); return 0; } <===><===> <===><===>)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 30, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 114
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

#C语言将记录写入文件中并进行读取记录按关键字搜索例子
##题目

[+++]

##个人体会:
1.利用每条记录中的分隔符,进行拆分并拼接,注意Remove the ‘\n’,at the end of each record.
2.注意拼接时,间隔的字节数,指针很好用,可以精确到每个字节.
3.中文记录运行正常.
4.体会到最简易的搜索方法.
5.C语言的拼接方法不如python中的更易于直接使用,尤其是python进行多级dict的层序遍历时。
6.没有找到类似strcpy中可以按地址加上字节数的方法进行string复制,有待改进.
没有去掉原来注释掉的部分,为了以后有更多的思考.
7.测试环境codelite v16.0.0.

#include 
#include 
#include 

typedef struct product_st
{
    char name[20];
    char place[20];
}Prdct;

int main(int argc, char *argv[])
{
    enum {N = 4};
    Prdct prdt[N];
    int i;
    
    FILE *fp;
    fp = fopen("D:\\prod.txt","w");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    for(i = 0; i < N; i++)
    {
        puts("\nEnter product name and place:\n");
        gets(prdt[i].name);
        fwrite(&prdt[i].name,strlen(prdt[i].name),1,fp); 
        fputc(',',fp);
        gets(prdt[i].place);
        fwrite(&prdt[i].place,strlen(prdt[i].place),1,fp);
        fputc('\n',fp);
    }
    
    fclose(fp);
    puts("Save file successfully!\n");
    
    // Read file
    fp = fopen("D:\prod.txt","r");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    char name[256]= {''};
    
    //Find product name according place.
    // Read one line.
    char pname[20];
    char pplace[20];
    char tmp[20];
    char result[256];
    char *p = result;
    int idx;
    int cnt = 0;
    puts("Enter place like[USA,UK]");
    gets(tmp);
    //tmp[strlen(tmp)+1] = '
'; while(fgets(name,256,fp)) { //read one record to name. //fscanf(fp,"%[^\n]",name); //fscanf(fp,"%[^\n]",name); for(i = 0; i < strlen(name); i++) { if(name[i] == ',') { idx = i; } } //get product name. for(i = 0; i < idx; i++) { *(pname + i) = name[i]; } // Notice. pname[i] = '

'; //get product place. strcpy(pplace, &name[idx+1]); //remove the '\n',at the end. pplace[strlen(pplace)-1]= '

'; printf("pname = %s place = %s\n",pname,pplace); if((strcmp(tmp,pplace)) == 0) { strcpy(p,pname); puts(pname); puts(p); puts("strcpy\n"); //p pointer address increae one pname bytes p = p + strlen(pname); } //puts(tmp); //puts(pplace); // for(i = 0; i < tmp[i] != ''; i++) // { // if(tmp[i] == pplace[i]) // { // cnt++; // } // else // { // break; // } // } // if(cnt == strlen(pplace)) // { // puts(tmp); // puts(pplace); // } } p[strlen(p)+1] = ''; puts("\n"); puts(result); // char tmp[20]= {'<===>'}; // // puts("Enter place like[USA,UK]"); // gets(tmp); // for(i = 0; i < N; i++) // { // if((fread(&prdt[i],sizeof(prdt[i]),1,fp)) > 0) // { // if(strcmp(tmp,prdt[i].place) == 0) // { // strcpy(name,prdt[i].name); // } // } // // } // // printf("%s \n", name); puts("Hello, World!"); fclose(fp); return 0; } <===><===> <===><===>)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 31, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 114
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

#C语言将记录写入文件中并进行读取记录按关键字搜索例子
##题目

[+++]

##个人体会:
1.利用每条记录中的分隔符,进行拆分并拼接,注意Remove the ‘\n’,at the end of each record.
2.注意拼接时,间隔的字节数,指针很好用,可以精确到每个字节.
3.中文记录运行正常.
4.体会到最简易的搜索方法.
5.C语言的拼接方法不如python中的更易于直接使用,尤其是python进行多级dict的层序遍历时。
6.没有找到类似strcpy中可以按地址加上字节数的方法进行string复制,有待改进.
没有去掉原来注释掉的部分,为了以后有更多的思考.
7.测试环境codelite v16.0.0.

#include 
#include 
#include 

typedef struct product_st
{
    char name[20];
    char place[20];
}Prdct;

int main(int argc, char *argv[])
{
    enum {N = 4};
    Prdct prdt[N];
    int i;
    
    FILE *fp;
    fp = fopen("D:\\prod.txt","w");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    for(i = 0; i < N; i++)
    {
        puts("\nEnter product name and place:\n");
        gets(prdt[i].name);
        fwrite(&prdt[i].name,strlen(prdt[i].name),1,fp); 
        fputc(',',fp);
        gets(prdt[i].place);
        fwrite(&prdt[i].place,strlen(prdt[i].place),1,fp);
        fputc('\n',fp);
    }
    
    fclose(fp);
    puts("Save file successfully!\n");
    
    // Read file
    fp = fopen("D:\prod.txt","r");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    char name[256]= {''};
    
    //Find product name according place.
    // Read one line.
    char pname[20];
    char pplace[20];
    char tmp[20];
    char result[256];
    char *p = result;
    int idx;
    int cnt = 0;
    puts("Enter place like[USA,UK]");
    gets(tmp);
    //tmp[strlen(tmp)+1] = '
'; while(fgets(name,256,fp)) { //read one record to name. //fscanf(fp,"%[^\n]",name); //fscanf(fp,"%[^\n]",name); for(i = 0; i < strlen(name); i++) { if(name[i] == ',') { idx = i; } } //get product name. for(i = 0; i < idx; i++) { *(pname + i) = name[i]; } // Notice. pname[i] = '

'; //get product place. strcpy(pplace, &name[idx+1]); //remove the '\n',at the end. pplace[strlen(pplace)-1]= '

'; printf("pname = %s place = %s\n",pname,pplace); if((strcmp(tmp,pplace)) == 0) { strcpy(p,pname); puts(pname); puts(p); puts("strcpy\n"); //p pointer address increae one pname bytes p = p + strlen(pname); } //puts(tmp); //puts(pplace); // for(i = 0; i < tmp[i] != ''; i++) // { // if(tmp[i] == pplace[i]) // { // cnt++; // } // else // { // break; // } // } // if(cnt == strlen(pplace)) // { // puts(tmp); // puts(pplace); // } } p[strlen(p)+1] = ''; puts("\n"); puts(result); // char tmp[20]= {''}; // // puts("Enter place like[USA,UK]"); // gets(tmp); // for(i = 0; i < N; i++) // { // if((fread(&prdt[i],sizeof(prdt[i]),1,fp)) > 0) // { // if(strcmp(tmp,prdt[i].place) == 0) // { // strcpy(name,prdt[i].name); // } // } // // } // // printf("%s \n", name); puts("Hello, World!"); fclose(fp); return 0; } <===><===> <===><===>)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 32, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 114
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

#C语言将记录写入文件中并进行读取记录按关键字搜索例子
##题目

[+++]

##个人体会:
1.利用每条记录中的分隔符,进行拆分并拼接,注意Remove the ‘\n’,at the end of each record.
2.注意拼接时,间隔的字节数,指针很好用,可以精确到每个字节.
3.中文记录运行正常.
4.体会到最简易的搜索方法.
5.C语言的拼接方法不如python中的更易于直接使用,尤其是python进行多级dict的层序遍历时。
6.没有找到类似strcpy中可以按地址加上字节数的方法进行string复制,有待改进.
没有去掉原来注释掉的部分,为了以后有更多的思考.
7.测试环境codelite v16.0.0.

#include 
#include 
#include 

typedef struct product_st
{
    char name[20];
    char place[20];
}Prdct;

int main(int argc, char *argv[])
{
    enum {N = 4};
    Prdct prdt[N];
    int i;
    
    FILE *fp;
    fp = fopen("D:\\prod.txt","w");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    for(i = 0; i < N; i++)
    {
        puts("\nEnter product name and place:\n");
        gets(prdt[i].name);
        fwrite(&prdt[i].name,strlen(prdt[i].name),1,fp); 
        fputc(',',fp);
        gets(prdt[i].place);
        fwrite(&prdt[i].place,strlen(prdt[i].place),1,fp);
        fputc('\n',fp);
    }
    
    fclose(fp);
    puts("Save file successfully!\n");
    
    // Read file
    fp = fopen("D:\prod.txt","r");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    char name[256]= {''};
    
    //Find product name according place.
    // Read one line.
    char pname[20];
    char pplace[20];
    char tmp[20];
    char result[256];
    char *p = result;
    int idx;
    int cnt = 0;
    puts("Enter place like[USA,UK]");
    gets(tmp);
    //tmp[strlen(tmp)+1] = '
'; while(fgets(name,256,fp)) { //read one record to name. //fscanf(fp,"%[^\n]",name); //fscanf(fp,"%[^\n]",name); for(i = 0; i < strlen(name); i++) { if(name[i] == ',') { idx = i; } } //get product name. for(i = 0; i < idx; i++) { *(pname + i) = name[i]; } // Notice. pname[i] = '

'; //get product place. strcpy(pplace, &name[idx+1]); //remove the '\n',at the end. pplace[strlen(pplace)-1]= '

'; printf("pname = %s place = %s\n",pname,pplace); if((strcmp(tmp,pplace)) == 0) { strcpy(p,pname); puts(pname); puts(p); puts("strcpy\n"); //p pointer address increae one pname bytes p = p + strlen(pname); } //puts(tmp); //puts(pplace); // for(i = 0; i < tmp[i] != ''; i++) // { // if(tmp[i] == pplace[i]) // { // cnt++; // } // else // { // break; // } // } // if(cnt == strlen(pplace)) // { // puts(tmp); // puts(pplace); // } } p[strlen(p)+1] = ''; puts("\n"); puts(result); // char tmp[20]= {''}; // // puts("Enter place like[USA,UK]"); // gets(tmp); // for(i = 0; i < N; i++) // { // if((fread(&prdt[i],sizeof(prdt[i]),1,fp)) > 0) // { // if(strcmp(tmp,prdt[i].place) == 0) // { // strcpy(name,prdt[i].name); // } // } // // } // // printf("%s \n", name); puts("Hello, World!"); fclose(fp); return 0; } <===> <===><===>)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 33, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 114
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

#C语言将记录写入文件中并进行读取记录按关键字搜索例子
##题目

[+++]

##个人体会:
1.利用每条记录中的分隔符,进行拆分并拼接,注意Remove the ‘\n’,at the end of each record.
2.注意拼接时,间隔的字节数,指针很好用,可以精确到每个字节.
3.中文记录运行正常.
4.体会到最简易的搜索方法.
5.C语言的拼接方法不如python中的更易于直接使用,尤其是python进行多级dict的层序遍历时。
6.没有找到类似strcpy中可以按地址加上字节数的方法进行string复制,有待改进.
没有去掉原来注释掉的部分,为了以后有更多的思考.
7.测试环境codelite v16.0.0.

#include 
#include 
#include 

typedef struct product_st
{
    char name[20];
    char place[20];
}Prdct;

int main(int argc, char *argv[])
{
    enum {N = 4};
    Prdct prdt[N];
    int i;
    
    FILE *fp;
    fp = fopen("D:\\prod.txt","w");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    for(i = 0; i < N; i++)
    {
        puts("\nEnter product name and place:\n");
        gets(prdt[i].name);
        fwrite(&prdt[i].name,strlen(prdt[i].name),1,fp); 
        fputc(',',fp);
        gets(prdt[i].place);
        fwrite(&prdt[i].place,strlen(prdt[i].place),1,fp);
        fputc('\n',fp);
    }
    
    fclose(fp);
    puts("Save file successfully!\n");
    
    // Read file
    fp = fopen("D:\prod.txt","r");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    char name[256]= {''};
    
    //Find product name according place.
    // Read one line.
    char pname[20];
    char pplace[20];
    char tmp[20];
    char result[256];
    char *p = result;
    int idx;
    int cnt = 0;
    puts("Enter place like[USA,UK]");
    gets(tmp);
    //tmp[strlen(tmp)+1] = '
'; while(fgets(name,256,fp)) { //read one record to name. //fscanf(fp,"%[^\n]",name); //fscanf(fp,"%[^\n]",name); for(i = 0; i < strlen(name); i++) { if(name[i] == ',') { idx = i; } } //get product name. for(i = 0; i < idx; i++) { *(pname + i) = name[i]; } // Notice. pname[i] = '

'; //get product place. strcpy(pplace, &name[idx+1]); //remove the '\n',at the end. pplace[strlen(pplace)-1]= '

'; printf("pname = %s place = %s\n",pname,pplace); if((strcmp(tmp,pplace)) == 0) { strcpy(p,pname); puts(pname); puts(p); puts("strcpy\n"); //p pointer address increae one pname bytes p = p + strlen(pname); } //puts(tmp); //puts(pplace); // for(i = 0; i < tmp[i] != ''; i++) // { // if(tmp[i] == pplace[i]) // { // cnt++; // } // else // { // break; // } // } // if(cnt == strlen(pplace)) // { // puts(tmp); // puts(pplace); // } } p[strlen(p)+1] = ''; puts("\n"); puts(result); // char tmp[20]= {''}; // // puts("Enter place like[USA,UK]"); // gets(tmp); // for(i = 0; i < N; i++) // { // if((fread(&prdt[i],sizeof(prdt[i]),1,fp)) > 0) // { // if(strcmp(tmp,prdt[i].place) == 0) // { // strcpy(name,prdt[i].name); // } // } // // } // // printf("%s \n", name); puts("Hello, World!"); fclose(fp); return 0; } <===><===>)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 34, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 114
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

#C语言将记录写入文件中并进行读取记录按关键字搜索例子
##题目

[+++]

##个人体会:
1.利用每条记录中的分隔符,进行拆分并拼接,注意Remove the ‘\n’,at the end of each record.
2.注意拼接时,间隔的字节数,指针很好用,可以精确到每个字节.
3.中文记录运行正常.
4.体会到最简易的搜索方法.
5.C语言的拼接方法不如python中的更易于直接使用,尤其是python进行多级dict的层序遍历时。
6.没有找到类似strcpy中可以按地址加上字节数的方法进行string复制,有待改进.
没有去掉原来注释掉的部分,为了以后有更多的思考.
7.测试环境codelite v16.0.0.

#include 
#include 
#include 

typedef struct product_st
{
    char name[20];
    char place[20];
}Prdct;

int main(int argc, char *argv[])
{
    enum {N = 4};
    Prdct prdt[N];
    int i;
    
    FILE *fp;
    fp = fopen("D:\\prod.txt","w");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    for(i = 0; i < N; i++)
    {
        puts("\nEnter product name and place:\n");
        gets(prdt[i].name);
        fwrite(&prdt[i].name,strlen(prdt[i].name),1,fp); 
        fputc(',',fp);
        gets(prdt[i].place);
        fwrite(&prdt[i].place,strlen(prdt[i].place),1,fp);
        fputc('\n',fp);
    }
    
    fclose(fp);
    puts("Save file successfully!\n");
    
    // Read file
    fp = fopen("D:\prod.txt","r");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    char name[256]= {''};
    
    //Find product name according place.
    // Read one line.
    char pname[20];
    char pplace[20];
    char tmp[20];
    char result[256];
    char *p = result;
    int idx;
    int cnt = 0;
    puts("Enter place like[USA,UK]");
    gets(tmp);
    //tmp[strlen(tmp)+1] = '
'; while(fgets(name,256,fp)) { //read one record to name. //fscanf(fp,"%[^\n]",name); //fscanf(fp,"%[^\n]",name); for(i = 0; i < strlen(name); i++) { if(name[i] == ',') { idx = i; } } //get product name. for(i = 0; i < idx; i++) { *(pname + i) = name[i]; } // Notice. pname[i] = '

'; //get product place. strcpy(pplace, &name[idx+1]); //remove the '\n',at the end. pplace[strlen(pplace)-1]= '

'; printf("pname = %s place = %s\n",pname,pplace); if((strcmp(tmp,pplace)) == 0) { strcpy(p,pname); puts(pname); puts(p); puts("strcpy\n"); //p pointer address increae one pname bytes p = p + strlen(pname); } //puts(tmp); //puts(pplace); // for(i = 0; i < tmp[i] != ''; i++) // { // if(tmp[i] == pplace[i]) // { // cnt++; // } // else // { // break; // } // } // if(cnt == strlen(pplace)) // { // puts(tmp); // puts(pplace); // } } p[strlen(p)+1] = ''; puts("\n"); puts(result); // char tmp[20]= {''}; // // puts("Enter place like[USA,UK]"); // gets(tmp); // for(i = 0; i < N; i++) // { // if((fread(&prdt[i],sizeof(prdt[i]),1,fp)) > 0) // { // if(strcmp(tmp,prdt[i].place) == 0) // { // strcpy(name,prdt[i].name); // } // } // // } // // printf("%s \n", name); puts("Hello, World!"); fclose(fp); return 0; } <===>)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
C语言将记录写入文件中并进行读取记录按关键字搜索例子_C_内存溢出

C语言将记录写入文件中并进行读取记录按关键字搜索例子

C语言将记录写入文件中并进行读取记录按关键字搜索例子,第1张

#C语言将记录写入文件中并进行读取记录按关键字搜索例子
##题目

;/*键盘输入10组商品信息,包括产地,名称,保存到d:prod.txt里.
 * 键盘输入一个地名,读取文件,找到所有此地生产的商品信息并输出.
 */

##个人体会:
1.利用每条记录中的分隔符,进行拆分并拼接,注意Remove the ‘\n’,at the end of each record.
2.注意拼接时,间隔的字节数,指针很好用,可以精确到每个字节.
3.中文记录运行正常.
4.体会到最简易的搜索方法.
5.C语言的拼接方法不如python中的更易于直接使用,尤其是python进行多级dict的层序遍历时。
6.没有找到类似strcpy中可以按地址加上字节数的方法进行string复制,有待改进.
没有去掉原来注释掉的部分,为了以后有更多的思考.
7.测试环境codelite v16.0.0.

#include 
#include 
#include 

typedef struct product_st
{
    char name[20];
    char place[20];
}Prdct;

int main(int argc, char *argv[])
{
    enum {N = 4};
    Prdct prdt[N];
    int i;
    
    FILE *fp;
    fp = fopen("D:\\prod.txt","w");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    for(i = 0; i < N; i++)
    {
        puts("\nEnter product name and place:\n");
        gets(prdt[i].name);
        fwrite(&prdt[i].name,strlen(prdt[i].name),1,fp); 
        fputc(',',fp);
        gets(prdt[i].place);
        fwrite(&prdt[i].place,strlen(prdt[i].place),1,fp);
        fputc('\n',fp);
    }
    
    fclose(fp);
    puts("Save file successfully!\n");
    
    // Read file
    fp = fopen("D:\prod.txt","r");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    char name[256]= {''};
    
    //Find product name according place.
    // Read one line.
    char pname[20];
    char pplace[20];
    char tmp[20];
    char result[256];
    char *p = result;
    int idx;
    int cnt = 0;
    puts("Enter place like[USA,UK]");
    gets(tmp);
    //tmp[strlen(tmp)+1] = '
'; while(fgets(name,256,fp)) { //read one record to name. //fscanf(fp,"%[^\n]",name); //fscanf(fp,"%[^\n]",name); for(i = 0; i < strlen(name); i++) { if(name[i] == ',') { idx = i; } } //get product name. for(i = 0; i < idx; i++) { *(pname + i) = name[i]; } // Notice. pname[i] = '

'; //get product place. strcpy(pplace, &name[idx+1]); //remove the '\n',at the end. pplace[strlen(pplace)-1]= '

'; printf("pname = %s place = %s\n",pname,pplace); if((strcmp(tmp,pplace)) == 0) { strcpy(p,pname); puts(pname); puts(p); puts("strcpy\n"); //p pointer address increae one pname bytes p = p + strlen(pname); } //puts(tmp); //puts(pplace); // for(i = 0; i < tmp[i] != ''; i++) // { // if(tmp[i] == pplace[i]) // { // cnt++; // } // else // { // break; // } // } // if(cnt == strlen(pplace)) // { // puts(tmp); // puts(pplace); // } } p[strlen(p)+1] = ''; puts("\n"); puts(result); // char tmp[20]= {''}; // // puts("Enter place like[USA,UK]"); // gets(tmp); // for(i = 0; i < N; i++) // { // if((fread(&prdt[i],sizeof(prdt[i]),1,fp)) > 0) // { // if(strcmp(tmp,prdt[i].place) == 0) // { // strcpy(name,prdt[i].name); // } // } // // } // // printf("%s \n", name); puts("Hello, World!"); fclose(fp); return 0; }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1323463.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存