Hi all.

Majorly bamboozled by the wxwidgets file streams. does wxFileInputStream have a GetLine() function, similar to the normal c++ file stream class???


had a wander thru the wx docs, and just ended up more confused.

heres what i want to do btw.
(note, functions are seperate but operate on same file)

here is an example of a possible line for the file being read:

12345`99`KM00099/09`Hello World`Im a line`1token1`2token2`comments

Function 1

open file, containing fields seperated by "`" symbols.
for each line in file:
extract the first field (a set of int's,).
convert this to integer var.
compares this to a variable containg the number of the last line.
keeps the larger of the two

returns the largest number in the first field

Function 2:

Exactly the same as above, but with second field rather than 1st.

Before function 3 is done, the user and program use the above to create a line similar to:
12346`100`KM00100/09`Hello World2`Im a line too`12ndlinetoken1`2blahblahtoken2`comments new entry

Function 3:

takes a line similar to above, as a wxstring, and appends it to the end of the file.


hope this all makes sence

thanks all muchly in advance.

Kev

got the brackets wrong. see next post

By the way, forgot to mention that i know i cant output (do function 3) with wxFileInputStream. i need to use wxFileOutputStream.

i got it to output something, but when i open the file, it seems corrupted. however in the preview in the icon of the file (running ubuntu) i can read the correct numbers (the icon displays "12346`... " only, so i dont know if the rest of the file is correct)

Heres the code that does that:

wxsNewEntryLine<<intLastLineID;
wxsNewEntryLine +=_("`");
wxsNewEntryLine+= cbxSpeciesBotanical->GetValue();
wxsNewEntryLine +=_("`");
wxDateTime wxdDate= dteDate->GetValue();
wxString wxsDate = wxdDate.FormatDate();
wxsNewEntryLine += wxsDate;
wxsNewEntryLine +=_("`");
wxsNewEntryLine+=tbxLocation->GetLineText(0);
wxsNewEntryLine +=_("`");
wxsNewEntryLine+=tbxLongitude->GetLineText(0);
wxsNewEntryLine +=_("`");
wxsNewEntryLine+= tbxLatitude->GetLineText(0);
wxsNewEntryLine +=_("`");

int ccc = tbxComments->GetNumberOfLines();
for (int aaa=0;aaa<ccc;aaa++)
{

wxsNewEntryLine+= tbxComments->GetLineText(aaa);
wxsNewEntryLine +=_("&");
}


strFile = CONFIG_HOMEDIR;

strFile+= ".data/CollectionData2.txt";

wxString wxsFileName(strFile.c_str(),wxConvUTF8);


wxFile wxofsFile(wxsFileName,wxFile::write_append);
wxofsFile.SeekO(0,wxFromEnd);
wxofsFile.Write(wxsNewEntryLine,wxsNewEntryLine.Len());
wxofsFile.Sync();
wxofsFile.Close();

sorry if i didnt get the code brackets right. i tried =]

thanks

Well:

i stumbled across wxTextFile, so that answers my last querry. however, when using the addline function, it deletes everything but the lines i add. is there a way to stop its doing this, or is there a work-around (maybe read file to wxstring array and then add each element of array?? tried below). im well and truely stuck AGAIN *grrr*

so, heres the code that makes a new entry in my text file. note ive tried to use the string array as above, without result. =[

wxString wxsNewEntryLine;
    wxString wxsFileName;
    int intNewEntryLineID=-1;
    char *chrLine;
    char *chrToken;
    int intThisLineID = -1;
    int intLastLineID = -1;
    int intLargestID =0;


    wxsFileName = CONFIG_HOMEDIR;
    wxsFileName += CONFIG_COLLECTIONDATA;

///////////////// TEMPORY FIX!!!!!. WORKING ON A FUNCTION TO GENERATE THE LARGEST PREVIOUS NUMERIC ID. AT THIS STAGE IT MAKES NO DIFERENCE WHAT THE ID IS.
    intLargestID++;



    wxsNewEntryLine<<intLargestID;
    wxsNewEntryLine +=_("`");
    wxsNewEntryLine += cbxSpeciesBotanical->GetValue();
    wxsNewEntryLine +=_("`");
        wxDateTime wxdDate = dteDate->GetValue();
        wxString wxsDate = wxdDate.FormatDate();
    wxsNewEntryLine += wxsDate;
    wxsNewEntryLine +=_("`");
    wxsNewEntryLine +=tbxLocation->GetLineText(0);
    wxsNewEntryLine +=_("`");
    wxsNewEntryLine +=tbxLongitude->GetLineText(0);
    wxsNewEntryLine +=_("`");
    wxsNewEntryLine += tbxLatitude->GetLineText(0);
    wxsNewEntryLine +=_("`");

        int ccc = tbxComments->GetNumberOfLines();
        for (int aaa=0;aaa<ccc;aaa++)
        {

        wxsNewEntryLine+= tbxComments->GetLineText(aaa);
        wxsNewEntryLine +=_("&");
        }
        //wxsNewEntryLine +=_("`");


    wxsFileName= CONFIG_HOMEDIR;
    wxsFileName+= _(".data/CollectionData3.txt");

    wxTextFile wxtfleOutFile(_("/home/kevin/Documents/SeedBank/.data/CollectionData3.txt"));
    int intNumLines = -1;
    intNumLines = wxtfleOutFile.GetLineCount();
    const int cintNumLines = intNumLines;
    wxString awxsFileContents[cintNumLines];
    for(int ddd = 1;ddd<cintNumLines;ddd++)
    {
        awxsFileContents[ddd] = wxtfleOutFile.GetLine(ddd);
    }

    wxtfleOutFile.Write();
    wxtfleOutFile.Close();


    //wxtfleOutFile.Open(_("/home/kevin/Documents/SeedBank/.data/CollectionData3.txt"));

    for (int eee=1;eee<intNumLines;eee++)
    {
        wxtfleOutFile.AddLine(awxsFileContents[eee]);
    }

    wxtfleOutFile.AddLine(wxEmptyString);
    wxtfleOutFile.AddLine(wxsNewEntryLine);
    wxtfleOutFile.Write();



    wxMessageBox(_("Closing."));

thanks all for your time and paitence. =]

Hi all. someone did solve this thread, and for some reason the post hasnt shown up. so i will repost the code here:

wxTextFile wxtfleOutFile(); // DO NOT INCLUDE FILE PATH IN CONSTUCTOR, UNLESS YOU WANT THE FILE OVERWRITTEN

wxtfleOutFile.Open(_("your/file.txt"));
//USE PATH HERE INSTEAD

wxtfleOutFile.AddLine(wxsNewEntryLine);


wxtfleOutFile.Write();