| | |
File operations
Thread Solved |
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
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
heres what i've done so far
C Syntax (Toggle Plain Text)
int main (void) { char fname[128]; int c, nlines,avgchar=0; int charctr= 0; FILE *file; do{ printf("Enter the name of a .TXT file:\n"); gets(fname); file=fopen(fname,"r"); if(file==NULL) { printf("Cannot open file.\n"); return EXIT_FAILURE; } else { while ((c = fgetc(file)) != EOF) { charctr++; nlines += c == '\n'; } avgchar=charctr/nlines; if((nlines<30)&&(avgchar<60)) { printf("Error!!! file cannot be accepted!\n"); } fclose(file); file=fopen("summaryData.txt","w"); fputs("avgchar",file); } }while((nlines<30)&&(avgchar<60)); return 0; }
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.
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.
•
•
•
•
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.
C Syntax (Toggle Plain Text)
int main (void) { ... while ((c = fgetc(file)) != EOF) {
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
I implemented something like this but the stats are incorrect I want to keep charctr so I can output the average characters perline.
C Syntax (Toggle Plain Text)
while ((c = fgetc(file)) != EOF) { nlines += c == '\n'; charctr++; if((charctr='\n')&&(charctr>longest)) { longest=charctr; } else if((charctr='\n')&&(charctr<shortest)) { shortest=charctr; }
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().
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:
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().
C Syntax (Toggle Plain Text)
file=fopen("summaryData.txt","w"); fputs("avgchar",file);
C Syntax (Toggle Plain Text)
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.
Just declare an array of characters. I think 512 should suffice for your purpose.
c Syntax (Toggle Plain Text)
char buffer[BUFSIZ] = {'\0'} ; fgets( buffer, BUFSIZ, stdin ) ; // here BUFSIZ is a macro which is defined as 512
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- file operations in c (C)
- File Handling in C (C)
- File Interactions (Python)
- File problem (Basic) (C)
- "File operations in c++" (C++)
Other Threads in the C Forum
- Previous Thread: Recursive String reversal returning reversed string
- Next Thread: help1
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue number odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






