2017-02-14 81 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) 
相關問題