Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

to read large text files with multiple strings in ANSI C

$
0
0
Hello,

#include <stdlib.h>
#include <stdio.h>
#include <string.h> /* for strlen(...) */

/** =>Read in data VDe. from  20460000.txt line by line
    =>Store each line in a VDe_String (Special variable of typedef for char strings
    =>Create a new dynamically allocated char_array and read the lines into it
      and dsiplay **/


	typedef char* VDe_String; /* VDe_String is type 'pointer to char' ... */

	/* returns NULL for EOF ... or a pointer to a NEW VDe_String ... */

	VDe_String readLine(FILE* f)

	{
	    static int ch = 0; /* static in ch ... to remember if a previous call set EOF */
	    static int line_Count = 0; /* if file empty then control comes to line_Count */
	    int Buffer_Size = 256; /* adjust 255 for data,cab be different */
	    int i = 0;   /* index parameter need in main*/

	    if(ch == EOF) {ch=0; line_Count=0; return NULL;} /* reset for read again */

	    VDe_String line = (VDe_String) calloc(Buffer_Size, sizeof(char));

	    while ((ch = fgetc(f)) != EOF && ch != '\n') /* if static ch not EOF & ch not line feed both true */

	    {
	        if(i >= Buffer_Size)
	        {
	            Buffer_Size += 256; /* adjust 256 for data */

	            line = (VDe_String) realloc(line, Buffer_Size*sizeof(char));
	        }

	        line[i++] = ch;
	    }

	    /* In case if file empty! ...*/

	    if(line_Count++ == 0 && ch == EOF) {free(line); return NULL;}

	    line[i] = '\0'; /* confirm terminal of line by '\0' */

	    return realloc(line, i+1); /* total len =  last index i ... + 1 more */
	}

	int main()

	{
	    VDe_String cs, *cs_arry;

	    int i=0, j=0; /* index for pointer */

	    FILE * fp;

	    fp = fopen("D:\\sspp.txt", "r");

	    if( fp == NULL )
	    {
	        printf("error opening file. press 'Enter' key to exit ... ");

	        getchar();

	        return 1;
	    }

	    puts("counting lines of text and showing each line ...");

	    while( (cs = readLine( fp )) )

	    {
	        ++i;
	        printf("%d: %s\n", i, cs);
	        free(cs);
	    }

	    rewind( fp );

	    /* reserve sufficent memory for an array of pointers to VDe_String ... */

	    cs_arry = (VDe_String*) malloc( i*sizeof(VDe_String) );

	    /* read the file into the array of VDe_String ... */

	    while( (cs = readLine( fp )) )

	        cs_arry[j++] = cs;

	    fclose(fp);

      	    if( i != j )

	        printf("\nError reading file i=%d, but j=%d\n", i, j );


	    puts("displaying array of strings...");

	    //break; // would not work

	    for( j=0; j<i; ++j)
	    {
	        printf("%d: %s\n", j+1, cs_arry[j]);

	        free( cs_arry[j] ); /* free memory reserved for each VDe_String when done with it */
	    }
	    free( cs_arry ); /* free memory reservered for the array when done with it */

	    printf("Press 'Enter' to continue ...");

	    getchar();

	    return 0;
}





I want to read very large text file(originally was .xml tags file i convert in text) with help of ANSI C which is about 36MB,in this text files i have two different kinds of strings(records)"Vde" & "IDe" kind with characters,digits and also special character etc,both type of strings comes multiple time in text file and they are different from each other because of few specific parameters,within those string there is also some informations.....my aim is just to read main "Heading" string with informations which comes under those strings.I like to attach example file but i can't bcoz it's not working(like server error...too big)....so i just write as example how string looks like and what come after string as following

Structure of file like to be read:
----------------------------------
heading string like

<measurementSiteIdentification>VDe-54-01-A1-MBGX1a</measurementSiteIdentification>

and after that string there are hundreds of other strings like

<measurementSpecificCharacteristics index="202">
<measurementSpecificCharacteristics>
<period>84000.0</period> etc....

my aim is just to read string in heading and then all informations which comes within those strings....for ref see my C codes!.

Error:
But this give me output on consol which just reading whole files and left or cut many of strings(records)etc and also infinitely long outputs ....i just start to learn reading files etc....therefore If some one have any idea or example then share with,this is not a home work or project etc!....thanks

Regards

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>