c 讀檔案

來源:互聯網
上載者:User

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;
}

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.