Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

linux - Count number of line using C

Is there a way to count the number of lines in my file using C?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are referring to the line number within your c source, most compilers support the __LINE__ macro. If you want to count line numbers of arbitrary text files in c, the following functions should be starting points:

  • fopen() to open a file for reading
  • fgets() to read lines
  • eof() to check for end of file
  • fclose() to close the file

Combining these into a line counter is left as an exercise to the reader :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...