2017-06-17 42 views
0

我想获取输入文件并将索引从其中取出并另存为另一个文件。使用argv从命令行读取文件[]

一切工作正常,当我重定向输入文件到我的程序一样,

./myprog <text.txt 

但是当我尝试打开该文件从argv的命令行参数[1]它不工作,我无法理解为什么

我猜它的东西与我怎么打开我的文件

发布整个代码,但我想这个问题是在代码的顶部,当我读到文件

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

#define MAXW 1000 
#define MAXC 100 



typedef struct { 
    char seen[MAXC]; 

    char lines[1024]; 


} wfstruct; 

int get_word_freq (wfstruct *words, size_t *idx, FILE *fp); 
int compare (const void *a, const void *b); 

int main (int argc, char **argv) { 
printf("%d",argc); 
printf("%s",argv[1]); 
    /* initialize variables & open file or stdin for seening */ 
    wfstruct words[MAXW] = {{{ 0 }, 0}}; 
    size_t i, idx = 0; 

FILE *fp2; 
if(argc>2) 
{ 
printf("too much args\n"); 
return 1; 
} 
if(argc<2) 
{ 
printf("give me a file\n"); 
return 1; 

} 

FILE *fp=fopen(argv[1],"r"); 
get_word_freq (words, &idx, fp); 






    /* sort words alphabetically */ 
    qsort (words, idx, sizeof *words, compare); 
    fp2 = fopen("Output.txt", "w"); 
fprintf(fp2, "The occurences of words are"); 
    printf ("\nthe occurrence of words are:\n\n"); 
    for (i = 0; i < idx; i++) 
{ 
fprintf(fp2, " %-28s : seen in lines %s \n", words[i].seen, words[i].lines); 

     printf (" %-28s : seen in lines %s \n", words[i].seen, words[i].lines); 
} 


fclose(fp2); 




    return 0; 
} 

int get_word_freq (wfstruct *words, size_t *idx, FILE *fp) 
{ 

    size_t i; 






    /* read each word in file */ 

    char *word; 
    word = malloc(sizeof(char)); 

int now; 
int line = 1; 
int j=0; 
for (;;j++) 
{ 
    now=getchar(); 
    if(now==EOF) 
    { 
     break; 
    } 
    if(!isalpha(now)){ 
     word[j] = '\0'; 
     j=-1; 
     for (i = 0; i < *idx; i++) { 
      /* if word already 'seen', update 'words[i]. freq' count */ 
      if (strcmp (words[i].seen, word) == 0) { 

      sprintf(words[i].lines + strlen(words[i].lines),"%d,",line); 







       goto skipdup; /* skip adding word to 'words[i].seen' */ 
      } 
     } /* add to 'words[*idx].seen', update words[*idx].freq & '*idx' */ 
     strcpy (words[*idx].seen, word); 

        sprintf(words[*idx].lines,"%d,",line); 





     (*idx)++; 

    skipdup: 

     if (*idx == MAXW) { /* check 'idx' against MAXW */ 
      fprintf (stderr, "warning: MAXW words exceeded.\n"); 
      break; 
     } 


    if(now=='\n'){ 
     line++; 
    } 
    continue; 
    } 
    now=tolower(now); 
    word[j]=now; 
    word=realloc(word,(j+1+1)*sizeof(char)); 

    } 






    fclose (fp); 

    return 0; 
} 

/* qsort compare funciton */ 
int compare (const void *a, const void *b) 
{ 
    wfstruct *ap = (wfstruct *)a; 
    wfstruct *bp = (wfstruct *)b; 
    return (strcmp (ap->seen, bp->seen)); 
} 
+0

./myprog <的text.txt与./myprog

+1

用随机压痕和不稳定的空格代替它,你做你的代码难以阅读。 –

+0

我会尝试重新格式化他为你们对不起.. –

回答

1

首先,请格式化您的代码,其次 - 在用fopen()打开文件后,您正试图用getchar()读取它。这是行不通的,因为getchar()从标准输入读取。要从你用fopen()打开的文件中读取字符,你应该使用fgetc()。

http://www.cplusplus.com/reference/cstdio/fgetc/

在你的函数get_word_freq()变化now=getchar();now=fgetc(fp);

+0

Ty非常花花公子! 不能相信,我错过了几个小时的东西! 现在它的工作! –