#Count lines in a CSV file

45 messages · Page 1 of 1 (latest)

marble spindle
#

Hey guys,
so I am currently trying to do something basic again, but I am struggling on one point.
So there is one solution which i find very intuitive to use:

honest hamletBOT
#

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.

marble spindle
#

!code while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}

honest hamletBOT
marble spindle
#

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)

small fern
#
while(!feof(fp))
{
  ch = fgetc(fp);
  if(ch == '\n')
  {
    lines++;
  }
}
marble spindle
small fern
marble spindle
#

wait

#

I mean yes.. but i did not reallly understood it

#

'''gcc
int main(){
while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}
}
'''

small fern
#

the only issue with feof here is that you're not checking ferr and also checking it at the wrong time

small fern
#

copy and paste from the embed when in doubt

marble spindle
small fern
#
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';
}
marble spindle
#

This looks really need!

#

But what is the technical reason behind that issue? Why can't I do it the other way around?

small fern
#

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

marble spindle
#

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.

marble spindle
#

But neat, thank you!

small fern
marble spindle
#

I use both layouts. Currently the US Layout, so its QWERTY

small fern
#

apparently there is no forward tick on QWERTY, you'd have to do Alt and then type 0180 on your numpad to get it

marble spindle
honest hamletBOT
#

@marble spindle Has your question been resolved? If so, run !solved :)

small fern
#

and you use backticks for code blocks, those can be typed on QWERTY

marble spindle
#

oh I see.. I thought I should also use the other ones at the end 😄

#
int main()
{
return 0;
}
#
int main()
{
return 0;
}
#

wat..

#
{
return 0;
}```
#

!solved