473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to read differnet no. of 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: <sms@localhost.localdomain>
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by localhost.localdomain (8.13.5/8.13.5) with ESMTP id l4GEk300002418
for <sms@localhost.localdomain>; 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: <sms@localhost.localdomain>
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by localhost.localdomain (8.13.5/8.13.5) with ESMTP id l4GEkhdo002426
for <sms@localhost.localdomain>; 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: <sms@localhost.localdomain>
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by localhost.localdomain (8.13.5/8.13.5) with ESMTP id l4GEltPQ002436
for <sms@localhost.localdomain>; 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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. main()
  3. {
  4. FILE * kp;
  5. char classmain[100];
  6. int i=0, j=0,x=0;
  7. //flag f=0;
  8. kp=fopen("sms","r");
  9. //fread(classmain,0,10000,kp);
  10.  
  11. if(kp==NULL)
  12. {
  13. printf("unable to open the file");
  14. exit(1);
  15. }
  16. while(!feof(kp))
  17. {
  18. while(fgets(classmain,100,kp) != NULL)
  19. {
  20. //fputs(classmain, stdout);
  21. //printf("%s", classmain);
  22. if (strstr(classmain,"Subject"))
  23. //printf("%s", classmain);
  24. break;
  25. }
  26. while(fgets(classmain,100,kp)!= NULL)
  27. {
  28. if(strcmp(classmain,"")!=0)
  29. break;
  30. }
  31. fgets(classmain,100,kp);
  32. printf("%s",classmain);
  33.  
  34. while(fgets(classmain,100,kp)!=NULL)
  35. {
  36. if(strcmp(classmain,"")==0)
  37. break;
  38. printf("%s",classmain);
  39. }
  40. while(fgets(classmain,100,kp) != NULL)
  41. {
  42. //fputs(classmain, stdout);
  43. //printf("%s", classmain);
  44. if (strstr(classmain,"Subject"))
  45. printf("%s", classmain);
  46. break;
  47. }
  48.  
  49. }//end of 1st while
  50.  
  51. fclose(kp);
  52. }
May 17 '07 #1
5 2198
AdrianH
1,251 Expert 1GB
Please indent your code. I'm not going to do it for you and I won't look at it till it is.
It is just too hard to read.

Adrian
May 17 '07 #2
i have provided it in indented form but when it displays on web there aren't any indentation it is opening in indented form in edit mode kindly check that out
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. main()
  3. {
  4.    FILE * kp;
  5.    char classmain[100];
  6.    int i=0, j=0,x=0;
  7.  
  8.       kp=fopen("sms","r");
  9.       if(kp==NULL)
  10.       {
  11.     printf("unable to open the file");
  12.     exit(1);
  13.       }
  14.  
  15.       while(!feof(kp))
  16.       {
  17.  
  18.     while(fgets(classmain,100,kp) != NULL)
  19.     {
  20.     if (strstr(classmain,"Subject"))
  21.     break;
  22.     }
  23.  
  24.       while(fgets(classmain,100,kp)!= NULL)
  25.       {
  26.       if(strcmp(classmain,"")!=0)
  27.       break;
  28.       }
  29.       fgets(classmain,100,kp);
  30.       printf("%s",classmain);
  31.  
  32.         while(fgets(classmain,100,kp)!=NULL)
  33.         {
  34.         if(strcmp(classmain,"")==0)
  35.         break;
  36.         printf("%s",classmain);
  37.         }
  38.         while(fgets(classmain,100,kp) != NULL)
  39.         {
  40.         if (strstr(classmain,"Subject"))
  41.         printf("%s", classmain);
  42.         break;
  43.         }
  44.  
  45.       }//end of 1st while
  46.  
  47. fclose(kp);
  48. }//end of main
May 18 '07 #3
AdrianH
1,251 Expert 1GB
You have to use [code][/code] tags, with the first tag stating what language it is. For C, use [code=c], for C++ use [code=cpp].

Even in edit I had problems reading it. so I've run it through a programme called indent using -kr switch. This is what I got:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. main()
  3. {
  4.     FILE *kp;
  5.     char classmain[100];
  6.     int i = 0, j = 0, x = 0;
  7.  
  8.     kp = fopen("sms", "r");
  9.     if (kp == NULL) {
  10.         printf("unable to open the file");
  11.         exit(1);
  12.     }
  13.  
  14.     while (!feof(kp)) {
  15.  
  16.         while (fgets(classmain, 100, kp) != NULL) {
  17.             if (strstr(classmain, "Subject"))
  18.                 break;
  19.         }
  20.  
  21.         while (fgets(classmain, 100, kp) != NULL) {
  22.             if (strcmp(classmain, "") != 0)
  23.                 break;
  24.         }
  25.         fgets(classmain, 100, kp);
  26.         printf("%s", classmain);
  27.  
  28.         while (fgets(classmain, 100, kp) != NULL) {
  29.             if (strcmp(classmain, "") == 0)
  30.                 break;
  31.             printf("%s", classmain);
  32.         }
  33.         while (fgets(classmain, 100, kp) != NULL) {
  34.             if (strstr(classmain, "Subject"))
  35.                 printf("%s", classmain);
  36.             break;
  37.         }
  38.  
  39.     }                           //end of 1st while
  40.  
  41.     fclose(kp);
  42. }                               //end of main
