You are here:

C/To read a text file line by line

Advertisement


Question
QUESTION: Hi Prince:

My question is I want to write a C program to read a text file line by line and and copy the line in a new file if and only if certain strings are found in the line. Let me give you an example, following is a part of the text file for which i want to write a code:

B 00 00 AC 48013 ETX   4911   10100301 0007 0001 001    S  99081300 99081301   149.8382 -1235.2500    137.2 6.098   350 36.07  28  17  28  27 24  0 7 7896 42101    0.11098          AG0007G BOILER1    STACK1     San Miguel P SAN MIGUEL ELECTRIC COOPERATIVE IN ATASCOSA    CHRISTINE       CO          A EPA    ST 06/13/2000 ARP
B 00 00 AC 48013 ETX   4911   10100301 0007 0001 001    S  99081301 99081302   149.8382 -1235.2500    137.2 6.098   350 36.07  28  17  28  27 24  0 7 7896 42101    0.11489          AG0007G BOILER1    STACK1     San Miguel P SAN MIGUEL ELECTRIC COOPERATIVE IN ATASCOSA    CHRISTINE       CO          A EPA    ST 06/13/2000 ARP
B 00 00 AC 48013 ETX   4911   10100301 0007 0001 001    S  99081302 99081303   149.8382 -1235.2500    137.2 6.098   350 36.07  28  17  28  27 24  0 7 7896 42101    0.11559          AG0007G BOILER1    STACK1     San Miguel P SAN MIGUEL ELECTRIC COOPERATIVE IN ATASCOSA    CHRISTINE       CO          A EPA    ST 06/13/2000 ARP
B 00 00 AC 48013 ETX   4911   10100301 0007 0001 001    S  99081307 99081308   149.8382 -1235.2500    137.2 6.098   350 36.07  28  17  28  27 24  0 7 7896 42603    0.40408          AG0007G BOILER1    STACK1     San Miguel P SAN MIGUEL ELECTRIC COOPERATIVE IN ATASCOSA    CHRISTINE       NOx          A EPA    ST 06/13/2000 ARP
B 00 00 AC 48013 ETX   4911   10100301 0007 0001 001    S  99081308 99081309   149.8382 -1235.2500    137.2 6.098   350 36.07  28  17  28  27 24  0 7 7896 42603    0.83038          AG0007G BOILER1    STACK1     San Miguel P SAN MIGUEL ELECTRIC COOPERATIVE IN ATASCOSA    CHRISTINE       NOx          A EPA    ST 06/13/2000 ARP
B 00 00 AC 48113 DFW   4911   10100601 0007 0004 004    S  99081510 99081511   311.4781  -764.2720     18.3 2.134   402 4.573   6  35  48  11 24  0 7 8736 42603    0.00000          DB0384A BOILER-BW5 STACK-BW5  C E Newman G GARLAND MUNICIPAL POWER AND LIGHT  DALLAS      GARLAND         NOx          A EPA    ST 06/14/2000 ARP
B 00 00 AC 48113 DFW   4911   10100601 0007 0004 004    S  99081511 99081512   311.4781  -764.2720     18.3 2.134   402 4.573   6  35  48  11 24  0 7 8736 42603    0.00000          DB0384A BOILER-BW5 STACK-BW5  C E Newman G GARLAND MUNICIPAL POWER AND LIGHT  DALLAS      GARLAND         NOx          A EPA    ST 06/14/2000 ARP
B 00 00 AC 48113 DFW   4911   10100601 0007 0004 004    S  99081512 99081513   311.4781  -764.2720     18.3 2.134   402 4.573   6  35  48  11 24  0 7 8736 42603    0.00000          DB0384A BOILER-BW5 STACK-BW5  C E Newman G GARLAND MUNICIPAL POWER AND LIGHT  DALLAS      GARLAND         NOx          A EPA    ST 06/14/2000 ARP
B 00 00 AC 48113 DFW   4911   10100601 0007 0004 004    S  99081513 99081514   311.4781  -764.2720     18.3 2.134   402 4.573   6  35  48  11 24  0 7 8736 42603    0.00000          DB0384A BOILER-BW5 STACK-BW5  C E Newman G GARLAND MUNICIPAL POWER AND LIGHT  DALLAS      GARLAND         NOx          A EPA    ST 06/14/2000 ARP
B 00 00 AC 48113 DFW   4911   10100601 0009 0002 006    S  99082016 99082017   318.9672  -772.3906     47.6 6.098   552 21.34  17  22  37  24 24  0 7 8736 43104    0.01316          DB0249H LH-B1      LH-S1      Lake Hubba G TXU GENERATING COMPANY LP          DALLAS      DALLAS          VOC          A EPA    ST 09/08/2000 ARP
B 00 00 AC 48113 DFW   4911   10100601 0009 0002 006    S  99082017 99082018   318.9672  -772.3906     47.6 6.098   552 21.34  17  22  37  24 24  0 7 8736 43104    0.01274          DB0249H LH-B1      LH-S1      Lake Hubba G TXU GENERATING COMPANY LP          DALLAS      DALLAS          VOC          A EPA    ST 09/08/2000 ARP
B 00 00 AC 48113 DFW   4911   10100601 0009 0002 006    S  99082018 99082019   318.9672  -772.3906     47.6 6.098   552 21.34  17  22  37  24 24  0 7 8736 43104    0.01273          DB0249H LH-B1      LH-S1      Lake Hubba G TXU GENERATING COMPANY LP          DALLAS      DALLAS          VOC          A EPA    ST 09/08/2000 ARP
B 00 00 AC 48113 DFW   4911   10100601 0011 0006 006    S  99081905 99081906   302.2812  -779.6396     45.4 2.744   394 8.537  13  16  57  14 24  0 7 8736 42101    0.00308          DB0253Q PD-B1      PD-S1A     Parkdale   G TXU GENERATING COMPANY LP          DALLAS      DALLAS          CO          E EPA    ST 06/13/2000 ARP

