473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Count words in a txt file

Hello, I am a beginner of c. I wrote a program about counting words in a txt file.But actually there are some error in it and it will stop counting at the first paragraph and just return the number of words i want to count in the 1st paragrah. Can anybody tell what is going wrong in my program? Thank you very much.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define SIZE 1000
  5.  
  6. char *search(char *, char *);
  7.  
  8. int main()
  9. {
  10.     char string1[SIZE], string2[SIZE], oriword[30], 
  11.          *newword, file1[30];
  12.     int  count;
  13.     FILE *inFile;
  14.  
  15.     printf("Please enter an input file name: ");
  16.     scanf("%s", &file1);
  17.     inFile = fopen(file1, "r");
  18.     printf("\nWhich word do you want to find: ");
  19.     scanf("%s",&oriword);
  20.     while (fgets(string1, SIZE, inFile) != NULL);
  21.     newword = search(string1, oriword);
  22.     count = 0;
  23.     while (newword != NULL)
  24.     {
  25.           count++;
  26.           newword = search(newword + strlen(oriword), oriword);
  27.           printf("%s\n", newword);
  28.     }
  29.  
  30.     printf("\nThe number of %c%s%c appeared %d time(s).\n", '"', oriword, '"', count);  
  31.     fclose(inFile);
  32.     fflush(stdin);
  33.     printf("\nPlease press the Enter key to continue......\n");
  34.     getchar();
  35.     return 0;
  36. }
  37.  
  38.  
  39. char *search(char *string3, char *owd)
  40. {
  41.      int j, k;
  42.      char *tmp;
  43.  
  44.      j = strlen(string3);
  45.      k = strlen(owd);
  46.      for (tmp = string3; tmp <= string3 + j - k; tmp++)
  47.      {         
  48.           if ((tmp == string3 || *(tmp - 1) == ' ' || *(tmp - 1) == '.')
  49.           && strncmp(tmp, owd, k) == 0 
  50.           && (*(tmp + k) == ' ' || *(tmp + k) == '.' || *(tmp + k) == '\0'))
  51.               return tmp;
  52.      }
  53.      return NULL;
  54. }
  55.  
  56.  
i am using win xp, my compiler is dev-c++ 4.9.9.2
Dec 2 '07 #1
4 4694
Dev C++ has a nice debugger;while debugging,you can use the "watch variable" option,wich lets you check the value of variables in every moment.
Dev C++ debugger only works with single file source code;it will not work with a file included in a .dev project file.
Dec 2 '07 #2
Thanks for your reply. But my program can be complied and run by Dev-C++. It doesn't contain any syntax, logical or runtime error. The error i mean is that the program cannot match my goal --- to count the words in the whole txt file but not just the 1st paragraph. So I need yours help to tell how should i rewrite the program to meet my requirement.
Dec 2 '07 #3
Yes OK I understand that your code does not contain any syntax error(because if it had syntax error ,debugger would not even start);but you should use debugger to search for semantic error as well.
Semantic errors occur when a variable has a value that it should not,or when a variable has a value that it is not expected to have according to the algorithm that you designed.
Semantic errors are the hardest kind of error to look for,and a debugging session should help you veru much.
Dec 2 '07 #4
Thank you for reply. I know where is the error now. Thank you for your patient reply.
Dec 2 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
9
by: dan | last post by:
this is a program to count average letters per word. i am able to count the total number of letters, but not words. How do you count the total number of words in a text file, so i am able to divide...
11
by: Foodbank | last post by:
Hello, I'm trying to develop a program that will enable me to count the number of words in a text file. As a plus, I'd like to be able to count how many different words there are too. I have a...
9
by: aaron | last post by:
I have a few documents in which I need to get a total word count. Can anyone help? I'd like to create this program so I can get the result after entering the filename, and have the option to...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
3
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
7
by: supriyanaidu | last post by:
Hi i am new to this forum . i am doing small project in that i want to count the number of words in the given .for example in my give file the content will be in this format Firstname, last...
3
by: frozz85 | last post by:
I need to search through the file and look for lines that begin with "From". I need to parse the From line and print out the second word for each From line and then also count the number of From...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.