English 中文(简体)
具体目标C中的空白
原标题:Save blank file in Objective-C

我正在制定一项法典,除其他外,该法典必须拯救零填充的档案。 它可以预期从10个联邦议院到甚至1个性别空白处立案。

它必须类似于这一联盟指挥部:

dd if=/dev/zero of=my_image.dsk count=20480

我可以做这项工作,规模小:

int totalSize = 1024*1024;
char buf[totalSize];
strcpy(buf,"");

NSData *theData = [NSData dataWithBytes:buf length:sizeof(buf)];

//NSLog(@"%@", theData);
NSLog(@"%lu", sizeof(buf));

[theData writeToFile:@"/Users/foo/Desktop/my_image.dsk" atomically:YES];

But if I try a bigger value (1024*1024*10, for instance), it crashes. So I tried:

NSFileManager *ddd = [[NSFileManager alloc ] init];
[ddd createFileAtPath:@"/Users/foo/Desktop/my_image.dsk" contents:[NSData data] attributes:nil];

它制造了一个空洞的档案,但由于档案尺寸为零,并不好。 这不应是零。

我花了几个小时时间试图找到答案,却没有成功。 我想在奥比-C这样做,但C也是我获得营养的一种选择。

请问,有人说几句灯!

提前感谢!

--——————

感谢大家,但又是一件事:在不把一切照相传的情况下,能否写字?

最佳回答

您可使用<条码>d。 这将让你书写,例如,10英镑,而不担心记忆或空间。

NSTask *task = [NSTask new];
long size = ...; // Note! dd multiplies this by 512 by default
NSString *path = ...;
[task setLaunchPath:@"/bin/dd"];
[task setArguments:[NSArray arrayWithObjects:@"dd", @"if=/dev/zero",
    [NSString stringWithFormat:@"of=%s", [path fileSystemRepresentation]],
    [NSString stringWithFormat:@"count=%ld", size],
    nil]];
[task launch];
[task waitUntilExit];
if ([task terminationStatus] != 0) {
    // an error occurred...
}
[task release];

一种优势是,如果情况有误,尖端用户可杀死<代码>d<>>/code>。

www.un.org/Depts/DGACM/index_spanish.htm 关于沙箱: 我的怀疑是,即便在沙箱里,这也会奏效,但我很想知道。 根据文件(link),一个分过程“简单地继承了制造过程的沙框”。 这正是你期待它就团结问题开展工作的。

您可以肯定,<代码>d将贴上前后的标签。 果 Apple声称,SOS X符合SUSv3。

问题回答

This is relatively simple in the POSIX API:

int f = open("filename", O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
if (f < 0) {
    perror("open filename");
    exit(1);
}

char empty = 0;

pwrite(f, &empty, sizeof(char), <offset into the file>);

抵消额是用“<代码>off_t”的分类法确定的,这种分类可能不足以满足您的具体需要。 检查你的系统预报文件,以发现64个轨道文档接口,如果其数量足够大。

这种做法的最好部分是,它只需要几个旁听方案记忆——你不再分配10千兆字不来写你的档案。

另一个更为简单的办法是使用<代码>truncate(2)系统电话。 并非所有平台都支持将文档延伸到<代码>truncate(2),但对于那些这样做的平台而言,则提供one打<>。

该法典:

int totalSize = 1024*1024;
// don t allocate on the stack
char *buf = calloc(totalSize, sizeof(char));

NSData *theData = [NSData dataWithBytesNoCopy:buf length:totalSize];

//NSLog(@"%@", theData);
NSLog(@"%lu", totalSize);

[theData writeToFile:@"/Users/foo/Desktop/my_image.dsk" atomically:YES];

你们也可以尝试这样做,更不用说记忆,它用1个GB文档开展工作。

void writeBlankBytes(FILE *file, size_t numKB)
{
    static char *oneKBZero = NULL;

    if (oneKBZero == NULL)
    {
        oneKBZero = calloc(1024, sizeof(char));
    }

    for (int i = 0; i < numKB; i++) {
        fwrite(oneKBZero, 1, 1024, file);
    }
}

感谢大家。 我找到了解决办法。 这基本上属于我即将列入可可图的法典。

工作罚款,我认为我只需要一些具有规模变化的防范措施,足够。

long size = 2000*500;

NSString *path = [[NSString alloc] initWithFormat: @"/Users/foo/Desktop/testeFile.dsk"];

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/dd"];
[task setArguments:[NSArray arrayWithObjects:
                    @"if=/dev/zero",
                    [NSString stringWithFormat:@"of=%s", [path fileSystemRepresentation]],
                    [NSString stringWithFormat:@"count=%ld", size],
                    nil]];
NSLog(@"%@",[task arguments]);
[task launch];

//[task waitUntilExit]; //The app would stuck here.. better use the loop

int cont = 0;
while ([task isRunning]) {
    cont++;
    if(cont%100==0) NSLog(@". %d", cont);
}

if ([task terminationStatus] != 0) {
    NSLog(@"an error occurred...");
}
[task release];
[path release];

NSLog(@"Done!");




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...