#include <iostream>

int main(int argc, char *argv[])
{
int count=0;
if (argc<2)
{
           std::cout<<"Wrong No Of Parameters";
           exit(1);
}
FILE *file;

file=fopen(argv[1],"r");
char c;
while((c=getc(file))!=EOF)
{
   if (c=='\n')
   {
	   count+=1;
       std::cout<<std::endl;
   }
   else                                      
   printf("%c",c);
}

std::cout<<"\nNo Of Lines In the File  :"<<count+1;

fclose(file);
return 0;
}

My program crashes whenever i type this in command line
c:\test Text.txt

Sorry i posted this...i know i am making very serious mistakes in the code....getting confused in c and c++...

No need to reply...thanx anyways

mixing c and c++ file operations is undesirable, but not something that will crash the program. Probably the reason it crashed is that there is no check to see if the file was opened successfully. You can't just assume that. The second reason is that c is defined as char, but getc() returns an int. change c to an int and it should detect EOF correctly.

This article has been dead for over six months. Start a new discussion instead.