C 程序 - 警告 : assignment makes pointer from integer without a cast

标签 c

我正在尝试读取一个文件并将其内容存储在一个变量中,这是我的代码:

#define _BSD_SOURCE
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

// CEK ROUTER MODEL
char* router_model;
char* model() {
    char filename[] = "/tmp/cpuinfo";
    char* key = "system type";
    char* value;
    FILE *file = fopen(filename, "r");

    if (file != NULL) {
        char line[1000];

        while (fgets(line, sizeof line, file) != NULL) /* read a line from a file */ {
            //fprintf(stdout, "%s", line); //print the file contents on stdout.
            if (strncmp(line, key, strlen(key)) == 0) {
                char* value = strchr(line, ':');
                value += 2;
                router_model = strdup(value);
                break;   // once the key has been found we can stop reading
            }
        }
        fclose(file);
    }
    else {
        perror(filename); //print the error message on stderr.
    }
    return router_model;
}

// TULIS SERIAL NUMBER KE FILE
char tulis(char p[100]) {
    // Write a serial number to a file
    char sn[30];
    char encrypt_sn[300];
    printf("Serial Number:\n");
    scanf("%s", sn);
    FILE *f = fopen("/tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c", "w");
    if (f == NULL) {
        printf("Error opening file!\n");
        exit(1);
    }
    fprintf(f,"Serial Number: %s", sn);
    fclose(f);
    sprintf(encrypt_sn, "ccrypt -e /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c -K %s", p);
    system(encrypt_sn);
    system("mv /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c.cpt /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c");
    printf("Serial number is saved in /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c\n");
    return 0;
}

// BACA SERIAL NUMBER & SIMPAN DALAM SEBUAH VARIABLE
char baca(char p[100]) {
    // Store the serial number from a file in a variable
    char line[50];
    char decrypt_sn[300];
    char key[30] = "Serial Number";
    char *serial_number;
    if( access( "/tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c", F_OK ) != -1 ) {
        system("cp /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c /tmp/");
        system("mv /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c.cpt");
        sprintf(decrypt_sn, "ccrypt -d /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c.cpt -K %s", p);
        system(decrypt_sn);
        FILE *file = fopen("/tmp/fsn-55cfc8770b69cc07268fae7f25ee444c", "r");
        if (file == NULL) {
            printf("Error opening file!\n");
            exit(1);
        }
        while (fgets(line, sizeof line, file) != NULL) /* read a line from a file */ {
            //fprintf(stdout, "%s", line); //print the file contents on stdout.
            if (strncmp(line, key, strlen(key)) == 0) {
                char* value = strchr(line, ':');
                value += 2;
                serial_number = strdup(value);
                break;   // once the key has been found we can stop reading
            }
        }
        fclose(file);
        //printf("Your hardware serial number is: (%s)\n", serial_number);
        remove("/tmp/fsn-55cfc8770b69cc07268fae7f25ee444c");
    }
    else {
        printf("fsn not found\n");
        return -1;
    }
    return 0;
}

int main(int argc, char* argv[]) {
    char *r;
    char *del;
    char *decrypt;
    int ret;
    char input[30];
    char *p;
    char *original_sn;
    p = "MmI4MTUxM2FjMjRlMDkzYmRkZGQyMjcwMjQ4OWY3MDAwNGZiYTM0MWNkZGIxNTdlYzAxN2";
    //tulis(p);
    original_sn = baca(p);
    printf("SN: %s\n", original_sn);
    return 0;
}

文件是/tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c,文件内容是序列号:1866203214226041original_sn应该输出1866203214226041。但是,当我运行该代码时,我得到:

test.c: In function ‘main’:
test.c:105:14: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  original_sn = baca(p);
              ^
SN: (null)

我该如何解决?

最佳答案

发生这种情况是因为您的 baca 函数返回一个 char,而您将其返回值分配给一个 char *。也许您想使用 char 变量。

关于C 程序 - 警告 : assignment makes pointer from integer without a cast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42223491/

相关文章:

c - 具有主要功能的多个源文件

c++ - 在运行时格式化字符串

c - 用FT245同步方式写入芯片

使用c递归创建线程

c - 无法理解,执行操作 &arr+1 时如何在数组中计算下一个地址

无法将字符添加到分配给 ""的字符指针

c - 它是链表实现吗?

C结构被双重包含

c - 最重要的信号要处理?

c - 显示后如何更改我输入的名称