Home » U++ Library support » U++ SQL » DBF crash error while writing record
DBF crash error while writing record [message #43812] |
Thu, 23 October 2014 19:25 |
|
forlano
Messages: 1185 Registered: March 2006 Location: Italy
|
Senior Contributor |
|
|
Hello,
I know that DBF format is no longer popular. Unfortunately I had to deal with it and get rid a very old Xbase library.
I created a very simple application that convert a text file in a dbf file. It links and run, but crash while tempting to write.
The DBF db is created reading the info contained in the txt file that look like this:
8
D_EVENT_ID C 9
D_SEC_NUM C 2
D_PAIR_NUM C 4
D_REC_SEQ C 1
D_MEM_ID C 8
D_RND01 C 5
D_RND02 C 5
D_RND03 C 5
023434001 1 1 1 156756756 W 4 L 2 W 6
023434001 1 2 1 456456456 W 5 W 1 L 3
023434001 1 3 1 567567567 D 6 W 4 W 2
023434001 1 4 1 324234234 L 1 L 3 W 5
023434001 1 5 1 234234234 L 2 D 6 L 4
023434001 1 6 1 123123455 D 3 D 5 L 1
That is I read the number of fields, how they are made and then the records (fields separated by '\t').
A test case is attached with the txt files to be converted. Here is the simple program so that you can preview it:
#include <CtrlLib/CtrlLib.h>
#include <plugin/dbf/dbf.h>
using namespace Upp;
void ConvertDB(String fn);
GUI_APP_MAIN
{
ConvertDB("thexport");
ConvertDB("tsexport");
ConvertDB("tdexport");
}
void ConvertDB(String fn)
{
String infile = fn + ".txt";
String outfile = fn + ".dbf";
FileIn in;
in.Open(infile);
if (in.IsError()) {
Exclamation("Error reading the file " + infile);
exit(0);
}
int numFields = StrInt(in.GetLine()); // read numebr of fields
int i;
Array<DbfStream::Field> fields;
for (i=0; i<numFields; i++)
{ // legge le specifiche dei campi
Vector<String> f = Split(in.GetLine()," ");
//Exclamation(f[0]+"-"+f[1]+"-"+f[2]);
if ( f[1]=="C") fields << DbfStream::Field::Text(f[0], StrInt(f[2]) );
else if ( f[1]=="N") fields << DbfStream::Field::Number(f[0], StrInt(f[2]), 0);
else if ( f[1]=="D") fields << DbfStream::Field::Date(f[0]);
else Exclamation("error");
}
DbfStream output; //create DB
bool isOpened = output.Create(outfile, fields, CHARSET_CP852);
if(!isOpened)
{
PromptOK("Error creating file [* \1" + outfile + "\1].");
exit(1);
}
while (in.IsOK()) //continue to read the txt file and write the dbf
{
Vector<Value> values;
Vector<String> f = Split( in.GetLine(), "\t", false);
if (f.GetCount()<=1) break;
for (int k=0; k<f.GetCount(); k++) values.Add(f[k]);
output.WriteRow(values); // <----- ??? crash program
}
output.Close();
in.Close();
}
Thanks,
Luigi
-
Attachment: DBF.rar
(Size: 1.89KB, Downloaded 271 times)
|
|
|
Goto Forum:
Current Time: Sat May 11 13:29:49 CEST 2024
Total time taken to generate the page: 0.01821 seconds
|