2015-10-20 134 views
-6

我想從數組中獲取輸入,輸出和數據文件的名稱以便進一步處理。但是,我收到了一個奇怪的錯誤或問題。所以,我的程序沒有達到for循環。它甚至沒有在for循環之前打印語句。但是,我嘗試使用調試器,程序正確地逐步打印。所以,當我運行它不打印,當我一步一步調試它打印。這很奇怪!指針讀取不正確

char *method; 
     method=malloc(25); 
     method=NULL; 
     char *dataFileName; 
     char *inputMethod; 
     inputMethod=malloc(25); 
     inputMethod=NULL; 
     char *inputFileName; 
     char *outputMethod; 
     outputMethod=malloc(25); 
     outputMethod=NULL; 
     char *outputFileName; 
     char *commandArray[]={"if=q.txt","of=output.txt"}; 
     char**args=(char**) malloc(sizeof(char*)*256); 
     args=commandArray; 
     int i; 
     printf("Before second for"); 
     for(i=0;i<2;i++) 
     { 
      printf("I am here"); 
      if(*args[i]=='d') 
       { 
        method=strtok_r(args[i],"=",&dataFileName); 
        printf("The method given is %s",method); 
        printf("Data File Name is %s",dataFileName); 
       } 
       else if(*args[i]=='o') 
       { 
        outputMethod=strtok_r(args[i],"=",&outputFileName); 
        printf("The output method given is %s",outputMethod); 
        printf("output File Name is %s",outputFileName); 
       } 
       else 
       { 
        inputMethod=strtok_r(args[i],"=",&inputFileName); 
        printf("The input method given is %s",inputMethod); 
        printf("Input File Name is %s",inputFileName); 
       } 
      } 

     if(method==NULL) 
     { 
       dataFileName=malloc(256); 
       printf("Please Enter A File Name"); 
       scanf("%255s",dataFileName); 
       printf("%s",dataFileName); 
     } 

     if((inputMethod==NULL)||(outputMethod==NULL)) 
     { 
      char* array[]={"stdin","stdout"}; 
      if(inputMethod==NULL) 
       inputMethod=array[0]; 
      if(outputMethod==NULL) 
       outputMethod=array[1]; 
     } 

我正在開發使用C中的Netbeans。上面的代碼寫在主要內部。謝謝!

+5

請開始閱讀一個C的書。你的「代碼」中存在許多基本錯誤,導致C中缺乏對基本概念的理解。 – Olaf

+3

你想做什麼?一步你分配內存到指針,並在下一行你讓他們指向NULL,然後再分配內存。 – ameyCU

+0

我知道我嘗試了很多調整來讓這個東西以某種方式運行,但仍然無法弄清楚。這就是原因。 – Sankalps

回答

0

我故意留下以前的答案,因爲理解的內存分配是微不足道的編程在C特別。正如我看到你有一個很大的問題。

但你仍然有問題在幾乎每一件事情。在我的實際答案中,我將嘗試簡化你如何使用strtok來拆分字符串並解析它。我想這是你的代碼的第二個主要問題。

代碼:

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


int main(void){ 
    char commandArray[][256]={ 
     "if=q.txt", 
     "of=output.txt" 
    }; 

    char infile[256], outfile[256]; 

    for(int i=0; i<2;i++){ 

     char *ptr,*cmd; 

     cmd=commandArray[i]; 
     ptr=NULL; 

     printf("parsing command '%s'\n",cmd); 

     cmd=strtok(cmd,"="); 
     ptr=strtok(NULL,"="); 

     if(!cmd){ 
      printf("Error parsing the string '%s'\n",commandArray[i]); 
      exit(1); 
     } 

     if (strcmp(cmd,"if")==0){ 
      strcpy(infile,ptr); 
     } 
     else if (strcmp(cmd,"of")==0){ 
      strcpy(outfile,ptr); 
     } 
     else{ 
      printf("unknow token '%s'\n",cmd); 
      exit(1); 
     } 
    } 


    printf(
     "\n\n" 
     "input file: '%s'\n" 
     "output file: '%s'\n" 
     "\n\n", 
     infile,outfile); 

    return 0; 
} 
+0

謝謝!當我在分配之前分配內存時,問題就解決了! – Sankalps

0

的主要問題是:

char *method; 
method=malloc(25);//allocating space for 25 char 
method=NULL; // throwing up the allocation without freeing it; 
      // now the allocation is lost 
      // now method is useless (it is null) 
+0

這是一個大問題,但我不確定這是否是* main *問題。還有很多其他的大問題。 –

+0

我沒有分析過代碼。那個指針剛剛跳到我眼前。 – milevyo