Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how to read lines from a file using c in linux?

the file which i have to read is:
From sms@localhost.localdomain Wed May 16 10:46:03 2007
Return-Path:
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by localhost.localdomain (8.13.5/8.13.5) with ESMTP id l4GEk300002418
for ; Wed, 16 May 2007 10:46:03 -0400
Received: (from sms@localhost)
by localhost.localdomain (8.13.5/8.13.5/Submit) id l4GEk3H3002417
for sms; Wed, 16 May 2007 10:46:03 -0400
Date: Wed, 16 May 2007 10:46:03 -0400
From: sms@localhost.localdomain
Message-Id: <200705161446.l4GEk3H3002417@localhost.localdomain>
To: sms@localhost.localdomain
Subject: mail 1

030044444
1st mail to be sent
by sms

From sms@localhost.localdomain Wed May 16 10:46:43 2007
Return-Path:
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by localhost.localdomain (8.13.5/8.13.5) with ESMTP id l4GEkhdo002426
for ; Wed, 16 May 2007 10:46:43 -0400
Received: (from sms@localhost)
by localhost.localdomain (8.13.5/8.13.5/Submit) id l4GEkhRa002425
for sms; Wed, 16 May 2007 10:46:43 -0400
Date: Wed, 16 May 2007 10:46:43 -0400
From: sms@localhost.localdomain
Message-Id: <200705161446.l4GEkhRa002425@localhost.localdomain>
To: sms@localhost.localdomain
Subject: 2nd mail


09888766
2nd mail to be sent
for df

From sms@localhost.localdomain Wed May 16 10:47:55 2007
Return-Path:
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by localhost.localdomain (8.13.5/8.13.5) with ESMTP id l4GEltPQ002436
for ; Wed, 16 May 2007 10:47:55 -0400
Received: (from sms@localhost)
by localhost.localdomain (8.13.5/8.13.5/Submit) id l4GEltF0002435
for sms; Wed, 16 May 2007 10:47:55 -0400
Date: Wed, 16 May 2007 10:47:55 -0400
From: sms@localhost.localdomain
Message-Id: <200705161447.l4GEltF0002435@localhost.localdomain>
To: sms@localhost.localdomain
Subject: 3rd mail



099888776
3rd mail for user
gsg

from this file i have to read lines below the "subject:" from there i have to read number and then the written e-mail right now i am able to just red the 1st number and mail below it i don't know what to do to read the 2nd and 3rd or any no. of mails from this file.
the code i have written is below will be really greatful for assisstance

#include
main()
{
FILE * kp;
char classmain[100];
int i=0, j=0,x=0;

kp=fopen("sms","r");
if(kp==NULL)
{
printf("unable to open the file");
exit(1);
}

while(!feof(kp))
{

while(fgets(classmain,100,kp) != NULL)
{
if (strstr(classmain,"Subject"))
break;
}

while(fgets(classmain,100,kp)!= NULL)
{
if(strcmp(classmain,"")!=0)
break;
}
fgets(classmain,100,kp);
printf("%s",classmain);

while(fgets(classmain,100,kp)!=NULL)
{
if(strcmp(classmain,"")==0)
break;
printf("%s",classmain);
}
while(fgets(classmain,100,kp) != NULL)
{
if (strstr(classmain,"Subject"))
printf("%s", classmain);
break;
}

}//end of 1st while

fclose(kp);
}//end of main
Sign In or Register to comment.