I need a way to count the number of lines in a file. I've tried a couple things with ctype but no luck, and out of ideas.. for tonight anyway.
I tried :
#include <ctype.h>
do {
fin.get(some_char);
if ( iscntrl(some_char) )
count++
} while ( isascii(some_char) );
Thinking that "if(iscntrl(some_char))" would count newlines (?) but it seems to keep looping due to isascii (the file is alphanumeric and punctuation)
Thanks,
~Jeff
EscapeCharacter
10-25-2001, 08:07 AM
have you tried loading the file into some buffer variable and then just checking that for newlines?
something like the following
char *buf = infile;
int count = 0;
int x = 0;
while(buf[x] != EOF){
if(buf[x] == '\n')
count++;
x++;
}
TheLinuxDuck
10-25-2001, 09:20 AM
JBrian:
Instead of reading char by char, why not use the getline function. It will only get up to the newline, anyway, and it should be faster than reading char by char.. maybe something like:
Of course, if you want to do subsequent reading from the file, it will require a rewind, or a close/open on that file stream again. (^=
Hope this helps!
jemfinch
10-25-2001, 10:58 AM
int i, c;
while((c = getchar()) != EOF)
if(c == '\n')
++i;
Jeremy
kmj
10-25-2001, 11:34 AM
uh, jemfinch, shouldn't that be fgetc() ?
TheLinuxDuck
10-25-2001, 01:01 PM
Originally posted by kmj:
<STRONG>uh, jemfinch, shouldn't that be fgetc() ?</STRONG>
Unless there's something I don't know about, I don't think fgetc will work with a cPP filestream. maybe I'm just drunk.
kmj
10-25-2001, 01:52 PM
Originally posted by TheLinuxDuck:
<STRONG> Unless there's something I don't know about, I don't think fgetc will work with a cPP filestream. maybe I'm just drunk.</STRONG>
I don't think jemfinch's example uses cpp filestreams at all. getchar is a C stdio.h function.
TheLinuxDuck
10-25-2001, 01:54 PM
Originally posted by kmj:
<STRONG>I don't think jemfinch's example uses cpp filestreams at all. getchar is a C stdio.h function.</STRONG>
That in itself is a conundrum, since JBrian was asking for aide with cpp, not c... (^=
kmj
10-25-2001, 01:58 PM
Well, cpp is a superset of c, so by providing an answer using c code, I'm answering his question. That's irrelevant anyway, since my reply was not directed at JBrian's question, but was correction of jemfinch's reply to JBrian's question.
TheLinuxDuck
10-25-2001, 02:03 PM
Originally posted by kmj:
<STRONG>Well, cpp is a superset of c, so by providing an answer using c code, I'm answering his question.</STRONG>
Although it may be a valid answer, it is not true to the language. I could write a C program that called system calls and executed perl scripts to do everything I wanted to do, but it isn't really using C for it's power and functionality...
<STRONG>That's irrelevant anyway, since my reply was not directed at JBrian's question, but was correction of jemfinch's reply to JBrian's question.</STRONG>
Maybe so, but without some type of explanation that jemfinches code sample, and your subsequent reply, does not use cpp to it's full potential, that might confuse someone who is reading this thread to learn more about using cpp.
Therefore I submit that it is VERY important!!
(^= The important thing is to learn, right? (^=
kmj
10-25-2001, 02:17 PM
Originally posted by TheLinuxDuck:
<STRONG> Maybe so, but without some type of explanation that jemfinches code sample, and your subsequent reply, does not use cpp to it's full potential, that might confuse someone who is reading this thread to learn more about using cpp.
Therefore I submit that it is VERY important!!
(^= The important thing is to learn, right? (^=</STRONG>
I submit that you're a bloody wanker.
Executing system calls or embedding code from another language is very different from using a different library to accomplish a task. In cpp, there are many ways to access a file. In my mind, the best way to do it depends entirely on context, which we have none of in this case. Without context, we should resort to the most efficient method. There's no need to involve the whole class hierarchy of streams when all we want to do is simply count the number of lines in a file; it's wasted overhead. Object Oriented programming allows us many advantages in design, but to use it forces you to incur substantial overhead, which in small programs is way unnecessary. For small programs, chances are that an OOP approach is inappropriate. There is nothing wrong practically speaking with using standard c functions to accomplish real world tasks if the use of those functions is easily incorporated with your program and will most assuredly result in a more efficient executable.
About the wanker thing, mainly I say that because, well you are. While the learning process may be important, that does not change the fact that I wasn't necessarily involved in it because I couldn't help him any more than he already had. Someone said something which I believed to be incorrect, and in an effort to ensure that no poor soul attempts to follow such incorrect advice, I corrected him.
What's important is that I saw a chance to correct jemfinch, and that is not something one passes up. Far more important, in my mind, than some silly "learning". Wonk. In fact, I think you only launched your initial barb at me because you were jealous that you were not the one who got to make the correct.
TheLinuxDuck
10-25-2001, 02:33 PM
Originally posted by kmj:
<STRONG>I submit that you're a bloody wanker.</STRONG>
If that was true, I would agree, however, in this case, it is not. I simply am not bloody. (^=
<STRONG>In my mind, the best way to do it depends entirely on context, which we have none of in this case.</STRONG>
No context? We're counting the number of lines in a text file.. how much more context do you want!? (^=
<STRONG>There's no need to involve the whole class hierarchy of streams when all we want to do is simply count the number of lines in a file; it's wasted overhead. Object Oriented programming allows us many advantages in design, but to use it forces you to incur substantial overhead, which in small programs is way unnecessary. For small programs, chances are that an OOP approach is inappropriate.</STRONG>
I agree wholeheartedly that the OO of cpp adds much overhead. That has been one of my biggest complaints when learning cpp. However, we do not know that the program, to which the line counting will be added, hasn't already added that overhead for other things it is doing. It would be falacy to say I know this for certain, however, my conclusion was based on the fact that a cpp file stream was already in use, and therefore the overhead of the stream class was already there.
And yes, small programs would be smaller without the overhead, and possible execute quicker.. however, my perl teacher taught me "when using perl, use perl". I think that this can apply to any language.
<STRONG>In fact, I think you only launched your initial barb at me because you were jealous that you were not the one who got to make the correct.</STRONG>
Truth be known, I only gave you a rough time of it because I consider you a friend.
Yes, get out your tissues, ladies and gentlemen! (^=
As for system calls being completely different than using the C functionality in CPP, I don't think they are completely different.. after all, using a system call is no different than calling any other function. A system call is a part of the language. Though, I admit that they are different enough that my example was not the best example. I'm not very good with debating... I generally can't think of anything to say until 15 minutes after the whole thing is over. (^=
kmj
10-25-2001, 02:45 PM
Originally posted by TheLinuxDuck:
<STRONG> Truth be known, I only gave you a rough time of it because I consider you a friend.
Yes, get out your tissues, ladies and gentlemen! (^=
</STRONG>
What!? How dare you call me a fiend! Why if you weren't way out there in Oklahomaha, Nebrarkansas, I'd come out there and lay beatin' on you like you've never... what? oh, friend. oh, ::blush:: uh.. okay...
<STRONG>
As for system calls being completely different than using the C functionality in CPP, I don't think they are completely different.. after all, using a system call is no different than calling any other function. A system call is a part of the language. Though, I admit that they are different enough that my example was not the best example. I'm not very good with debating... I generally can't think of anything to say until 15 minutes after the whole thing is over. (^=</STRONG>
Should I wait fifteen minutes?
It is different because a 'system' or 'exec' forks off a new process, which is a way big hit in speed and memory... way unnecessary. C/C++ share a relationship that most other languages do not. printf() can be found scattered throughout many many cpp projects. A guy's perfectly within his rights to use stdlib functions in response to a c question, especially given that there are numerous nonstandard cpp libraries out there (though the stl is getting more widely used).
TheLinuxDuck
10-25-2001, 02:54 PM
Originally posted by kmj:
<STRONG>It is different because a 'system' or 'exec' forks off a new process, which is a way big hit in speed and memory... way unnecessary. C/C++ share a relationship that most other languages do not. printf() can be found scattered throughout many many cpp projects. A guy's perfectly within his rights to use stdlib functions in response to a c question, especially given that there are numerous nonstandard cpp libraries out there (though the stl is getting more widely used).</STRONG>
Oh yea!!
Well.. .. . shut up!
jemfinch
10-25-2001, 03:52 PM
Since we're being pedantic and all...
"cpp" is the C PreProcessor. It runs through a C file and does all the wacky
stuff with #define and #include and all.
"C++" is a programming language.
Jeremy
JBrian
10-25-2001, 04:05 PM
Umm... Thanks =)
TheLinuxDuck
10-25-2001, 04:11 PM
Originally posted by jemfinch:
<STRONG>"cpp" is the C PreProcessor. It runs through a C file and does all the wacky
stuff with #define and #include and all.
"C++" is a programming language.
</STRONG>
Yea.. I tend to call c++ cpp, using p as an abbreviation of plus..
So, from now on, anyone who ever sees me type cpp, I mean c++, ok? (^=
kmj
10-25-2001, 05:25 PM
Originally posted by jemfinch:
<STRONG>Since we're being pedantic and all...
"cpp" is the C PreProcessor. It runs through a C file and does all the wacky
stuff with #define and #include and all.
"C++" is a programming language.
Jeremy</STRONG>
I think it's fine to refer to c++ as cpp since c++ files have the extension cpp and it's obvious from the context what we're talking about. I didn't really need to say this, but I wanted to have the last word.
inline bool isNewLine(char c) { return c == '\n'; }
int main(void)
{
ifstream infile("test.txt");
size_t lines = 0;
if (infile)
lines = count_if(istreambuf_iterator<char>(infile), istreambuf_iterator<char>(), isNewLine);
cout << "Lines: " << lines << endl;
return 0;
}
jemfinch
10-26-2001, 12:41 AM
Originally posted by kmj:
I think it's fine to refer to c++ as cpp since c++ files have the extension cpp and it's obvious from the context what we're talking about. I didn't really need to say this, but I wanted to have the last word.
Ah, but a .C file is a C++ file too -- does that mean whenever anyone talks
about "C" they mean C++?
:p
Jeremy
justlinux.com
Copyright Internet.com Inc. All Rights Reserved.