我需要在我正在制作的游戏中拥有一系列结构 - 但我不想将数组限制为固定大小.我被告知有一种方法可以在需要时使用realloc使数组更大,但是找不到任何有用的例子.
有人可以告诉我该怎么做吗?
所以我有一个从父进程到子进程的文件流 - 大多数时候它工作正常.但是,当快速读取多次时,使用fgets()将返回NULL并将错误设置为"资源暂时不可用".问题是间歇性的 - 并且运行执行读取的脚本有时会使fgets返回NULL,有时会返回NULL.
谁能帮我阻止这个错误发生?谢谢!
编辑:这里有一些代码..我不确定其他代码会有什么用处?有很多
// this is the bit that gets a line from the child
if( fgets( line, MAX_LINE_LENGTH, fpin ) == NULL ) {
if( ferror(fpin) ) {
perror("error on stream fpin");
}
free( line );
return FAIL;
}
Run Code Online (Sandbox Code Playgroud)
根据要求,打开管道并处理子进程的代码.
// set up pipes
int readPipe[2]; // child -> parent communication
int writePipe[2]; // parent -> child communication
int errorPipe[2]; // child -> parent, to check for errors in exec
/* create pipe */
pipe( …Run Code Online (Sandbox Code Playgroud) 我正在使用NSOutlineView对象来表示文件结构,并发现它不能正确缩进任何可扩展的子节点,尽管它会缩进不可扩展的子节点.
这是一张显示我的意思的照片:

在此示例中,"AnotherFolder"是"Folder2"的子级,但它不会与其他缩进文件一致缩进.奇怪的是,"AnotherFolder"的子"AnotherFile.java"确实缩进(2级).
我试过设置"indentationFollowsCells"等属性无济于事.这似乎应该很简单,但我无法解决它.
谢谢!
编辑:根据要求提供一些额外信息:
我正在使用NSOutlineViewDataSource协议来实现,这里是与之相关的代码:
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
return item;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
NSMutableDictionary* dict;
if(item == nil) {
dict = fileTree;
} else {
dict = [((MyFile*) item) children];
}
NSArray* keys = [dict allKeys];
NSArray* sorted = [keys sortedArrayUsingSelector:@selector(compare:)];
NSString* key = [sorted objectAtIndex:index];
return [dict objectForKey:key];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return [[item children] count] > 0;
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if(item == nil) …Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法来检查管道在C中写入之前是否已关闭?我有一个子进程和父进程,父进程有一个写入子进程的管道.但是,如果孩子关闭管道并且父级尝试读取 - 我收到管道损坏的错误.
那么如何检查以确保我可以写入管道,所以如果我不能,我可以将其作为错误处理?谢谢!
我只是想知道如果没有提供特定的端口号,如何使用C自动分配一个空闲端口(并查看使用的内容).
例如,我正在使用这个:
struct sockaddr_in address;
address->sin_family = AF_INET;
address->sin_addr.s_addr = INADDR_ANY;
address->sin_port = htons( port );
Run Code Online (Sandbox Code Playgroud)
但是如何替换sin_port赋值并让C自动为我分配?
谢谢!
基本上我有一些类型船的结构,它们将放在可以具有可变宽度和高度的板上.有关船只的信息是从文件中读取的,我只需要知道确保没有任何船只重叠的最佳方法.
这是Ship的结构:
int x // x position of first part of ship
int y // y position of first part of ship
char dir // direction of the ship, either 'N','S','E' or 'W'
int length // length of the ship
Run Code Online (Sandbox Code Playgroud)
此外,处理方向的好方法是什么.比使用switch语句更清洁,并为每个方向使用不同的条件.
任何帮助将不胜感激!
我正在使用java中的Scanner类来浏览一个文本文件并提取每个句子.我在我的扫描仪上使用setDelimiter方法到正则表达式:
Pattern.compile("[\\w]*[\\.|?|!][\\s]")
Run Code Online (Sandbox Code Playgroud)
这目前似乎有效,但它在句子的末尾留下了空格.有没有一种简单的方法来匹配最后的空白,但不包括在结果中?
我意识到这可能是一个简单的问题,但我从来没有使用正则表达式,所以很容易:)
我正在尝试解决子进程运行 execvp() 并且需要让父进程知道它是否返回的问题。因此,在 execvp() 返回后(因为出现错误),我如何告诉父级此特定事件已发生,以便它可以处理它。
有一种方法可以通过我正在使用的管道写入一串文本,然后从父级读取它......但它似乎有点草率。有没有更好的办法?
谢谢!
编辑:这是我正在尝试的一些代码,我似乎无法让读取返回。
int errorPipe[2];
signal( SIGPIPE, SIG_IGN );
int oldflags = fcntl (errorPipe[0], F_GETFD, 0);
oldflags |= FD_CLOEXEC;
fcntl (errorPipe[0], F_SETFD, oldflags);
oldflags = fcntl (errorPipe[1], F_GETFD, 0);
oldflags |= FD_CLOEXEC;
fcntl (errorPipe[1], F_SETFD, oldflags);
pipe( errorPipe );
// in the child..
char *error_message = "exec failed";
write( errorPipe[1], error_message, strlen(error_message)+1 );
exit(-1);
// in the parent
printf("read gives: %d\n", read(errorPipe[0], error_read, MAX_LINE_LENGTH) );
Run Code Online (Sandbox Code Playgroud)