number of lines in c++ files
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char ch;
    int words = 0, characters=0 , lines=0;

    ifstream file("myfile.txt");
    file.seekg(0,ios::end); // points to the end of file
    int length = file.tellg(); // returns the end of file's index , if its 0 then the file is empty

    if(!file) // checks the existence of file
    {
        cout<< "file was not found ";
    }

    if (length == 0)
    {
        cout << "file is empty\n ";
        cout << "words = " <<words <<endl
             << "line = " <<lines <<endl
             << "characters = " << characters <<endl;
    }

    else
    file.seekg(0,ios::beg); // pointer set to beginning as it was pointing to the end
    {
        while (file)
        {
            file.get(ch);
            if (ch == ' ')
            {
                words ++; 
            }

            if (ch == '\n')
            {
                lines ++;
            }

            characters ++;
        }
        cout<< "words = " <<words+1 <<endl // they are incremented to one because they were initialized to 0
            << "line = "  <<lines+1 <<endl
            << "characters = " << characters+1 <<endl;

        return 0;

        /*
        there might be a question that , what if there was no words or letters or line,
        the program will always give 1
        the answer is a no character case occurs if the file is empty
        since the program already checks for that case, its ggs
        */
    }
}

C++相关代码片段

how to kill

hello world cpp

cpp hello world

when kotlin

how to write hello world c++

fenwick tree

main function

python loop

is palindrom

subsequence

insertion sort

react cookie

data structure

Stack Modified

increment integer

496. Next Greater Element I.cpp

c++ freecodecamp course 10 hours youtube

simple interest rate

deliberation meaning

fingerprint

c++ system() from variable

operator using

unambiguous

Stream Overloading

quick_sort

hash table

graph colouring

the question for me

fname from FString

how togreper

is plaindrome

logisch oder

robot framework for loop example

spyder enviroment

pallindrome string

ue4 c++ string from fvector

interactive problem

two sum solution

interpolation search

Required Length

new expression

Targon lol

xor linked list

stack implementation

typescript

loop through array

loop array

how to point to next array using pointer c++

file exist

floating point exception

array di struct

castin in C++

varint index

how to make a instagram report bot python

windows servis from console app

add integers

listas enlazadas/ linked lists

niet werkend

bubble sort

triangle angle sum

divisor summation

rotateArray

destiny child

Populating Next Right Pointers in Each Node

cosnt cast

bucket sort

double plus overload

Z-function

binary search

permutation

linked list

Implicit conversion casting

square root

public method

tower of hanoi

selection sort

dangling pointer

hello world programming

statements

volumeof a sphere