小编Blu*_*kis的帖子

我是否正确嵌套数组?

我目前正在练习编写拼字游戏分数计算器,我正在尝试一步一步地构建它,但我被出了什么问题难住了:

#include <stdio.h>
#include <cs50.h>
#include <string.h>

// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};

int compute_score(string word);

int main(void)
{
    // Get word from player
    string word1 = get_string("Player 1: ");
    
    // Calculate score
    int score1 = compute_score(word1);
    
    // Print score
    printf("%i\n", score1);
}

int compute_score(string word)
{
    // …
Run Code Online (Sandbox Code Playgroud)

c arrays ascii function cs50

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

fread() 可以用来确定文件类型吗?

我试图用来fread()确定文件是否是 jpeg,我的想法是使用fread()读取文件的标题以确保它是 jpeg,然后,如果是 - 复制文件。我知道标准的 jpeg 标头是0xff 0xd8 0xff 0xe(0 to f),所以我制作了一个程序来读取文件的前四个字节。

#include <stdio.h>
#include <stdint.h>

typedef uint8_t BYTE;

int main (int argc, char *argv[])
{
    FILE *in = fopen(argv[1], "rb");
    
    FILE *out = fopen("output.jpg", "wb");
    
    BYTE buffer [4096];
    
    while (fread(buffer, 1, sizeof(buffer), in))
    {
        if ((buffer[0] == 0xff) & (buffer[1] == 0xd8) & (buffer[2] == 0xff) & ((buffer[3] & 0xf0) == 0xe0))
        {
            fwrite(buffer, 1, sizeof(buffer), out);
        }
    }
    
    fclose(in);
    fclose(out);
}
Run Code Online (Sandbox Code Playgroud)

但是,在运行程序后,当我尝试打开 …

c fwrite fread

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

标签 统计

c ×2

arrays ×1

ascii ×1

cs50 ×1

fread ×1

function ×1

fwrite ×1