473,789 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to read differnet no. of lines from a file using c in linux?

6 New Member
the file which i have to read is:
From sms@localhost.l ocaldomain Wed May 16 10:46:03 2007
Return-Path: <sms@localhost. localdomain>
Received: from localhost.local domain (localhost.loca ldomain [127.0.0.1])
by localhost.local domain (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.local domain (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.l ocaldomain
Message-Id: <200705161446.l 4GEk3H3002417@l ocalhost.locald omain>
To: sms@localhost.l ocaldomain
Subject: mail 1

030044444
1st mail to be sent
by sms

From sms@localhost.l ocaldomain Wed May 16 10:46:43 2007
Return-Path: <sms@localhost. localdomain>
Received: from localhost.local domain (localhost.loca ldomain [127.0.0.1])
by localhost.local domain (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.local domain (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.l ocaldomain
Message-Id: <200705161446.l 4GEkhRa002425@l ocalhost.locald omain>
To: sms@localhost.l ocaldomain
Subject: 2nd mail


09888766
2nd mail to be sent
for df

From sms@localhost.l ocaldomain Wed May 16 10:47:55 2007
Return-Path: <sms@localhost. localdomain>
Received: from localhost.local domain (localhost.loca ldomain [127.0.0.1])
by localhost.local domain (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.local domain (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.l ocaldomain
Message-Id: <200705161447.l 4GEltF0002435@l ocalhost.locald omain>
To: sms@localhost.l ocaldomain
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 2225
AdrianH
1,251 Recognized Expert Top Contributor
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
shermeenz
6 New Member
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 Recognized Expert Top Contributor
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
shermeenz
6 New Member
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(class main," ")==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 Recognized Expert Top Contributor
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(class main," ")==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
8802
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 will probably use a hyperlink with a unique ID, but I also want to know how to go about reading from a mailbox with Php so I can use an email reply method, too. I'm having trouble finding any kind of tutorial. From the bit of searching I've done,...
8
8361
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 the records are sorted by row numbers. (I had to split the fields to different sheets because Excel has a limit of 256 fields in each sheet) My sheets are quite large (~55,000 rows and 200 columns each) and I'll have to repeat this action many...
2
9811
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, when migrate it to Linux platform, issue occurs. Linux only has fopen() function, and fgetws() could not correctly get lines, in fact, it gets nothing.
2
6800
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
6157
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 the code please.
6
3554
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 I'm avoiding the use of fscanf and fgets to read in lines. I don't want to have to specify a temporary char* buffer to read in each line, and then have to concern myself with the (remote) possibility of overflows or with increasing the buffer...
9
7388
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 to read was maindata.xml . I cannot however seem to use the gzip module correctly. Have tried the program 2 ways for no success, any ideas would be appreciated. Attempt 1 #!/usr/bin/python
16
2947
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 format: Microsoft-style CR/LF pair, Unix-style NL, or Mac-style CR. It also allows for EOF that does not follow a blank line. I thought this would make text-file sharing a bit easier. Here it is: /* Load a file, normalizing newlines to *nix...
28
3280
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 this...whenever the file gets larger that ~5MB, the page just displays nothing, as though a timeout has occurred but I get no error. At 4.8MB (last confirmed size)...the function still works. Any ideas what code below is lacking?? <? $handle=...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.