zoukankan      html  css  js  c++  java
  • c 读文件

    c 读文件

    #include <stdio.h>
    #include 
    <stdlib.h>
    #include 
    <string.h>

    #define szBUF 30000



    int main(int argc, char * argv[])
    {
        
    char filename[szBUF]={0};
        
    char buf[szBUF]={0};
        
    char a[szBUF];

        
    char *need_find_start = "<RoomType>";
        
    int lens_start = strlen(need_find_start);
        
    char *need_find_end = "</RoomType>";
        
    int lens_end = strlen(need_find_end);


        
    char *head  = NULL;
        
    char *end = NULL;

        
    //    int r = 0;
        
    //    int b=0;
        FILE * f = 0;
        
    if(argc < 2) {
            printf(
    "please input the file name:");
            gets(filename);
        }
        
    else
            strcpy(filename, argv[
    1]);
        
    //end if
        f = fopen(filename, "r");
        
    if(!f) {
            printf(
    "file %s open fault!\n", filename);
            
    return 0;
        }
    //end if



        
    while(fgets(buf,30000,f) != NULL)
        {
            head 
    = strstr(buf, need_find_start);
            
    if(head != NULL)
            {
                end 
    = strstr(buf, need_find_end);
                
    if(end != NULL)
                {
                    
    long long int lens = (long long int)(end) - (long long int)(head) - lens_start;
                    
    char *strings = (char*)malloc(lens + 1);
                    strncpy(strings, head 
    + lens_start, lens);
                    printf(
    "123\n");
                    printf(
    "%s\n\n", strings);
                }
                
    else
                {
                    
    while(fgets(buf,30000,f) != NULL)
                    {
                        end 
    = strstr(buf, need_find_end);
                        
    if(end != NULL)
                        {
                            
    long long int lens = (long long int)(end) - (long long int)(head) - lens_start;
                            
    char *strings = (char*)malloc(lens + 1);
                            strncpy(strings, head 
    + lens_start, lens);
                            printf(
    "123\n");
                            printf(
    "%s\n\n", strings);
                        }
                    }
                }
            }

            
    /*
               head = strstr(buf, "<RoomType>");
               int room_lens = strlen("<RoomType>");
               if(head != NULL)
               {
               end = strstr(head, "</RoomType>" );
               if(end != NULL)
               {
               long long int lens = (long long int)(end) - (long long int)(head) - room_lens;
               strings = (char*)malloc(lens + 1);
               strncpy(strings, head + room_lens, lens);
               }
               }
               
    */
        }

        fclose(f);
        
    return 0;
    }

      

  • 相关阅读:
    Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)
    P1494 [国家集训队]小Z的袜子(luogu)
    【题解】洛谷P1311 [NOIP2011TG] 选择客栈(递推)
    【题解】洛谷P2296 [NOIP2014TG] 寻找道路(SPFA+DFS)
    【题解】洛谷P2661 [NOIP2015TG] 信息传递
    【题解】洛谷P1065 [NOIP2006TG] 作业调度方案(模拟+阅读理解)
    【题解】洛谷P1032 [NOIP2002TG]字串变换(BFS+字符串)
    [BZOJ2127]happiness-[网络流-最小割]
    [BZOJ3218]a + b Problem-[主席树+网络流-最小割]
    BZOJ4049][CERC2014]Mountainous landscape-[线段树+凸包+二分]
  • 原文地址:https://www.cnblogs.com/wangkangluo1/p/2104619.html
Copyright © 2011-2022 走看看