English 中文(简体)
1. 档案中现有内容的重写
原标题:Overwriting current contents in file

我想在我称为数据.txt(在我的资源夹中)的文本档案一开始就夸大目前的数据。 总是只有一条线(没有空间或任何东西),我希望这条线在适当的时候被推翻。 这正是我迄今为止所做的工作,但这只是在案卷尾写的内容,从一开始,就开始写上我所需要的线。 如果任何人能够帮助我,将不胜感激。 感谢!

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];

//append filename to docs directory
NSString *myPath =  [documentsDirectory stringByAppendingPathComponent:@"data.txt"];
fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:myPath];

writtenString= [[NSString alloc]initWithFormat:@"%d", message];     

[fileHandle seekToEndOfFile];
[fileHandle writeData:[writtenString dataUsingEncoding:NSUTF8StringEncoding]];

[writtenString release];
最佳回答

迈克的回答将使你能够超越你档案的内容,而不是以你重新想象的偏爱线的方式。

您可以调整档案的内部内容,这样,如果你插入一栏,以下现有数据就会被推到后面,如果你去掉脚,现有的以下数据就会在移解点之后撤回。

你们有两项基本行动:你可以以一定时间(可能为0)将档案搁置起来,抛弃过去的任何旧数据。 并且,你可以以某种立场书写数据。 如果该职位上已有数据,则数据超成文(即丢失)。 如果你重写的字面范围延伸到档案的结束,档案就会延期。

然而,你既未开立又未穿透的旧数据,都仍留在原地。 在任何情况下,都不提出。

因此,如果你想要改变文本档案的第一行,如果你确信新线与旧线完全相同,那么你必须注意把所有东西从第二行移至新的地点。 一种适合小档案的简单做法是把全部东西都读到记忆中,在那里加以调整,并再次书写。 另一种做法是编造档案,读草坪,然后在新地点书写。 如果你将数据移到一开始,你就在一开始;如果你把数据推向尾,你就开始。

问题回答

这正是我迄今为止所做的工作,但这只是在案卷尾写的内容,从一开始,就开始写上我所需要的线。

是的,因为你要求:

[fileHandle seekToEndOfFile];

并做你要求的事情。 Try:

[fileHandle seekToFileOffset: 0];




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签