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

      

  • 相关阅读:
    PyTorch在NLP任务中使用预训练词向量
    使用Google的Colab+pytorch
    深度学习与Pytorch入门实战(十六)情感分类实战(基于IMDB数据集)
    深度学习与Pytorch入门实战(十五)LSTM
    深度学习与Pytorch入门实战(十四)时间序列预测
    深度学习与Pytorch入门实战(十三)RNN
    PyTorch的nn.Linear()详解
    Pytorch中with torch.no_grad()或@torch.no_grad() 用法
    深度学习与Pytorch入门实战(十二)实现ResNet-18并在Cifar-10数据集上进行验证
    深度学习与Pytorch入门实战(十一)数据增强
  • 原文地址:https://www.cnblogs.com/wangkangluo1/p/2104619.html
Copyright © 2011-2022 走看看