小编Jor*_*dan的帖子

使用awk在一行上打印特定索引的字符

好吧,所以我知道使用$打印一行的特定参数非常简单:

$ cat file
hello world

$ awk '{print $1}' file
hello
Run Code Online (Sandbox Code Playgroud)

但是如果我想打印2到8的字符怎么办?还是3到7?用awk可以吗?

awk

38
推荐指数
1
解决办法
7万
查看次数

C - 将字符串拆分为字符串数组

我不完全确定如何在C中执行此操作:

char* curToken = strtok(string, ";");
//curToken = "ls -l" we will say
//I need a array of strings containing "ls", "-l", and NULL for execvp()
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

c c-strings

34
推荐指数
2
解决办法
9万
查看次数

确定shell程序中的相对路径或绝对路径

如标题中所述,如果路径是相对路径或绝对路径,我需要确定何时运行程序:

./program #relative
dir/dir2/program #relative
~User/dir/dir2/program #absolute
/home/User/dir/dir2/program #absolute
Run Code Online (Sandbox Code Playgroud)

这是我的测试用例.我怎样才能在shell程序中这样做呢?

或者更一般地说,如何检查路径,$0在这种情况下,是相对的还是绝对的?

linux shell path

21
推荐指数
3
解决办法
8085
查看次数

在C中实现shell并需要帮助处理输入/输出重定向

第2轮

在阅读了一些答案之后,我的修改后的代码是:

int pid = fork();

if (pid == -1) {
    perror("fork");
} else if (pid == 0) {   

    if (in) { //if '<' char was found in string inputted by user
        int fd0 = open(input, O_RDONLY, 0);
        dup2(fd0, STDIN_FILENO);
        close(fd0);
        in = 0;
    }

    if (out) { //if '>' was found in string inputted by user
        int fd1 = creat(output, 0644);
        dup2(fd1, STDOUT_FILENO);
        close(fd1);
        out = 0;
    }   

    execvp(res[0], res);
    perror("execvp");
    _exit(1);
} else {
    waitpid(pid, 0, 0);
    free(res); …
Run Code Online (Sandbox Code Playgroud)

c linux shell

13
推荐指数
2
解决办法
5万
查看次数

iPad纹理加载差异(32位与64位)

我正在绘制一个绘图应用程序,我注意到在32位iPad和64位iPad上加载的纹理存在显着差异.

这是在32位iPad上绘制的纹理:

在此输入图像描述

这是在64位iPad上绘制的纹理:

在此输入图像描述

64位是我想要的,但似乎它可能正在丢失一些数据?

我使用以下代码创建默认画笔纹理:

UIGraphicsBeginImageContext(CGSizeMake(64, 64));
CGContextRef defBrushTextureContext = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(defBrushTextureContext);

size_t num_locations = 3;
CGFloat locations[3] = { 0.0, 0.8, 1.0 };
CGFloat components[12] = { 1.0,1.0,1.0, 1.0,
    1.0,1.0,1.0, 1.0,
    1.0,1.0,1.0, 0.0 };
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);

CGPoint myCentrePoint = CGPointMake(32, 32);
float myRadius = 20;

CGGradientDrawingOptions options = kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation;
CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, myCentrePoint,
                             0, myCentrePoint, myRadius,
                             options);

CFRelease(myGradient);
CFRelease(myColorspace);
UIGraphicsPopContext();

[self setBrushTexture:UIGraphicsGetImageFromCurrentImageContext()];

UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

然后实际设置画笔纹理如下:

