2014-04-11 56 views
0

我真的試圖尺寸836的文件分配到大小200連續的緩衝區和搜索CR和CRLF字符之間則跳過他們並寫入新文件!連續讀取和刪除字符,然後寫入新文件

文件這樣

 
CR 
CRLF 
1bb8CR 
CRLF 
!DOC...........text text etx> 
html text ...........text text etx...........text text etx....>CR 
CRLF 
1704CR 
CRLF 
texte classes=====.......> 
.................> 
CR 
CRLF 
0CR 
CRLF 
CR 
CRLF 
/EOF 
#include <stdlib.h> 
#include <stdio.h> 
int main() 
{ 
    const int  BUF_SIZE = 200; 
    FILE   *fptr_in; 
    FILE   *fptr_out; 
    char   buffer[BUF_SIZE + 1]={0}; 
    char   CRLF[BUF_SIZE]={0}; 
    char   lastChar = '\0'; 
    int    i = 0, j = 0, z = 0, n = 0, sub; 
    size_t   result = 0; 
    long   lSize; 
    if((fptr_in = fopen("LogFile_ProxyBufferContents_FJ_small.html", "r")) == NULL){ 
     printf ("\nError opening file"); 
     return 0; 
    } 
    else{ 
     while(fgetc(fptr_in) != EOF){ 
      n++; 
     } 
     if(feof(fptr_in)){ 
      printf_s("\nTotal number of bytes read: %d", n); 
      printf_s(" Bytes.\n"); 
     } 
    } 
    if((fptr_out = fopen("LogFile_ProxyBufferContents_Out.html", "w")) == NULL){ 
     fclose(fptr_in); 
     return 0; 
    } 
    // Obtain the File size 
    fseek(fptr_in, 0, SEEK_END); 
    lSize = ftell(fptr_in); 
    rewind(fptr_in); 
    // Buffer Null check 
    if(buffer == NULL){ 
     fclose(fptr_in); 
     return 0; 
    } 
    // Read File into Buffer by result size 
    while((result = fread_s(buffer, bufSize, 1, bufSize, fptr_in)) != 0){ 
     while(i < (long)result){ 
      if(buffer[i] == '\r' && buffer[i + 1] == '\n'){ 
       if(buffer[i + 6] == '\n'){ 
        i += 6; 
       } 
       else if(buffer[i + 6] == '\r'){ 
        i += 7; 
       } 
      } 
      else{ 
       sub = z -i; 
       CRLF[j] = buffer[i]; 
       j++; 
      } 
      i++; 
     } 

    fclose(fptr_in); 
    fclose(fptr_out); 
    //printf("\nBuffer after removing CRLF %s\n", CRLF); 
    system("pause"); 
    return 0; 
} 

因此,如果CR CRLF的開頭塊沒有問題,但在不使用緩衝器結束第[i + 6]不能弄清楚?

我的想法是,如果ii+1是true,則檢查是否下一個6 buffer[i] != '\0',(我使用6因爲每次保證CR和CRLF之間來到3個或4個字符)如果是的話,例如,如果緩衝器的結束是5 CR和下一個CRLF位於進入下一個緩衝區索引2後,平均指數:緩衝一個:texte ...文本> CR170 \ 0和下一個緩衝區2 4CRCRLF然後HTML的休息...... 我新編程,如何跳到下一個讀取buffer2並跳過CR之前的字符,並將其他地方的進程工作保存到文件中,對不起我的英文。 請幫忙嗎?

編輯: 也許我還不能正確解釋......我想找到第一CRLF和第二個然後跳過中間的十六進制數,在我的情況下,它是通過代理的套接字緩衝區,有來始終Chunk的長度由CRLF包圍。你能建議我該怎麼做? 如果塊長度進來頂部或中間或最後是好的,但如果塊長度分成一半我堆棧!

++++示例文件: https://drive.google.com/file/d/0Bw62NZwp1GSnaG1ydXVHREZibEE/edit?usp=sharing

+0

您的編碼不符合您的問題說明。你爲什麼檢查緩衝區[I + 6]?這不是你問題描述的一部分。另外:在第3行檢查緩衝區[I + 1]可能會超出緩衝區。 –

+0

謝謝你對我的問題重播,也許我不能正確解釋......我想找到第一個CRLf,然後第二個跳過中間的十六進制數字,在我的情況下它是一個通過代理的套接字緩衝區,由CRLF包圍的Chunk的長度。你能建議我該怎麼做?謝謝 –

+0

分塊的HTML?你想實現什麼?是否想要讀取分塊的HTML流並輸出非分塊的HTML流? –

回答

0

一個完整的代碼我的問題,我想萬一幫助一些人分享......

#include <stdio.h> 
#include <ctype.h> 
#include <tchar.h> 
#include <windows.h> 

const char* LOG_FILE = "ORIGIN.html"; 
const char* OUT_FILE = "Out.html"; 

//#define BUF_SIZE 200 
#define BUF_SIZE 4096 

// Search for CRLF line endings, if CRLF found its return the position of the next CRNL, else return none found 
int find_CRLF(TCHAR* buffer, int size) { 
    int result; 
    for(TCHAR* pos = buffer; size > 1; ++pos, --size) { 
     // Return the line followed by CRLF 
     if(pos[0] == '\r') { 
      if(pos[1] == '\n') { 
       result = pos - buffer; 
       return result; 
      } 
     } 
    } 
    // None is found 
    return -1; 
} 

// Check the given string whether a Hex number 
int is_HEX(TCHAR* buffer, int size) { 
    if(! size){ 
     return 0; 
    } 
    for(int i = 0; i < size; ++i) { 
     if(! iswxdigit(buffer[i])) { 
      return 0; 
     } 
    } 
    //If buffer is_HEX return True 
    return 1; 
} 

// Write the line from input to output if isn't HEX 
// buffer input data assumed to start at new line after CRLF or begining of File 
int get_Line(TCHAR* buffer, int size, TCHAR* out, int* byteCopied) { 
    // Declare line by CRLF endings 
    int line_end = find_CRLF(buffer, size); 
    // If no end could be found return -1 
    if(line_end < 0){ 
     return line_end; 
    } 
    // If line is_HEX skip 2 lines 
    if(is_HEX(buffer, line_end)) { 
     fprintf(stderr, "Hex skipped\n"); 
     return line_end + 2; /* skip */ 
    } 
    // Else !is_HEX, copy buffer 
    for(int i = 0; i < line_end + 2; ++i, ++(*byteCopied)) { // Dereference pointer & increment the value pointer 
     if(buffer[i] == '\r') {  
      return line_end + 2; 
     } 
     out[i] = buffer[i]; /* copy buffer*/ 
    } 
    return line_end + 2; 
} 

// Return number of bytes processed, & reminder is non COMPLETE LINES 
int filter_Buffer(TCHAR* buffer, int size, TCHAR* out, int* byteCopied) { 
    TCHAR* pos = buffer; 
    *byteCopied = 0; 
    for(;;) { 
     int next_start = get_Line(pos, size, out + *byteCopied, byteCopied);//out+address, value) 
     if(next_start < 0) { 
      return pos - buffer; 
     } 
     pos += next_start; 
     size -= next_start; 
    } 
} 

// Handle the Reminder, return Number of unprocessed Bytes in in_buf or -1 on EOF 
int filter_BufferFile(FILE* input, FILE* output, TCHAR* in_buf, int in_pos) { 
    TCHAR out_buf[BUF_SIZE]; 
    int size = fread(in_buf + in_pos, 1, BUF_SIZE - in_pos, input); 
    size += in_pos; 
    if(! size) { 
     return -1; 
    } 
    int byteCopied; 
    int end_pos = filter_Buffer(in_buf, size, out_buf, &byteCopied); 
    if(end_pos) { 
     for(int i = 0; i < size - end_pos; ++i) { 
      in_buf[i] = in_buf[i + end_pos]; 
     } 
    } 
    else { 
     if(size == BUF_SIZE) { 
      fwrite(in_buf, 1, size, output); 
      return 0; 
     } 
     else { 
      /* no newline at EOF */ 
      get_Line(in_buf, size, out_buf, &byteCopied); 
     } 
    } 
    //If no check for buffer[i&i+1]!=\r&\n -> minus 2 bytes 
    //fwrite(out_buf, 1, byteCopied-2, output); 
    fwrite(out_buf, 1, byteCopied, output); 
    return size - end_pos; 
} 

// Call untill the whole input file is processed 
void filter_File(FILE* input, FILE* output) { 
    char in_buf[BUF_SIZE]; 
    int in_pos = 0; 
    do { 
     in_pos = filter_BufferFile(input, output, in_buf, in_pos); 
    } while(in_pos >= 0); 
} 

int main(void) { 

    FILE* input = fopen(LOG_FILE, "r"); 
    if(! input) { 
     return 1; 
    } 

    FILE* output = fopen(OUT_FILE, "w"); 
    if(! output) { 
     return 1; 
    } 

    filter_File(input, output); 

    fclose(output); 
    fclose(input); 

    system("pause"); 
} 

演示文件:

Download the file

相關問題