I find this much more readable. (except perhaps the colours. We are looking into changing them ;))

Please reply to this message and put comments for each loop telling me what you are trying to accomplish.


Adrian
May 18 '07 #4
i have made changes to the logic
the new code is
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. main()
  3. {
  4. FILE * kp;
  5. char classmain[100];
  6. int i=0, j=0, x=0;
  7. kp=fopen("sms","r");
  8.         //if no file read do the following     
  9.         if(kp==NULL)
  10.     {
  11.     printf("unable to open the file");
  12.     exit(1);
  13.     }
  14.         //else read the file 
  15.     while(!feof(kp))
  16.     {
  17.             //read every string until find Subject in file and then break and move to next while
  18.         while(fgets(classmain,100,kp) != NULL)
  19.         {
  20.         if (strstr(classmain,"Subject"))
  21.         break;
  22.         }
  23.  
  24.         while(fgets(classmain,100,kp)!= NULL)
  25.         {
  26.                 //check for the blank lines writen after subject ignore spaces and read number and next two lines
  27.         if(strcmp(classmain," ")==1)
  28.         break;
  29.         }
  30.         if(feof(kp))
  31.         break;
  32.         printf("%s",classmain);
  33.         printf("%s",(fgets(classmain,100,kp)));
  34.         printf("%s",(fgets(classmain,100,kp)));
  35.     }//end of 1st while
  36. fclose(kp);
  37. }
  38.  
this code is working and giving me desired output but still have logical problems
if(strcmp(classmain," ")==1)
in this condition i am comparing space with the string available in classmain while its not space its value in hex is 0a i checked the contents of my file using command
hexdump -C /sms
with what i should compare the string obtained?
do consider my file of mails with it and run this code and help me improving this thnx
May 19 '07 #5
AdrianH
1,251 Expert 1GB
i have made changes to the logic
the new code is
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. main()
  3. {
  4. FILE * kp;
  5. char classmain[100];
  6. int i=0, j=0, x=0;
  7. kp=fopen("sms","r");
  8.         //if no file read do the following     
  9.         if(kp==NULL)
  10.     {
  11.     printf("unable to open the file");
  12.     exit(1);
  13.     }
  14.         //else read the file 
  15.     while(!feof(kp))
  16.     {
  17.             //read every string until find Subject in file and then break and move to next while
  18.         while(fgets(classmain,100,kp) != NULL)
  19.         {
  20.         if (strstr(classmain,"Subject"))
  21.         break;
  22.         }
  23.  
  24.         while(fgets(classmain,100,kp)!= NULL)
  25.         {
  26.                 //check for the blank lines writen after subject ignore spaces and read number and next two lines
  27.         if(strcmp(classmain," ")==1)
  28.         break;
  29.         }
  30.         if(feof(kp))
  31.         break;
  32.         printf("%s",classmain);
  33.         printf("%s",(fgets(classmain,100,kp)));
  34.         printf("%s",(fgets(classmain,100,kp)));
  35.     }//end of 1st while
  36. fclose(kp);
  37. }
  38.  
this code is working and giving me desired output but still have logical problems
if(strcmp(classmain," ")==1)
in this condition i am comparing space with the string available in classmain while its not space its value in hex is 0a i checked the contents of my file using command
hexdump -C /sms
with what i should compare the string obtained?
do consider my file of mails with it and run this code and help me improving this thnx
Look up strcmp() here. Or for comparing a single char, classmain[0] == ' ' would work too.


Adrian
May 19 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Chuck Anderson | last post by:
Can anyone point me in the right direction? I want to use Php to automate confirmation of someone joining an email list by them replying to an email (so they don't have to have a browser?). I...
8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
2
by: hezhenjie | last post by:
Hi, all: I just need to parse a unicode file, and assume to get data one line by one line. I use _wfopen(), fgetws(), wcslen(), wcsstr(), making it work normally on Windows platform. However,...
2
by: VM | last post by:
Is it possible to open an ascii file, read the first 499 lines, overwrite line 500 with the new string, and close the file? All the lines before and after line 500 will be untouched. Thanks.
24
by: rudranee | last post by:
hi there, can anyone tell me how to lines from a file which are odd numbered i.e. 1st,3rd,5th...lines. i tried incrementing file pointer by 2 (fp=fp+2) but it does'nt work Can someone give me...
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
16
by: mazwolfe | last post by:
Someone recently asked about reading lines. I had this code written some time ago (part of a BASIC-style interpreter based on H. Shildts in Art of C) to read a file with the lines ended in any...
28
by: tlpell | last post by:
Hey, read some tips/pointers on PHP.net but can't seem to solve this problem. I have a php page that reads the contents of a file and then displays the last XX lines of the file. Problem is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.