File operations

Thread Solved

Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

File operations

 
0
  #1
Nov 15th, 2006
I working on a program that summerizes text documents and I need a way to find the length of the longest and shortest lines in the file and display it in a log file. I've already done a character count and line count. Also say I wanted to created different logfiles for each document i summerize how can this be achieved without overwriting the previos log file?
heres what i've done so far
  1. int main (void)
  2. {
  3. char fname[128];
  4. int c, nlines,avgchar=0;
  5. int charctr= 0;
  6. FILE *file;
  7.  
  8. do{
  9.  
  10. printf("Enter the name of a .TXT file:\n");
  11. gets(fname);
  12.  
  13. file=fopen(fname,"r");
  14.  
  15. if(file==NULL)
  16. {
  17. printf("Cannot open file.\n");
  18. return EXIT_FAILURE;
  19.  
  20. }
  21. else
  22. {
  23.  
  24.  
  25.  
  26. while ((c = fgetc(file)) != EOF)
  27. {
  28.  
  29. charctr++;
  30. nlines += c == '\n';
  31.  
  32. }
  33. avgchar=charctr/nlines;
  34.  
  35.  
  36.  
  37. if((nlines<30)&&(avgchar<60))
  38. {
  39. printf("Error!!! file cannot be accepted!\n");
  40.  
  41.  
  42. }
  43. fclose(file);
  44.  
  45. file=fopen("summaryData.txt","w");
  46. fputs("avgchar",file);
  47.  
  48.  
  49.  
  50.  
  51. }
  52. }while((nlines<30)&&(avgchar<60));
  53.  
  54.  
  55. return 0;
  56. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,337
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1459
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File operations

 
0
  #2
Nov 15th, 2006
open the log file for append mode, not write mode, if you want just one log file that contains info for all files
fopen(filename,"a");

or create a unique log file name if you want results in separate log files.
Last edited by Ancient Dragon; Nov 15th, 2006 at 11:31 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: File operations

 
0
  #3
Nov 16th, 2006
Originally Posted by boujibabe View Post
I working on a program that summerizes text documents and I need a way to find the length of the longest and shortest lines in the file and display it in a log file. I've already done a character count and line count.
  1. int main (void)
  2. {
  3. ...
  4. while ((c = fgetc(file)) != EOF)
  5. {
Instead of fgetc() use fgets() to read an entire line. Then you can add a loop to continue doing the part you have already done, and you can check the length of the line for the min and max.

Or start a counter to count each character and when you read the '\n' test that counter against the current min & max. Then reset the counter to 0 and continue.

I recommend reading the first line and set min & max to the length of that line. That would be a good starting point.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: File operations

 
0
  #4
Nov 16th, 2006
I implemented something like this but the stats are incorrect I want to keep charctr so I can output the average characters perline.

  1. while ((c = fgetc(file)) != EOF)
  2. {
  3.  
  4. nlines += c == '\n';
  5. charctr++;
  6.  
  7.  
  8. if((charctr='\n')&&(charctr>longest))
  9. {
  10. longest=charctr;
  11. }
  12. else
  13. if((charctr='\n')&&(charctr<shortest))
  14. {
  15. shortest=charctr;
  16. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,337
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1459
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File operations

 
0
  #5
Nov 16th, 2006
>>but the stats are incorrect
how do you know that? What are you comparing them to?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: File operations

 
0
  #6
Nov 16th, 2006
the longest line is always ten characters the number of lines is one short and nothing is being written to the log file
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,337
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1459
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File operations

 
0
  #7
Nov 16th, 2006
does the last line of the file contain '\n' ? If not, then your program will not count it. fgets() will fix that problem because it reads all lines. Getting the length of the longest line is trivel too -- just use strlen() to get the length of the line read by fgets().

The size of the file can not be determined by either your program or using fgets() becuase in MS-Windows os reading text files converts "\r\n" into just "\n". It will be easier to get file length by calling fstat() or stat().

  1. file=fopen("summaryData.txt","w");
  2. fputs("avgchar",file);
you forgot to close the file after writing to it. This will cause your program to eventually run out of file handles. And the fputs() statement is probably incorrect -- all it is doing is printing the same hard-coded text "avgchar" in the file each time. Use something like this:
  1. fprintf(file,"%d\n", avgchar);
Last edited by Ancient Dragon; Nov 16th, 2006 at 12:58 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: File operations

 
0
  #8
Nov 16th, 2006
How do I use fgets if I am not sure of the maximum line size?


while (fgets(line, LINE_MAX, file) != EOF)
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: File operations

 
0
  #9
Nov 16th, 2006
Just declare an array of characters. I think 512 should suffice for your purpose.

  1. char buffer[BUFSIZ] = {'\0'} ;
  2. fgets( buffer, BUFSIZ, stdin ) ;
  3. // here BUFSIZ is a macro which is defined as 512
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: File operations

 
0
  #10
Nov 16th, 2006
If you use EOF you will be laughed at. He he. DON'T
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC