0
#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

3
Contributors
3
Replies
4
Views
12 Years
Discussion Span
Last Post by Ancient Dragon
0

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

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.