944,123 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 7304
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 15th, 2006
0

File operations

Expand 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. 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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 15th, 2006
0

Re: File operations

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 16th, 2006
0

Re: File operations

Click to Expand / Collapse  Quote originally posted by boujibabe ...
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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Nov 16th, 2006
0

Re: File operations

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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 16th, 2006
0

Re: File operations

>>but the stats are incorrect
how do you know that? What are you comparing them to?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 16th, 2006
0

Re: File operations

the longest line is always ten characters the number of lines is one short and nothing is being written to the log file
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 16th, 2006
0

Re: File operations

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 16th, 2006
0

Re: File operations

How do I use fgets if I am not sure of the maximum line size?


while (fgets(line, LINE_MAX, file) != EOF)
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 16th, 2006
0

Re: File operations

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
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 16th, 2006
0

Re: File operations

If you use EOF you will be laughed at. He he. DON'T
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Recursive String reversal returning reversed string
Next Thread in C Forum Timeline: help1





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC