Quick Search for:  in language:    
Explains,logic,program,which,inputs,file,name
   Code/Articles ╗ |  Newest/Best ╗ |  Community ╗ |  Jobs ╗ |  Other ╗ |  Goto ╗ | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 728,035. lines
 Jobs: 113. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for C/ C++.
basic math stat4u
By snehitn on 2/23


Linked List with C
By AsiF Karim on 2/23


ClassDialog
By Bonj on 2/22


[Auto Library
By Mohit Sharma1 on 2/22


Message Digest (Console)
By Vikram Shinde on 2/21


Center a Dialog Box with Respect to its Owner Window
By Ixac on 2/21


Simulation Of Gas Station / Petrol Filling Station
By Sharath AV on 2/20


ffindc
By Bonj on 2/20


Matrix_1
By Norax on 2/20


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

File test

Print
Email
 

Submitted on: 12/19/2003 12:04:27 PM
By: Kamran Riaz  
Level: Intermediate
User Rating: By 3 Users
Compatibility:C, Microsoft Visual C++, Borland C++

Users have accessed this article 654 times.
 
 
     Explains logic of a program which inputs a file name and displays number of Characters, Blanks, tabs and Lines in it with only a few lines of code. As simple as creating some loops with decisions.

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

#include <stdio.h>
#include <fcntl.h>

Includes files necassary for the program to work. "stdio.h" for standard input/output functions such as printf(), scanf() etc. and "fcntl.h" for file handling functions such as fopen()

int main()
{
Declaration of function main().


char ch;
int nol=0,not=0,nob=0,noc=0;
Variables include number of lines, number of tabs, number of blanks and number of characters


FILE *fp;
Declaring a pointer variable for file handling use.

char name[20];
Declaring a string to use for file name.

printf("Enter File Name:");
Displaying a prompt for user saying "Enter File Name:"

gets(name);
Inputting file name in form of string

fp=fopen(name,"r");
Opening a file based on filename in write mode (r) and issuing it to pointer fp

if(fp==NULL)
{
printf("\nCannot open file\n");
goto programend;
}

If file cannot be opened (thus returning fp as NULL) display "Cannot open file" and goto end of program which is displayed after file usage. (exit function can be used here but that would make things complicated)

while(1)
{
Using an infinite loop, it will keep repeating forever until we make it break out of loop.

ch=fgetc(fp);
Character variable we defined earlier in the beginning of main function is assigned the value of current character cursor is on (or under consideration in other words).

if(ch==EOF) break;
If current character under consideration is End Of File (meaning file's end has came), break out of infinite loop thus going to end of program.

noc++;
Passed above conditions, now value number of characters is increased by 1
if(ch==' ') nob++;
if current character is space/blank, value number of blanks is increased by 1
if(ch=='\n') nol++;
if current character is new line character, value number of lines is increased by 1
if(ch=='\t') not++;
if current character is tab character, value number of tabs is increased by 1
}


fclose(fp);
Close opened file

printf("\nChracters=%d \nBlanks=%d \nTabs=%d \nLines=%d \n", noc, nob, not, nol);
Display our calculations

programend:
The point of program end. if anywhere in the program it is said that goto programend; the program will jump here.

return (0);
Beginners don't need to understand this, for advanced users main function returns nothing.

}
Main function closes here.

DO NOT FORGET TO RATE THIS TUTORIAL. ITS EASY, JUST SCROLL DOWN AND YOU'LL SEE SCROLLING BOX

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.

Virus note:All files are scanned once-a-day by Planet Source Code for viruses,but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:
1)Re-scan downloaded files using your personal virus checker before using it.
2)NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 3 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyrightę 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.