I'm trying to open a file and append data to it using ExtAudioFileWrite. Now, creating/initial writing(on creation)/converting works just fine, but unfortunately I can't seem to be able to open the file for writing afterwards. There is only one function that seems to open the file: ExAudioFileOpenURL but only for reading. So, how can I write at the end of a file if I'm not able to open the file for writing?
I might be wrong, or not fully understanding your question. But, once you start writing to the file using ExtAudioFileWrite you can keep writing to it by calling the ExtAudioFileWrite again. I am about to venture into coding with no compiler, hopefully you will get the idea
Example:
....variables declaration and your logic.....
//1.Create File to write output
ExtAudioFileRef outputFile;
//+++++++ LOOP to append as many files as you want +++++++//
//2.Create file reference to files you want to merge
ExtAudioFileRef audioFileObject = 0;
//3.open the first file
ExtAudioFileOpenURL (firstFileURL, &audioFileObject);
//4.get file properties
AudioStreamBasicDescription AudioFileFormat = 0;
UInt64 numberOfPacketsToReadInCurrentFile = 0;
ExtAudioFileGetProperty(audioFileObject,
kExtAudioFileProperty_FileLengthFrames,
sizeof(numberOfPacketsToReadInCurrentFile),
&numberOfPacketsToReadInCurrentFile
);
ExtAudioFileGetProperty(audioFileObject,
kExtAudioFileProperty_FileDataFormat,
sizeof(AudioFileFormat),
&AudioFileFormat
);
//5.Set destination ASBD
ExtAudioFileSetProperty(audioFileObject,
kExtAudioFileProperty_ClientDataFormat,
sizeof (importFormat),
&importFormat
);
//6.Set/Create/Allocate Some Buffers
//use AudioFileFormat to setup your buffers accordingly
AudioBufferList theBufferList = 0 // bufferList setup (I dont remember that)
//7.Read the file into the buffer
ExtAudioFileRead(audioFileObject,&numberOfPacketsToReadInCurrentFile,theBufferList);
//8.Write the buffer to a File
ExtAudioFileWrite(outputFile, numberOfPacketsToReadInCurrentFile, theBufferList);
free(theBufferList);
//if you want to append more files go to point 2 with the next file URL
//+++++++CLOSE LOOP+++++++++//
//once you are done.. just dispose the file and clean up everything left
ExtAudioFileDispose(outputFile);
This will not compile, I guess. But hopefully will help you get the idea.
Related
I'm using Photoshop script. I get files from folders. My problem is that when I get the files and place them in an array the array contains hidden files that are in the folder for example ".DS_Store". I can get around this by using:
if (folders[i] != "~/Downloads/start/.DS_Store"){}
But I would like to use something better as I sometimes look in lots of folders and don't know the "~/Downloads/start/" part.
I tried to use indexOf but Photoshop script does not allow indexOf. Does anybody know of a way to check if ".DS_Store" is in the string "~/Downloads/start/.DS_Store" that works in Photoshop script?
I see this answer but I don't know how to use it to test: Photoshop script to ignore .ds_store
For anyone else looking for a solution to this problem, rather than explicitly trying to skip hidden files like .DS_Store, you can use the Folder Object's getFiles() method and pass an expression to build an array of file types you actually want to open. A simple way to use this method is as follows:
// this expression will match strings that end with .jpg, .tif, or .psd and ignore the case
var fileTypes = new RegExp(/\.(jpg|tif|psd)$/i);
// declare our path
var myFolder = new Folder("~/Downloads/start/");
// create array of files utilizing the expression to filter file types
var myFiles = myFolder.getFiles(fileTypes);
// loop through all the files in our array and do something
for (i = 0; i < myFiles.length; i++) {
var fileToOpen = myFiles[i];
open(fileToOpen);
// do stuff...
}
For anybody looking I used the Polyfill found here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
indexOf() was added to the ECMA-262 standard in the 5th edition; as
such it may not be present in all browsers. You can work around this
by utilizing the following code at the beginning of your scripts. This
will allow you to use indexOf() when there is still no native support.
This algorithm matches the one specified in ECMA-262, 5th edition,
assuming TypeError and Math.abs() have their original values.
Hey there skbio team.
So I need to allow either DNA or RNA MSAs. When I do the following, if I leave out the alignment_fh.close() skbio reads the 'non header' line in the except block making me think I need to close the file first so it will start at the beginning, but if I add alignment_fh.close() I cannot get it to read the file. I've tried opening it via a variety of methods, but I believe TabularMSA.read() should allow files OR file handles. Thoughts? Thank you!
try:
aln = skbio.TabularMSA.read(alignment_fh, constructor=skbio.RNA)
except:
alignment_fh.close()
aln = skbio.TabularMSA.read(alignment_fh, constructor=skbio.DNA)
I've tried opening it via a variety of methods, but I believe TabularMSA.read() should allow files OR file handles.
You're correct: scikit-bio generally supports reading and writing files using open file handles or file paths.
The issue you're running into is that your first TabularMSA.read() call reads the entire contents of the open file handle, so that when the second TabularMSA.read() call is hit within the except block, the file pointer is already at the end of the open file handle -- this is why you're getting an error message hinting that the file is empty.
This behavior is intentional; when scikit-bio is given an open file handle, it will read from or write to the file but won't attempt to manage the handle's file pointer (that type of management is up to the caller of the code).
Now, when asking scikit-bio to read a file path (i.e. a string containing the path to a file on disk or accessible at some URI), scikit-bio will handle opening and closing the file handle for you, so that's often the easier way to go.
You can use file paths or file handles to accomplish your goal. In the following examples, suppose aln_filepath is a str pointing to your alignment file on disk (e.g. "/path/to/my/alignment.fasta").
With file paths: You can simply pass the file path to both TabularMSA.read() calls; no open() or close() calls are necessary on your part.
try:
aln = skbio.TabularMSA.read(aln_filepath, constructor=skbio.RNA)
except ValueError:
aln = skbio.TabularMSA.read(aln_filepath, constructor=skbio.DNA)
With file handles: You'll need to open a file handle and reset the file pointer within your except block before reading a second time.
with open(aln_filepath, 'r') as aln_filehandle:
try:
aln = skbio.TabularMSA.read(aln_filehandle, constructor=skbio.RNA)
except ValueError:
aln_filehandle.seek(0) # reset file pointer to beginning of file
aln = skbio.TabularMSA.read(aln_filehandle, constructor=skbio.DNA)
Note: In both examples, I've used except ValueError instead of a "catch-all" except statement. I recommend catching specific error types (e.g. ValueError) instead of any exception because the code could be failing in different ways than what you're expecting. For example, with a "catch-all" except statement, users won't be able to interrupt your program with Ctrl-C because KeyboardInterrupt will be caught and ignored.
I have a program which is counting the biggest number from 3 numbers. I need to modify the program as the result to be write in text file maxim.out (PASCAL)
You can write the value (assuming it is an integer and it has the name, say, yourValue) with:
var
maximFile: Text;
...
Assign(maximFile, 'maxim.out'); // link the name to the Text variable
Rewrite(maximFile); // open it for writing
Writeln(maximFile, yourValue); // write the value as a line of its own
Close(maximFile); // close the file
You can then read back the value later on with:
Assign(maximFile, 'maxim.out');
Reset(maximFile);
Readln(maximFile, yourValue);
Close(maximFile);
I did not add any error handling (e.g. if the file can't be found, or if it is readonly, or empty, or ...). Depending on settings, that is either done with exceptions or with IOResult values. Read the documentation on how to do that. There should be examples in the docs.
You should read about "file management in pascal". Anyway, declare a variable of type textfile:
var
outputfile : TextFile;
then assignfile() to it your name of choice (maxim.out), rewrite() the file, use writeln() to write into it, and finally closefile() it.
You can find a complete example program here: http://wiki.freepascal.org/File_Handling_In_Pascal
I want to write data to zedboard's sdcard. I am able to write data to DRAM. Now I want to read DRAM's data and write it Sdcard. I have followed this (http://elm-chan.org/fsw/ff/00index_e.html) but it does not fulfill my requirement. I am not able to find any tutorial any example etc for this.
Please any tutorial link or any example. Thanks.
If you're using Vivado SDK, which I assume you are, it is really straightforward to use the SD Card.
To include the Fat File System, inside Xilinx SDK, open your Board Support Package (system.mss file) an select Modify this BSP's Settings. Under Overview, you can select xilffs.
Next, you must write the software to access the SD Card. This library offers a wide variety of functions. You can either look at here_1, in here_2 or in here_3. In this second reference is provided a wide variety of complex functions.
Aside from this, in order to use the SD Card, what you should basically do is written below. Note that this is just an overview, and you should refer to the references I gave you.
# Flush the cache
Xil_DCacheFlush();
Xil_DCacheDisable();
# Initialize the SD Card
f_mount(0, 0);
f_mount(0, <FATFS *>)
# Open a file using f_open
# Read and Write Operations
# Either use f_write or f_read
# Close your file with f_close
# Unmount the SD with f_mount(0,0)
Note that experience teaches me that you need to write to the file in blocks that are multiples of the block size of the file system, which for the FAT files syste, is typically 512 bytes. Writing less that 512 bytes and closing the file will make it zero bytes in length.
In new version of Xilffs (Fatfs) lib syntax is little changed.
New syntax is:
static FATFS FS_instance; // File System instance
const char *path = "0:/"; // string pointer to the logical drive number
static FIL file1; // File instance
FRESULT result; // FRESULT variable
static char fileName[24] = "FIL"; // name of the log
result = f_mount(&FS_instance, path, 0); //f_mount
result = f_open(&file1, (char *)fileName, FA_OPEN_ALWAYS | FA_WRITE); //f_open
May be this can help you.
This is the problem I wanted to solve:
Create a file, add data and Save it in isolatedStorage.
Open the file, add more data to this file.
How do I append data into this file? Will the new data line up ahead of the old data?
Some code will be appreciated.
Thanks
Your code looks like this (from your comment in the question - next time edit the question and insert the code so it's more readable and we don't have to repost it):
StreamWriter writeFile = new StreamWriter(
new IsolatedStorageFileStream(
txtBlkDirName.Text + "\\" + strFilenm,
FileMode.OpenOrCreate,
isf));
writeFile.WriteLine(txtPreSetMsg.Text); writeFile.Close();
Note that the mode you use is OpenOrCreate, which is going to open the existing file and put your stream pointer at the start. If you immediately start writing, it's going to overwrite anything in the file.
Your options for appending would be:
Use FileMode.Append instead so the pointer is already at the end of the stream after opening
Keep what you have but before writing, call Seek(0, SeekOrigin.End) to move the pointer to the end of the file manually.