I am brand new to all this and I can't get my program to read the file from input. Here is my code. It says segmentation fault (core dumped) I'm assuming because fp is not getting the file information. Can anyone help?

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define getchar() getc(stdin)

FILE *fopen(const char *filename, const char *mode);

int main(int argc, char *argv[])
{
   int i;
   int count1, count2, count3;
   FILE *fp;
   char ch;
   
   for(i=0; i<argc; i++)
      {
        count1=0; count2=0; count3=0;
        fp=fopen(argv[i], "r");
        fread(argv[i], sizeof(argv[i]), sizeof(argv[i]), fp);

        while((ch=getc(fp))!=EOF) 
          {
             if(isalpha(ch=getc(fp)))
               count1++;
             if(isspace(ch=getc(fp)))
               count2++;
             if((ch=getc(fp))=='\n')
               count3++;
             printf("The first file has %d characters, %d words, and %d lines\n"
, count1, count2, count3);
             fclose(fp);
            }
       }
    return 0;
}


]

You really should check the results of this action

fp=fopen(argv[i], "r");

Something like

if (!(fp=fopen(argv[i], "r")))
{
	fprintf(stdout, "could not open %s\n", argv[i]);
	exit(EXIT_FAILURE);
}

I put that on there and it says "file has 0 characters, 0 words, and 0lines could not open". Its not opening the file. What can I do to fix that?

Check the file system and see if the file actually exists.

file has 0 characters, 0 words, and 0lines

Is that what you typed on the command line for the name of the file ????

No. I have a makefile that executes to stats. I typed stats main.c. It prints the first file has..... because that's what I am trying to do is figure out the number of char, lines, and words in a file given from the command line. I know the file exists because its in my directory. I also tried other files I had and it still didn't work.

>>#define getchar() getc(stdin)

>>FILE *fopen(const char *filename, const char *mode);

Delete both those lines as they are declared in the header files. Its not necessary to repeat them in your programs.

for(i=0; i<argc; i++)
{
count1=0; count2=0; count3=0;
fp=fopen(argv[i], "r");

The first command line argument is argv[1], not argv[0]. The string in argv[0] is the name of the program that is being run. So that loop sould be for(i = 1; i < argc; i++)

while((ch=getc(fp))!=EOF)
{
if(isalpha(ch=getc(fp)))
count1++;
if(isspace(ch=getc(fp)))

That is just crazy coding :scared: Why are you getting characters from the file so often? Get a character at the beginning of that while loop then use the same character to test for isalpha(), isspace() etc . Reading the file for each of those is just plain dumb.

Thanks for all your help. It was really stupid I wasn't thinking. ITs all good now. Thank you!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.