![]() |
| | #1 |
| Registered User Join Date: Sep 2009
Posts: 7
| How would I go about counting the lines in a text file? I have looked up the ascii code for a CR, this being 0x15, is it as simple as scanning the file for CR's? This is just a practical exercise to get back into C. Cheers Mick |
| mickpc is offline | |
| | #2 |
| Registered User Join Date: Sep 2009
Posts: 7
| here is what I have so far... Code: //name: linecount.c
//description: counts the number of lines in a text document
//
#include <stdio.h>
#include <stdlib.h> //need for file operations?
void main(int argc,char *argv[])
{
int count=0,c;
FILE *ifp,*ofp;
ifp = fopen(argv[1],"r");
while((c=getc(ifp))!=EOF)
if(c==\r)
count++;
printf("the charater count was:%5d\n",count);
}
|
| mickpc is offline | |
| | #3 |
| Registered User Join Date: Sep 2009
Posts: 7
| Ok worked the last one out, now I am trying to convert to a function, what is wrong with this? Code: //name: linecount.c
//description: counts the number of lines in a text document
//
#include <stdio.h>
#include <stdlib.h> //need for file operations?
void linecount(int,char);
void main(int argc,char *argv[])
{
linecount(int argc,char *argv[]);
}
void linecount(int argc,char *argv[])
{
int count=0,c;
FILE *ifp,*ofp;
ifp = fopen(argv[1],"r");
while((c=getc(ifp))!=EOF)
if(c=='\n')
count++;
printf("the number of lines is:%5d\n",count);
}
|
| mickpc is offline | |
| | #4 |
| Deprecated Join Date: Oct 2004 Location: Canada
Posts: 944
| Why would you call it like this: Code: linecount(int argc,char *argv[]); Code: linecount(argc, argv); Code: void linecount(int,char); Code: void linecount(int, char**);
__________________ Warning: Have doubt in anything I post. GCC 4.5.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101 |
| Dae is offline | |
| | #5 |
| Registered User Join Date: Sep 2009
Posts: 7
| Code: //name: linecount.c
//description: counts the number of lines in a text document
//
#include <stdio.h>
#include <stdlib.h> //need for file operations?
void linecount(int, char**);
void main(int argc,char *argv[])
{
linecount(argc, argv);
}
void linecount(int, char**)
{
int count=0,c;
FILE *ifp,*ofp;
ifp = fopen(argv[1],"r");
while((c=getc(ifp))!=EOF)
if(c=='\n')
count++;
printf("the number of lines is:%5d\n",count);
}
|
| mickpc is offline | |
| | #6 |
| Deprecated Join Date: Oct 2004 Location: Canada
Posts: 944
| - main return type should be int. - main should return 0 (not required - thanks laserlight). - linecount's implementation arguments should have names (just like main). You could really improve your formatting. You should probably change the linecount interface, instead of taking argc/argv, extract the filename from argc/argv and pass it as a c-string to linecount.
__________________ Warning: Have doubt in anything I post. GCC 4.5.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101 Last edited by Dae; 09-29-2009 at 03:22 PM. Reason: edit: c-1999 |
| Dae is offline | |
| | #7 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Why on Earth would you want to pass all of argc and argv to linecount, esp. since you don't use it? If you are going to write a function, make it in some sense "functional" relative to the rest of the program, eg. Code: #include <stdio.h>
int linecount (char *file) {
int ch, total = 0;
FILE *fp = fopen(file,"r");
if (!fp) return -1;
while ((ch=fgetc(fp)) != EOF) if (ch==10) total++;
fclose(fp);
return total;
}
int main (int argc, char *argv[]) {
int num_of_lines;
if (argc<2) {
puts("Filename required!");
return 0;
}
num_of_lines = linecount(argv[1]);
printf("\n%d lines in \"%s\"\n",num_of_lines,argv[1]);
return 0;
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #8 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| Code: #include <stdio.h>
#define EOL '\n'
#define CAR_RETURN '\r'
int line_count(char* __str_file_name) {
FILE* fd;
if ((fd = fopen(__str_file_name, "r")) == NULL) {
printf("[Error] : While opening the file\n");
exit;
}
unsigned int line_count = 0;
while ( (ch = fgetc(fd)) != EOF)
if (ch == EOL || ch == CAR_RETURN)
++line_count;
if (fd) {
fclose(fd);
}
return line_count;
}
int main(const unsigned int argc, char* argv[] ) {
int status = 0;
if (argc < 2)
status = -1;
else
printf("Number of lines in file[%s] == [%d]\n", argv[1], line_count(argv[1]));
return status;
}
|
| RockyMarrone is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Memory Address | kevinawad | C++ Programming | 18 | 10-19-2008 10:27 AM |
| Formatting a text file... | dagorsul | C Programming | 12 | 05-02-2008 03:53 AM |
| Formatting the contents of a text file | dagorsul | C++ Programming | 2 | 04-29-2008 12:36 PM |
| Basic text file encoder | Abda92 | C Programming | 15 | 05-22-2007 01:19 PM |
| Batch file programming | year2038bug | Tech Board | 10 | 09-05-2005 03:30 PM |