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

int main()
{
    int count = 0;
    char *doc;
    FILE *inp;
    
    printf("Enter the file name: ");
    scanf("%s", doc);
    
    inp=fopen(doc, "r");
    
    while((doc = fgetc(doc)) != EOF)
    {
        if (doc == ' ')
            count++;
    }

    fclose(doc);
    
    printf("%s contains %d words\n",doc,count);
    
    return 0;
}

So basically I'm a little bit confused. I believe the logic of my program is correct; file pointer is declared, and I do have a character to store my array, it reads the file, and counts all the words pending a space in front/between/etc. Basically the program should ask the used the used to input the name of the file they're scanning. The file opens and goes through the program and outputs a line that tells the user the # of words in the file that was inputted.

My problem has been compiling it.

I'm getting this:

wordcount.c: In function ‘main’:
wordcount.c:13:5: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:271:14: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
wordcount.c:15:5: warning: passing argument 1 of ‘fgetc’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:535:12: note: expected ‘struct FILE *’ but argument is of type ‘char’
wordcount.c:21:5: warning: passing argument 1 of ‘fclose’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:236:12: note: expected ‘struct FILE *’ but argument is of type ‘char’

So basically I'm a little bit confused. I believe the logic of my program is correct; file pointer is declared, and I do have a character to store my array, it reads the file, and counts all the words pending a space in front/between/etc. Basically the program should ask the used the used to input the name of the file they're scanning. The file opens and goes through the program and outputs a line that tells the user the # of words in the file that was inputted.

My problem has been compiling it.

How can it possibly do all that stuff you claim it does if the program doesn't even compile? The mind boggles! :icon_rolleyes:


Your code:

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

int main()
{
    int count = 0;
    char *doc;              // Where is the actual storage?  A pointer 
                            //   does not hold data, it holds a pointer
    FILE *inp;
    
    printf("Enter the file name: ");
    scanf("%s", doc);       // See the scanf() link below
    
    inp=fopen(doc, "r");    // Why are you opening the file with a char*? 
                            //   Aren't you supposed to use a FILE pointer?
    
    while((doc = fgetc(doc)) != EOF)    // You get a character and
                                        // 1) destroy your file pointer
                                        // 2) destroy your character pointer.
    {
        if (doc == ' ')     // doc is a pointer.  It can't be == to a character.
            count++;
    }

    fclose(doc);
    
    printf("%s contains %d words\n",doc,count);
    
    return 0;
}

scanf()

commented: Ever consider that someone has been learning this on the side for less than a week or two? -1
commented: Attempts to help people develop their skills themselves, accurate comments and assistance +3

Here comes captain douchebag thinking thread is someone asking another to write the code for them.

commented: I gove direct help and you post this comment? What a lame-ass! -4
commented: what are you talking about? -1
#include <stdio.h>
#include <string.h>

int main()
{
    int count = 0;
    char *doc;
    FILE *inp;
    
    printf("Enter the file name: ");
    scanf("%s", doc);
    
    inp=fopen(doc, "r");
    
    while((doc = fgetc(doc)) != EOF)
    {
        if (doc == ' ')
            count++;
    }

    fclose(doc);
    
    printf("%s contains %d words\n",doc,count);
    
    return 0;
}

So basically I'm a little bit confused. I believe the logic of my program is correct; file pointer is declared, and I do have a character to store my array, it reads the file, and counts all the words pending a space in front/between/etc. Basically the program should ask the used the used to input the name of the file they're scanning. The file opens and goes through the program and outputs a line that tells the user the # of words in the file that was inputted.

My problem has been compiling it.

I'm getting this:

wordcount.c: In function ‘main’:
wordcount.c:13:5: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:271:14: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
wordcount.c:15:5: warning: passing argument 1 of ‘fgetc’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:535:12: note: expected ‘struct FILE *’ but argument is of type ‘char’
wordcount.c:21:5: warning: passing argument 1 of ‘fclose’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:236:12: note: expected ‘struct FILE *’ but argument is of type ‘char’

The root of all problems in your program is this line

char *doc;

Walt is right ,"A pointer does not hold a data, it holds a pointer" i.e the address of where the data actually is.You better declare an array of chars

char file_path[25];

or you have to allocate memory for the data using malloc.

char* file_path;
if((file_path=malloc(25))==NULL)
    exit(1);//couldn't allocate memory

Though you can use scanf,

scanf("%s",file_path);

I'd recommend fgets() instead

if((fgets(file_path,sizeof(file_path),stdin))!=NULL){
char *ptr=strchr(file_path,'\n'); //replace the trailing zero     
*ptr='\0';//with '\0' character
}

You also better check if the file you're going to open exists or not,

if((inp=fopen(file_path,"r"))==NULL)
    exit(1);//exit if couldn't open the file

Use another int variable while reading characters from the file, since fgetc returns an int.

int c;
while((c = fgetc(inp)) != EOF){
    if (c == ' ')// now the comparision is possible    
        count++;
}

and finally, fclose takes file handle as it's only argument.So it should be

fclose(inp);

Here comes captain douchebag thinking thread is someone asking another to write the code for them.

What the h--- are you talking about?

Your description claims everything was working. I commented on that. Nothing is working.

I then added comments in your code showing what was wrong with the code itself?
Where do you get the asinine idea I thought you were asking for us to write it for you? Please, explain how you got that impression? I need to know... Please.

@MrNo - What the h... dude? Walt is actually giving you assistance with your code, NOT writing it for you, and encouraging you to develop your understanding and knowledge your damn self

Stop being a complete ass and learn a thing or 2, your comment you state you've only been learning this on the side for a while? so LEARN!!! dont mope and insult people encouraging that...

This forum is actually a set of decent people helping each other and you attitude is simply going to make LOADS of people ignore you and not offer any assistance any more...

commented: Thanks.... +17

@MrNo - What the h... dude? Walt is actually giving you assistance with your code, NOT writing it for you, and encouraging you to develop your understanding and knowledge your damn self

Stop being a complete ass and learn a thing or 2, your comment you state you've only been learning this on the side for a while? so LEARN!!! dont mope and insult people encouraging that...

This forum is actually a set of decent people helping each other and you attitude is simply going to make LOADS of people ignore you and not offer any assistance any more...

Although the thread is posted pretty recently(just November last year and his last activity being december 2nd) I don't think MrNo will give a proper explanation less likely a proper reply, I think this thread should be left alone now or maybe closed... just saying

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.