reading file from commandline with argv[]

I want to get an input file and making an index out of it and saves it another file.

everything is working fine when i redirect an input file to my program like that

./myprog <text.txt

but when i try to open the file as an argument from the command line with argv[1] its not working and i cant understand why

i guess its something with how i open my file

publishing the whole code but i guess the problem is in the top of the code when i read the file

#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));
}

1 answer

  • answered 2017-06-17 18:39 Vlad Dinev

    First, please format your code, and second - after opening the file with fopen(), you are trying to read it with getchar(). This won't work, because getchar() reads from stdin. To read characters from a file you have opened with fopen() you should use fgetc().

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

    In your function get_word_freq() change now=getchar(); to now=fgetc(fp);