-(void) setBrushTexture:(UIImage*)brushImage{
// …
Run Code Online (Sandbox Code Playgroud)

opengl-es core-graphics objective-c ios

12
推荐指数
1
解决办法
1337
查看次数

在C中从二进制转换为char

我对如何从二进制值转换为c中的char感到困惑.

例如,假设我有01010110,并希望从中打印相应的字母'V'.我该怎么做呢?

谢谢你的帮助!

c binary type-conversion char character-encoding

9
推荐指数
2
解决办法
2万
查看次数

与按位运算混淆| 和<<

unsigned long long n = 0;
for (int i = 0; i <= 64; i+=2)
    n |= 1ULL << i;       //WHAT DOES THIS DO? AH! 
Run Code Online (Sandbox Code Playgroud)

我试图围绕这段代码实际上做的第三行.有人请帮忙清除这个!

c bit-manipulation

3
推荐指数
1
解决办法
1998
查看次数

在Objective-C中使用块

const char *sentence = "He was not in the cab at the time.";

printf("\"%s\" has %d spaces\n", sentence, (int) ^ {
    int i = 0;
     int countSpaces = 0;

    while (sentence[i] != '\0') {
        if (sentence[i] == 0x20) {
            countSpaces++;
        }
        i++;
    }    
    return countSpaces;
});
Run Code Online (Sandbox Code Playgroud)

这段代码只计算一个字符串中的空格,但由于某种原因,它表示1606416608个空格而不是8个.我不确定出了什么问题,所以感谢您的帮助!

objective-c objective-c-blocks

3
推荐指数
1
解决办法
294
查看次数

在C中确定文件类型

我正在使用Linux机器上的C程序,该程序显示作为程序参数的文件的文件类型.程序需要确定文件是否是以下任何一个:目录,设备,(常规)文件,链接,套接字或fifo.我不确定如何确定文件类型.

到目前为止,这是我的代码(不多):

int
main(int argc, char **argv)
{
    if( argc == 1 )     /* default: current directory */
        puts("Directory");
    else
        while( --argc > 0 )
            determine_ftype(*++argv);

    return  0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

c file-type file

3
推荐指数
1
解决办法
2万
查看次数

为结构数组中的所有元素的字段赋值

我有:

#include <stdio.h>

struct DVD {
    char *movie_title;
    int minutes;
    float price;
};

void display_struct(struct DVD *ptr);

int
main()
{
    struct DVD movies[10];
    movies[0].movie_title = "I Am Legend"; //Don't want to do this
}

void
display_struct(struct DVD *ptr)
{
    printf("%s\n%i\n%f\n", ptr->movie_title, ptr->minutes, ptr->price);
}
Run Code Online (Sandbox Code Playgroud)

我想在一个语句中为我的结构数组分配10个电影标题.这可能吗?谢谢!

c struct

2
推荐指数
1
解决办法
68
查看次数

C - execvp()第二个参数

execvp(argv[1], &argv[1])
Run Code Online (Sandbox Code Playgroud)

究竟是用execvp()的第二个参数做的?

c unix execvp

1
推荐指数
1
解决办法
1008
查看次数

Facebook SDK和iOS应用程序的问题(authButtonAction)

我不确定出了什么问题.我实现了developers.Facebook.com指定的所有内容,以在我的iOS应用程序中验证Facebook登录.以下是我认为与此问题相关的内容:

我正在制作的应用程序是带有导航控制器选项卡式应用程序.

我收到以下代码的警告(由Facebook开发者页面提供):

- (IBAction)authButtonAction:(id)sender {
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    // The user has initiated a login, so call the openSession method
    // and show the login UX if necessary.
    [appDelegate openSessionWithAllowLoginUI:YES];
}
Run Code Online (Sandbox Code Playgroud)

警告是: 使用不兼容类型'id <UIApplicationDelegate>'的表达式初始化'AppDelegate*'

当我点击authButton时,我的iPhone上的Facebook应用程序将打开,并向用户显示有关授予我的应用程序访问发布状态更新的相应消息.但是...控制流程从未返回给我的应用程序,Facebook应用程序保持打开状态.我相信这与上述警告有关.

iphone cocoa-touch facebook objective-c ios

1
推荐指数
1
解决办法
718
查看次数