The text in the file has a particular format which needs to be maintained in the output file too. In the above file I want to copy the lines which satisfy the conditions that it has DALLAS AND NOx OR VOC in the line.

I am not sure if I was able to explain it to you properly. I will highly appreciate your assistance in this matter.

Thanks
Sincerely
Neelesh

ANSWER: Hi dear Neelish !

 Im sure this file is organized in terms of records ! and i hope all the records have the same format and fields ! Here only information i need is the actual record structure and the fields associated with the record !

here let me explain you what actually i need
assume that  the text file be in the following format given below

filler  28
Turner  51
Joe Rex 35

in this case this file  is organized in terms of records with same size and with same number of fields the actual structure of the record may be implemented in C using the following construct
struct empl
{
   char name[20];
   in age;
 };

so the only thing i need is the record structure of your database , then it will be of course very easy for me to answer you !

but anyhow i will give some suggestions sure that will  meet your expectation !

For our discussion let me assume a sample record structure to explain you how to extract the essential information

/* sample record with number of fields */
struct record
{
  char name[20];
  char city_name[30];
  char desig[3];
  .
  .
  .
  .
}rec; /* structure variable declared here */


/* expected structure with limited fields */
struct extracted
{
 char city_name[30];
 char desig[4];
}newrec;
/* make sure that the new struct should contain only the required fields */

Note : Assume that number of records stored in a data file consecutively by following the structure described above , our objective is to retrieve the records that contain city_name = "DALLAS" , and desig = "VOC"

here im presenting you the excerpt to retrieve the expected records

rewind(fp); /* reset the pointer to the beginning of the file */

