I am looking to create a text file on my iPhone programmatically and also write some data onto the file. How can I read the text file manually?
Use NSString's initWithContentsOfFile:encoding:error: to read the file, then use stringByAppendingFormat: to add some data, and then use writeToFile:atomically:encoding:error: to write it on disk.
Related
Is it possible to save an image from a base64, with a file extension in the name?
As per
https://github.com/facebook/react-native/blob/master/Libraries/Image/RCTImageStoreManager.m#L54
it looks like the imageTag, i.e. file name, is constructed form the path and a simple incremental id. Extension is not considered.
Is there another way to append the extension, preferably with standard APIs?
Are you using RN's ImageStore? Don't know if this is the best way to do it, but the ImageStore will return the uri and you could use react-native-fs to read the base64 from that file and write it to whatever path/extension you would like it to have.
Don't forget to also removeImageForTag(uri) afterwards so you don't have both files.
react-native-fs: https://github.com/johanneslumpe/react-native-fs
I'm trying to write text to file according to this documentation:
https://github.com/torch/torch7/blob/master/doc/serialization.md
With the following code:
require 'torch'
torch.save('temp.txt','text')
A new file named temp.txt is created but when I open it in text editor I see a few null symbols before the text.
Is there an other way to do it?
torch.save does not write only text to the file, but it serializes the given object, so these bytes are probably type of object and length of the string or similar. This is the intended way.
If you want to write a file with text only, use normal Lua API:
fd = io.open('temp.txt', 'w')
fd:write('text')
fd:close()
I want to write simple text pdf file and I use Synopse pdf Delphi library.
Is it possible to write one text line to file and it automatically insert new line to file without using coordinates?
The easiest is to use the mORMotReport.pas unit.
It is very easy to add some text, with automatic insert of lines, and page layout.
See this sample folder as reference.
Is there a way I can use coding
Memo1.lines.savetofile ('textfile1.txt') without it then deleting whatever already is in the texfile to save the memos lines?
Create a TFileStream that opens the existing file, seek to the end of the stream, and then pass the stream to Memo1.Lines.SaveToStream().
Presumably you want to append the text to the file. Do that like so:
TFile.AppendAllText(FileName, Memo1.Text);
This uses the TFile class from the IOUtils unit. Pass an encoding parameter if you want to exercise control over the encoding used.
I am trying to modify an XLSX file programmatically using Objective-C.
So far, I am only modifying the data on one of the sheets. The steps I am taking are as follows:
Copy the XLSX file to Documents folder
Unzip the XLSX container with keeping the directory structure
Parse the corresponding sheet XML file (sheet2.xml in my case)
Add some rows
Rewrite the XML structure and save it
Put the updated XML file back into the XLSX container
However, the new XLSX file becomes corrupt. I am using GDataXML for XML parsing/writing and Objective-Zip for zipping/unzipping.
I know that the XML file I have created is proper, because when I manually unzip and the re-zip the corrupt XLSX file, it opens without any errors. I have done this on both OS X (using Unarchiver) and Windows (using 7-Zip).
The problem is either with the Objective-Zip library, or the way I use it. Below is how I implement the zipping method:
ZipFile *zipFile = [[ZipFile alloc] initWithFileName:XLSXDocumentsFilePath mode:ZipFileModeAppend];
ZipWriteStream *stream= [zipFile writeFileInZipWithName:XLSX_WORKSHEET_XML_SUBPATH compressionLevel:ZipCompressionLevelNone];
[stream writeData:data];
[stream finishedWriting];
[zipFile close];
I also tried the other compressionLevel arguments available with no luck:
ZipCompressionLevelDefault
ZipCompressionLevelBest
ZipCompressionLevelFastest
My questions are:
Which zipping library should I use to create a valid XLSX file programmatically?
If Objective-Zip is suitable, what is wrong with my code?
From an answer to another question, I found out that: "The OOXML format imposes that the only compression method permitted in the package is DEFLATE".
Is it possible to force Objective-Zip to use DEFLATE? Or is there an open-source iOS zipping library that uses DEFLATE?
I found the answer upon doing some research and also having a one to one correspondence with Objective-Zip's developer, flyingdolphinstudio.
First of all, Objective-Zip uses DEFLATE as the default compression method. I also confirmed this with the developer, who told me that using ZipCompressionLevelDefault, ZipCompressionLevelFastest or ZipCompressionLevelBest for the argument compressionLevel: will guarantee a DEFLATE compression.
So, the problem is coming from the mode: argument, which is ZipFileModeAppend in my case. It seems that MiniZip does not have a method to delete the files inside a zip file and that's why I am not overwriting the existing file, but adding a new one. To make it more clear, take a look at how my xl/worksheets folder look like after zipping it using Objective-Zip:
So, the only way to create a valid XLSX container is to create the zip file from scratch, by adding all the files and also keeping the directory/file structure intact.
I hope this experience would help somebody out.