Hellpp Urgent. Please

closed account (i23bjE8b)
Hi guys I hope someone help me with this code I'm so confused what wrong in this code why it doesn't rrun. Plssssss someone help me with thisssss.


Here's the code:

#include<stdio.h>

#define F "wisdom.txt"
#define MX 500

void main(){
char s[MX], word[MX];
int i;
File *fin = fopen(F,"r");

if(!fin){

printf("Warning: No %s file found. Program is now terminating.", f);
exit(0);
}
while(fgets(s,MX,fin)!=EOF){

printf("%s",s);

word=strtok(s," ");

while(word!=NULL){

word=strtok(s," ");
i++;
}
printf("Total words: %d\n---------------\n\n",i);
}
return 0;
}
> Plssssss someone help me with thisssss.
Maybe it has something to do with your faulty keyboard.
Your 's' key seems to be particularly sticky.

http://www.catb.org/esr/faqs/smart-questions.html
For things like
- claims of unnecessary urgency
- use of English
- asking a specific question

When you post code (on any forum), there are format rules to follow.
https://www.cplusplus.com/articles/jEywvCM9/
When people see a mess, they move on quickly.

1
2
3
4
void main(){
  char s[MX], word[MX];
  int i;
  File *fin = fopen(F,"r"); 

1. main returns int, not void
2. C is case sensitive, so "File" is not the same as the "FILE" you should have used.
3. Pick decent names for all your variables.

> while(fgets(s,MX,fin)!=EOF){
fgets() returns NULL, not EOF
https://www.cplusplus.com/reference/cstdio/fgets/


There's several issues the program - including not understanding strtok()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <string.h>

#define F "wisdom.txt"
#define MX 500

int main()
{
	FILE* fin = fopen(F, "r");

	if (fin == NULL) {
		printf("Warning: No %s file found. Program is now terminating.", F);
		return 1;
	}

	for (char s[MX]; fgets(s, MX, fin) != NULL; ) {
		int i = 0;

		printf("\n%s", s);
		for (char *word = strtok(s, " "); word != NULL; ++i, word = strtok(NULL, " "));

		printf("Total words: %d\n---------------\n", i);
	}

	return 0;
}

Topic archived. No new replies allowed.