Brý deň, chlapi, na základe sn3dovho článku som si chcel urobiť programček, ktorý by napr. z tohto formátu "23,32,43,2,34,43,43" vyberal čísla.
text je v súbore. neviete mi povedať, kde je tá chybička? poprípade viac..
#include <iostream>
using namespace std;
#include <fstream>
#include <pcre.h>
#define REG_EXP "[0-9]{1,2}"
#define VEC_SIZE 30
int main()
{
/*--------------------------------*/
pcre* re_handle;
const char* err;
int err_offset;
int ovector[VEC_SIZE];
int res;
int length;
char* a;
int pos=0;
char str[100];
int count_num=0;
int numbers[30];
int num;
/*--------------------------------*/
cout << "Zadaj meno suboru: ";
cin.getline(str,100);
ifstream infile;
infile.open(str,ios_base::binary);
cout << "File is open!\n";
if (infile.good())
{
infile.seekg (0, ios::end);
length = infile.tellg();
infile.seekg (0, ios::beg);
a=new char[length];
infile.read(a,length);
cout << "sstring is loaded!\n";
}else cout << "something went wrong!";
infile.close();
// compilation of expression
re_handle=pcre_compile(REG_EXP,0,&err,&err_offset,NULL);
if (!re_handle)
{
cout << "Error: " << err;
return 0;
}
const_cast<const char*> ( a );
while((res = pcre_exec(re_handle,NULL,a,length,pos,0,ovector,VEC_SIZE))>=0)
{
memcpy(&numbers[count_num],a+ovector[0],ovector[1]-ovector[0]);
count_num++;
pos=ovector[1];
}
for (int i=0;i<count_num;i++) cout << numbers[i] << " " ;
delete[] a;
return 0;
}
Fórum › C / C++
Pcre help!

Dajak som to šalamúnsky vyriešil. namiesto to aby som to rovno zapisoval do číselného poľa, tak som to dal do znakového a potom konvertovam pomocou fkcie atoi na cislo a funguje to. Pre niekoho koho by zaujímalo riešenie a vedel by to nejak vylepšiť dám vám tu ten kódik:
#include <iostream>
using namespace std;
#include <fstream>
#include <pcre.h>
#include <cstdlib>
#define REG_EXP "[0-9]{1,2}"
#define VEC_SIZE 30
int main()
{
/*--------------------------------*/
pcre* re_handle;
const char* err;
int err_offset;
int ovector[VEC_SIZE];
int res=0;
int length;
char* a;
int pos=0;
char str[100];
int count_num=0;
int numbers[30];
int num=0;
int len=0;
ifstream infile("Skuska\\ofstream.txt",ios_base::in);
char t[10];
/*--------------------------------*/
//cout << "Zadaj meno suboru: ";
//cin.getline(str,100);
//infile.open(str,ios_base::binary);
cout << "File is open!\n";
if (infile.good())
{
infile.seekg (0, ios::end);
length = infile.tellg();
infile.seekg (0, ios::beg);
a=new char[length];
infile.read(a,length);
cout << "sstring is loaded!\n";
}else cout << "something went wrong!";
infile.close();
// compilation of expression
re_handle=pcre_compile(REG_EXP,0,&err,&err_offset,NULL);
if (!re_handle)
{
cout << "Error: " << err;
return 0;
}
const_cast<const char*> ( a );
while( (res = pcre_exec ( re_handle,NULL,a,length,pos,0,ovector,VEC_SIZE ) ) >=0 )
{
len=ovector[1]-ovector[0];
memcpy(t,(a+ovector[0]),(len) );
t[len]='\0';
numbers[num]=atoi(t);
cout << numbers[num] << " ";
num++;
pos=ovector[1];
}
delete[] a;
return 0;
}

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Moderátoři diskuze