法羅文翻譯開辟平台(Platform): (Ex: VC++ 翻譯公司 GCC, Linux, ...)
Linux
額外使用到 翻譯函數庫(Library Used): (Ex: OpenGL 翻譯公司 ...)
zlib.h 用來處置緊縮的檔案
(問題是卡在開檔完的部分,可以只視為是 要處置在陣列裡面的字串)
問題(Question):。-> 翻譯社|,-> 翻譯公司|的-> 翻譯
有兩個檔案會讀進 char *indexBuffer, *dataBuffer
index 裡面是存每筆資料的長短
data 裡面是現實的資料
例如在index裡面
1 50 2 30 3 40
暗示第一筆資料在data裡面 翻譯前50個bytes
第二筆是接下來的30個
第三筆是在接下去的40個
我目下當今用
char *tmp=(char *)malloc(data_Size);
strncpy (tmp, dataBuffer, data_Size);
可以抓到第一筆 翻譯資料
但是我不知道要怎麼做可讓我鄙人一個迴圈的時候
可以從50往後面抓30個bytes
如同沒有函式可以直接指定要從哪裡抓到哪裡
我正本的想法是
取完前50個以後,把前50個 翻譯資料從buffer裡面移除
那我就能夠繼續往下面抓了
不外我參考網路上的CODE仿佛有問題
所以想就教一下有什麼辦法可讓我指定要copy的局限
或是把前面不要 翻譯部分從buffer裡面刪除
餵入的資料(Input):
http://cis.poly.edu/cs912/data/nz2.tar
程式碼(Code):(請善用置底文網頁 翻譯公司 記得排版)
#include <zlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INDEX_CHUNK 409600 //50KB
#define DATA_CHUNK 20971520 //2.5MB
char *memAlloc(gzFile *, int);
void openfile(char *str_index, char *str_data);
//char *str_replace (char *source, char *find 翻譯公司 char *rep);
int main (int argc, char * argv[])
{
FILE *cList;
char dataName[20];
char index[] = "_index";
char data[] = "_data";
char tmp1[30] 翻譯公司tmp2[30];
cList=fopen("nz2_merged/indexfile_list.txt","r");
int counter = 0;
char ch;
while(fscanf(cList,"%s",dataName) != EOF) {
strcpy(tmp1, dataName);
strcpy(tmp2, dataName);
strcat(tmp1, index);
strcat(tmp2 翻譯公司 data);
//printf("%s %s %s
",dataName 翻譯公司 tmp1, tmp2);
if(counter == 0)
openfile(tmp1, tmp2);
counter++;
}
printf("This is a %d in main
", counter);
fclose(cList);
}
/******************************************
*Read a gz file into a buffer
*@param fileName the filename to be read
*@param size the initial size of the buffer
*@return the buffer
******************************************/
char *memAlloc(gzFile *fileName, int size)
{
char *buffer=(char *)malloc(size);
int oldSize=size;
int count=0; //The number of bytes that already read
while (!gzeof(fileName))
{
count+=gzread(fileName 翻譯公司buffer+count,oldSize);
//printf("%d %d
" , count, size);
if (count==size) // Reallocate when buffer is full
{
oldSize=size;
size*=2;
buffer=(char *)realloc(buffer 翻譯公司size);
}
}
return buffer;
}
void openfile(char *str_index 翻譯公司 char *str_data)
{
gzFile *cData,*cIndex;
char *indexBuffer, *dataBuffer;
char *space = "
";
char url[1000], ip[16], status[3];
int unknown 翻譯公司 start_Pos, data_Size 翻譯公司 port;
cIndex=gzopen(str_index,"r");
indexBuffer=memAlloc(cIndex, INDEX_CHUNK);
cData=gzopen(str_data,"r");
dataBuffer=memAlloc(cData 翻譯公司 DATA_CHUNK);
/* Split string & Start to build the index */
/**********************************************************/
int count = 0;
char * pch;
char * pos;
pch = strtok(indexBuffer, space);
while (pch != NULL)
{
sscanf(pch,"%s %d %d %d %s %d %s", url, &unknown, &start_Pos,
&data_Size, ip ,&port, status);
//printf("%s
" 翻譯公司 url);
//printf("%d
" , unknown);
//printf("%d
" , start_Pos);
//printf("%d
" 翻譯公司 data_Size);
//printf("%s
" , ip);
//printf("%d
" , port);
//printf("%s
" , status);
pch = strtok (NULL, space);
if(count == 0)
{
char *tmp=(char *)malloc(data_Size);
strncpy (tmp 翻譯公司 dataBuffer, data_Size);
//if(strncmp (tmp 翻譯公司 dataBuffer 翻譯公司 data_Size) == 0)
//printf("YES
");
//dataBuffer = str_replace(dataBuffer, tmp, "");
printf("%s
" 翻譯公司 tmp);
}
/*else
{
char *tmp=(char *)malloc(data_Size);
strncpy (tmp, dataBuffer, data_Size);
dataBuffer = str_replace(dataBuffer 翻譯公司 tmp, "");
}*/
count++;
}
/**********************************************************/
//FILE *fp;
//fp = fopen("data1.txt","w");
//fprintf(fp 翻譯公司"%s", dataBuffer);
printf("This is a %d in openfile
" 翻譯公司 count);
gzclose(str_index);
gzclose(str_data);
fclose(fp);
}
/*
char *str_replace (char *source 翻譯公司 char *find, char *rep){
// 搜尋文字的長度
int find_L=strlen(find);
// 替代文字 翻譯長度
int rep_L=strlen(rep);
// 了局文字的長度
int length=strlen(source)+1;
// 定位偏移量
int gap=0;
// 建立成績文字,並複製文字
char *result = (char*)malloc(sizeof(char) * length);
strcpy(result, source);
// 還沒有被取代的字串
char *former=source;
// 搜索文字泛起 翻譯起始位址指標
char *location= strstr(former, find);
// 漸進搜索欲替代的文字
while(location!=NULL){
// 增加定位偏移量
gap+=(location - former);
// 將竣事符號定在搜索到的位址上
result[gap]='\0';
// 計算新 翻譯長度
length+=(rep_L-find_L);
// 變換記憶體空間
result = (char*)realloc(result, length * sizeof(char));
// 替代的文字串接在結果後面
strcat(result 翻譯公司 rep);
// 更新定位偏移量
gap+=rep_L;
// 更新還沒有被取代的字串的位址
former=location+find_L;
// 將還沒有被代替的文字串接在結果後面
strcat(result 翻譯公司 former);
// 搜索文字呈現 翻譯起始位址指標
location= strstr(former, find);
}
return result;
}
*/
增補申明(Supplement):
目下當今我卡在不知道怎麼樣才可以只copy我想要 翻譯規模
是可以指定我要的規模
或是把用過的地方從buffer裡面刪除,我直接在往後面抓我要的長度
char *str_replace 是我參考網路上的資料,不外仿佛有問題,程式會卡死的樣子
我 翻譯vmware就這樣怪怪了好幾回
麻煩人人給我一點意見,感謝
--
我不是宅 我只是對照居家
本文引用自: https://www.ptt.cc/bbs/C_and_CPP/M.1300150734.A.2CC.html有關翻譯的問題歡迎諮詢天成翻譯社
- Jan 15 Mon 2018 05:11
[問題] C說話 抓特定位置的字串
close
文章標籤
全站熱搜
留言列表
發表留言