2015-10-20 78 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