while( fread(&rec , sizeof(rec) , 1 , fp))
{
  if( strcmp( rec.city_name  , "DALLAS") && strcmp( rec.desig , "VOC"))
   {

       /* collect the retrieved details in a new record */
       strcpy(newrec.city_name , rec.city_name);
       strcpy(nrerec.desig , rec.desig);
       /* Then write the contents of the new records in the  new file , assume that the new file handle is fp1  */

       fwrite(&newrec , sizeof(newrec) , 1 , fp1);
}

      
That after the complete execution of the while() loop given above , im sure that all the records are extracted from the pool of records from file1 ( handled by file handle fp ) will be written into the new file ( handled by file handle fp1 )

I hope this information is enough for you to understand better ,If you wish to have any more discussion , then please feel free to as a follow up with the required information !
 
Thanks and Regards !
Prince M. Premnath

---------- FOLLOW-UP ----------

QUESTION: Hi Prince:

Thank you for a prompt and comprehensive reply. It was very helpful. I tried to use the code but it is giving me some error. For testing the code i just considered a small part of my original file shown below:

B 00 00 AC 48013 ETX   4911
B 00 00 AC 48013 ETX   4911
B 00 00 AC 48013 DFW   4911
B 00 00 AC 48013 ETX   4911
B 00 00 AC 48013 ETX   4911
B 00 00 AC 48113 DFW   4911

Following is the code which i have written for it
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <conio.h>
   int main ()
   {
     FILE * fp;
     FILE * fp1;
     fp = fopen ("structex.txt","r");
     fp1 = fopen("nee1.txt","w");
     struct record
     {
      char col1[1];
      int col2;
      int col3;
      char col4[2];
      int col5;
      char col6[5];
      int col7;
     }trial; /* structure variable declared here */

     struct extracted
     {
      char col6[5];
     } newtrial;
     
     rewind(fp); /* reset the pointer to the beginning of the file */

     while( fread(&trial , sizeof(trial) , 1 , fp))
     {
      if( strcmp( trial.col6  , "DFW"))
      {

      /* collect the retrieved details in a new record */
      strcpy(newtrial.col6 , trial.col6);
      /*strcpy(nrerec.desig , rec.desig);*/
      /* Then write the contents of the new records in the  new file , assume that the new file handle is fp1  */

      fwrite(&newtrial , sizeof(newtrial) , 1 , fp1);
      }
      }
}

The code is not giving me the desired output, kindly help me in identifying the error in the code. In the above example I am trying to copy records having "DFW".
I appreciate your time and patience.

Thank you
Sincerely,
Neelesh
ANSWER: Dear neelish !

Here im presenting the complete code  sure you can learn and understand better !

#include<stdio.h>
#include<string.h>


struct sample
{
  char name[30];
  int age;
  char desig[4];
}rec;

struct extracted
{
  char name[30];
  char desig[4];
}newrec;

void main()
{

FILE* fp1 , *fp2;
int  opt;

/* fp1 = fopen("file1.txt" , "w");
do{
    printf("Enter name :");
    scanf("%s" , rec.name);

    printf("Enter age ");
    scanf("%d" , &rec.age);
    printf("Enter desig :");
    scanf("%s" , rec.desig);
    fwrite(&rec , sizeof(rec) , 1 , fp1);
    printf("Wish to enter another rec <1/2> ?:");
    scanf("%d" , &opt);
} while( opt != 2);
fclose(fp1);


fp1 = fopen("file1.txt" , "r");
rewind(fp1);
while( fread( &rec , sizeof(rec) , 1 , fp1))
{
  printf(" Name = %s\n " , rec.name);
  printf(" Age = %d\n" , rec.age);
  printf(" Designation = %s" , rec.desig);
}


*/
fp1 = fopen("file1.txt" , "r");
fp2 = fopen("file2.txt" , "w");

rewind(fp1);
printf(" EXTRACTED RECORDS AND FILEDS \n");
while( fread( &rec , sizeof(rec) , 1 , fp1))
{
  if( strcmp(rec.desig  , "VGA") == 0)
  {
     strcpy( newrec.name , rec.name);
     strcpy( newrec.desig , rec.desig);
     fwrite( &newrec , sizeof(newrec) , 1 , fp2);
  }
}
fclose(fp1);
fclose(fp2);

fp2 = fopen("file2.txt" , "r");
rewind(fp2);
while( fread ( &newrec , sizeof(newrec) , 1 , fp2) )
{
  printf(" Name = %s\n" , newrec.name);
  printf(" Desig = %s\n" , newrec.desig);
}
fclose(fp2);
}

Note: i have commented some lines of text in that program , that code will actually perform the INPUT opration of te file file1.txt ( make sure don't just type some data's in the file file1.txt , sure it will show some un expected results , better use the same program to fetch records by removing the comments

Im sure using this sample program you can develop your own program to extract the required information form your data base


Thanks and Regards !
Prince M. Premnath

---------- FOLLOW-UP ----------

QUESTION: Hi Prince:

Thanks, but still I am a bit confused, because my actual file has got 5000 lines and i cannot input for each of them so is there a way i can automate the input process?

Thanks
Sincerely,
Neelesh

Answer
Dear Neelish !

 Yep you are right , actually i gave that code just for your understanding!  im sure you need not to follow the same methodology for your own database ( regarding 5000 lines of code )

You have to be concise about how these data's are actually stored ( or packed in your file )
Here let me present you few suggestions how to develop application program to manipulate your data base !

* All the records stored in your database satisfy the same length and size
* You have to determine what is the data type ( say integer , float or etc .. ) of each attributes , if its a character string then find the length of the string
* If you are not the creator of the original database , then contact the person who created the actual  database ,its the easiest way to determine the characteristics of both the database and records stored in it!
* Based on this information create a user defined structure   using the struct construct available in c ( make sure the structure you going to construct with the struct construct should obsoletely fix with the records stored in the data base ) I'm sure you have to burn your mid night oil to accomplish this task !

* That after creating the structure , then develop your application program by enforcing the constraints over records retrieval!

tune up your program using TRIAL AND ERROR METHOD sure it will work fine !

ALL THE BEST

Thanks and Regard !
Prince M. Premnath

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.