2017-02-14 70 views
0

我试图读取文件及其内容存储在一个变量,这里是我的代码: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和内容的文件是Serial Number: 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) 

我该如何解决?

+0

你明白吗,'baca'返回一个值?该值必须先存储,然后才能以安全的方式获取其地址。从临时工来的地址总是一个坏主意。 – Wolf

+0

这是非常明显的做什么。警告说'original_sn'是一个指针,但你试图存储一些不是指针的东西。这应该让你怀疑'baca'返回错误的类型。 – Lundin

+0

请[编辑]你的代码,以减少它到你的问题[mcve]。您当前的代码包含很多与您的问题相关的代码 - 通常,最小样本看起来与单元测试相似:只执行一项任务,输入值指定为可重现性。 –

回答

0

在你分配使用的strdup初始化内存巴卡: serial_number = strdup(value); ,那么你什么都不做。 很明显,您认为函数返回一个指向该内存的指针,以便您可以打印它的内容。但是,这不是你在做什么。因为你所有的baca函数在做的是返回一个值,如果它是sucseede(0)或不是(-1),则返回一个值。而且你只是忽略那个指针而留下一些浪费你的编程分配的未使用的内存。 他们是2个methodes解决您的代码:

方法一:返回SERIAL_NUMBER

char* baca(const char* p) { 
    // 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=NULL; 
    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, ':'); 
       if(value!=NULL){/*testing the return value for erros so you prog doesn't cruch*/ 
       value += 2; 
       serial_number = strdup(value); 
       } 
       /*in case off erreor you can choose one of two options:*/ 
       /*optinon1: print an error mesage then kill your prog*/ 
       else{ 
       printf("Error: corrupted file!\n"); 
       exit(1); 
       } 
       /*option 2: removing the else part your baca then will return NULL and the calling code should understand that an error has occured*/    
       break; 
      } 
     } 
     fclose(file); 
     remove("/tmp/fsn-55cfc8770b69cc07268fae7f25ee444c"); 
    } 
    else { 
     printf("fsn not found\n"); 
    } 
    return serial_number; 
} 
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); 
    if(original_sn!=NULL){ 
     printf("SN: %s\n", original_sn); 
     free(original_sn);/*you should free the memory allocated by strdup once you are done using it.*/ 
    } 
    else{ 
    printf("An error has occured\n"); 
    } 
    return 0; 
} 

方法二:通过引用传递

char baca(const char* p, char **serial_number) { 
    // Store the serial number from a file in a variable 
    char line[50]; 
    char decrypt_sn[300]; 
    char key[30] = "Serial Number"; 
    char ret = 0;/*the return value 0 means no error.*/ 
    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, ':'); 
       if(value!=NULL){/*testing the return value for erros so you prog doesn't cruch*/ 
       value += 2; 
       *serial_number = strdup(value); 
       } 
       /*in case off erreor you can choose one of two options:*/ 

       else{ 
       /*optinon1: print an error mesage then kill your prog*/ 
       /*option 2: making the return value non 0 and the calling code should understand that an error has occured*/ 
       #define OPTION1 
       #ifdef OPTION1 
       printf("Error: corrupted file!\n"); 
       exit(1); 
       #else 
       ret=-2; //to used this option comment out #define OPTION1 
       #endif 
       }    
       break; 
      } 
     } 
     fclose(file); 
     remove("/tmp/fsn-55cfc8770b69cc07268fae7f25ee444c"); 
    } 
    else { 
     printf("fsn not found\n"); 
     ret=-1; 
    } 
    return ret; 
} 
int main(int argc, char* argv[]) { 
    char *r; 
    char *del; 
    char *decrypt; 
    int ret; 
    char input[30]; 
    char *p; 
    char *original_sn=NULL; 
    p = "MmI4MTUxM2FjMjRlMDkzYmRkZGQyMjcwMjQ4OWY3MDAwNGZiYTM0MWNkZGIxNTdlYzAxN2"; 
    //tulis(p); 
    switch(baca(p,&original_sn)) 
    { 
     case 0: //evrything is fine 
      printf("SN: %s\n", original_sn); 
      free(original_sn); 
      break; 
     case -1:/* handle each error as you should*/ 
     case -2: 
     default: 
     printf("An error has occured\n"); 
    } 
    return 0; 
} 

希望这有助于。 :)。

+0

非常感谢你,最后我的代码是固定的:) – Squidward

3

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

+0

对不起,我还是编程新手。我已将char * original_sn;'更改为'char original_sn;',错误消失,但它仍然返回'SN:(null)' – Squidward

+0

我认为您需要打印参数,而不是'baca'的返回值函数,尝试打印'p'的值。 @Squidward – Jarvis

+0

'original_sn =巴卡(P); printf的( “SN:%S \ n”,p)的;它''返回SN:MmI4MTUxM2FjMjRlMDkzYmRkZGQyMjcwMjQ4OWY3MDAwNGZiYTM0MWNkZGIxNTdlYzAxN2' – Squidward

1

如果功能baca可以改变由输入参数指向的存储器块的内容:

更改此:

char* p = "MmI4MTUxM2FjMjRlMDkzYmRkZGQyMjcwMjQ4OWY3MDAwNGZiYTM0MWNkZGIxNTdlYzAxN2"; 

向该:

char p[] = "MmI4MTUxM2FjMjRlMDkzYmRkZGQyMjcwMjQ4OWY3MDAwNGZiYTM0MWNkZGIxNTdlYzAxN2"; 

如果功能baca不能更改输入参数指向的内存块的内容:

更改此:

char baca(char p[]) 

要这样:

char baca(const char* p) 
相关问题