Thread: Basic file io
View Single Post
Old 05-06-2014, 11:52 AM   #4
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,043
Default

Looks like the WDL file classes are not designed to help dealing with text data...Writing is fairly trivial, but reading requires more careful handling...

Writing is like :

Code:
        WDL_FileWrite outfile("C:\\ssd_tests\\wdl_test_file.txt");
        if (outfile.IsOpen()==true)
        {
            std::string txt1("Foo\n");
            outfile.Write(txt1.data(),txt1.size());
            std::string txt2("Bar\n");
            outfile.Write(txt2.data(),txt2.size());
        } else std::cout << "could not open file for writing\n";
Reading would start something like :

Code:
        WDL_FileRead infile("C:\\ssd_tests\\wdl_test_file.txt");
        if (infile.IsOpen()==true)
        {
            std::vector<char> buffer(infile.GetSize()+1); // +1 to have space for the terminating zero
            infile.Read(buffer.data(),infile.GetSize());
            buffer[infile.GetSize()]='\0'; // put in the string terminating zero
But I think I will leave the rest (parsing newlines etc) as an exercise for the reader...(No pun intended.)

If you need to handle text files, perhaps the C++ standard library facilities might be worth looking into again, after all...What kinds of problems did you encounter while trying to use them? (My own pains with ifstream and ofstream have mostly been when trying to use them for binary files, such as wav-files etc...For text files they may work tolerably enough.)
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass, Paul(X)Stretch and λ) :
http://xenakios.wordpress.com/

Last edited by Xenakios; 05-06-2014 at 12:00 PM.
Xenakios is online now   Reply With Quote