#Count lines in a CSV file
45 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
!code while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}
```cpp
int main() {}
```
int main() {}
But there are some statements about this code that, it is unsafe or even wrong to use !feof(fp) in this case.
Are there any other ways to get the number of lines of a file?
(In C language)
while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}
What have you done right so it got formattet that way? xD
this
wait
I mean yes.. but i did not reallly understood it
'''gcc
int main(){
while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}
}
'''
the only issue with feof here is that you're not checking ferr and also checking it at the wrong time
you're using the wrong quote character
copy and paste from the embed when in doubt
ah damn it
while(1)
{
ch = fgetc(fp);
if (feof(fp)) break;
if(ch == '\n')
{
lines++;
}
}
this would be the right order of things
the convenient thing about fgetc is that it returns EOF when reading failed, so you don't need feof in the first place
while((ch = fgetc(fp)) != EOF)
{
if(ch == '\n')
{
lines++;
}
}
and you could golf this further to be branchless
while((ch = fgetc(fp)) != EOF)
{
lines += ch == '\n';
}
This looks really need!
But what is the technical reason behind that issue? Why can't I do it the other way around?
because feof checks if the previous operation reached the end of the file
you don't actually know whether that happened before you do fgetc
the general rule in C is: read/write first, check for errors after
your code should also work by coincidence though:
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
this would be safe even if ch is EOF, so there's not really an issue
and '\n' == EOF is never true, so there's no false counting
BTW. I can type in this character: " ` " with the key where i get my tilde from, but how do I get the other sheered quote character? It should be one which is two keys left to my Enter button, but that isn't working. Nothing happens when I press it with the alt key together.
Oh I see okay 😄
But neat, thank you!
I'm not sure where it is in on a QWERTY keyboard, I use QWERTZ
I use both layouts. Currently the US Layout, so its QWERTY
apparently there is no forward tick on QWERTY, you'd have to do Alt and then type 0180 on your numpad to get it
I usw Arch btw.
😄
Damn okay, Is it a thing for the server to use those ticks? Or can that be changed?
@marble spindle Has your question been resolved? If so, run !solved :)
it's always like that on Discord
and you use backticks for code blocks, those can be typed on QWERTY