Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Need the parser function for writing data from a structure to a text file. (http://www.go4expert.com/forums/parser-function-writing-data-structure-t27917/)

madhugupta 29Feb2012 17:03

Need the parser function for writing data from a structure to a text file.
 
Hello,
My requirement is to write the data from a structure to a text file using some c parser function.
Structure contains int,char,and structure.i mean structure inside structure.
Could you please guide me to write the function or someone please provide the basic function for me.

Thanks in advance.

Regards
Madhu

Chong 29Feb2012 20:10

Re: Need the parser function for writing data from a structure to a text file.
 
Hi M
I have written a simple or sample program for you as below. I hope it helps you.
Best regards
Chong
++++++++++++++++++++++++++++
Code:

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

struct dummy {
        char address[100];
};

struct test {
        char name[20];
        int age;
        struct dummy home;
};

int main()
{
        struct test student[2];

        strcpy(student[0].name, "Chong Kim");
        student[0].age = 20;
        strcpy(student[0].home.address,"Funcy road");


        strcpy(student[1].name,"Christina Kim");
        student[1].age = 30;
        strcpy(student[1].home.address,"Tadcaster road");

        FILE *fw,*fr;

        if ((fw=fopen("test.txt","rw"))==NULL){
                printf("File open for reading or writing failed\n");
                return -1;
        }

        fwrite(student,sizeof(struct test),2,fw);

        fseek(fw,0,SEEK_SET);
        fread(student,sizeof(struct test),2,fw);

        printf("%s is %d old\n",student[0].name,student[0].age);
        printf("address:%s\n",student[0].home.address);
        printf("%s is %d old\n",student[1].name,student[1].age);
        printf("address:%s\n",student[1].home.address);


        fclose(fw);
}


madhugupta 1Mar2012 09:45

Re: Need the parser function for writing data from a structure to a text file.
 
Thanks Chong.
Actually it is not writing in the file.
One more thing also the structure containing one 2 dimensional array also.
Could you please help me.

New to programming.

Chong 1Mar2012 11:02

Re: Need the parser function for writing data from a structure to a text file.
 
Hi M
I am not sure what you are asking. You need to explain your project specifications more clearly. If you have already started programming it, it will be better to show it here. Show me some cording of yours so far.
Best regards
Chong
P.S.
You declare two dimentional array in the same way as you do with one dimentional array.

madhugupta 1Mar2012 13:51

Re: Need the parser function for writing data from a structure to a text file.
 
Code:

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

struct inside {
        int field ;
};

struct initialdata {
        int timeout_t3_init;
        char eirp;
        int bsid[6];
        int priority[3][2];
        struct inside neighbor[2];
};

int main()
{
        struct initialdata ptr[2];

        ptr [0].timeout_t3_init =10;

        strcpy(ptr[0].eirp, "hello");
        ptr[0].bs_id[0]=1;
        ??


        FILE *fw,*fr;

        if ((fw=fopen("test.txt","rw"))==NULL){
                printf("File open for reading or writing failed\n");
                return -1;
        }

        fwrite(ptr,sizeof(struct initialdata),1,fw);

        fseek(fw,0,SEEK_SET);
        fread(ptr,sizeof(struct initialdata),1,fw);



        fclose(fw);
}

My intention is when i will open the test.txt file i need to see the parameter and coresponding value written in the txt file.

Chong 1Mar2012 14:06

Re: Need the parser function for writing data from a structure to a text file.
 
Hi M
The file created in your program is a text file (c.f. you may add 't' to make sure that it is a text file, i.e. "rw+t"). If you open the file to see what are in it, you should be able to see character strings but not integers which you have written to the file. The integers are not chacracters and will look like garbage in your file.
Best regards
Chong

Chong 1Mar2012 17:09

Re: Need the parser function for writing data from a structure to a text file.
 
Hi M
I have modified my sample program a bit to add how to deal with errors.
Best regards
Chong
++++++++++++++++++++++++
Code:

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

struct dummy {
        char address[100];
};

struct test {
        char name[20];
        int age;
        struct dummy home;
};

int main()
{
        struct test student[2];
        struct test tmp;

        strcpy(student[0].name, "Chong Kim");
        student[0].age = 20;
        strcpy(student[0].home.address,"Funcy road");


        strcpy(student[1].name,"Christina Kim");
        student[1].age = 30;
        strcpy(student[1].home.address,"Tadcaster road");

        FILE *fw,*fr;

        if ((fw=fopen("test.txt","w+"))==NULL){
                printf("File open for writing failed\n");
                return -1;
        }

        fwrite(student,sizeof(struct test),2,fw);

        fseek(fw,0,SEEK_SET);

        while(1){
                fread(&tmp,sizeof(struct test),1,fw);
                if (feof(fw)) break;
                else if (ferror(fw)){
                        printf("fread() failed\n");
                        return -1;
                }

                printf("%s is %d old\n",tmp.name,tmp.age);
                printf("address:%s\n",tmp.home.address);

        }

        fclose(fw);
}


shabbir 1Mar2012 17:19

Re: Need the parser function for writing data from a structure to a text file.
 
Chong, please use code blocks when you post code snippets in post. See http://www.go4expert.com/misc.php?do=bbcode#code

Chong 11Mar2012 12:32

Re: Need the parser function for writing data from a structure to a text file.
 
Quote:

Originally Posted by madhugupta (Post 92915)
Code:

#include <stdio.h>
#include <string.h>
 
struct inside {
        int field ;
};
 
struct initialdata {
        int timeout_t3_init;
        char eirp;
        int bsid[6];
        int priority[3][2];
        struct inside neighbor[2];
};
 
int main()
{
        struct initialdata ptr[2];
 
        ptr [0].timeout_t3_init =10;
 
        strcpy(ptr[0].eirp, "hello");
        ptr[0].bs_id[0]=1;
        ??
 
 
        FILE *fw,*fr;
 
        if ((fw=fopen("test.txt","rw"))==NULL){
                printf("File open for reading or writing failed\n");
                return -1;
        }
 
        fwrite(ptr,sizeof(struct initialdata),1,fw);
 
        fseek(fw,0,SEEK_SET);
        fread(ptr,sizeof(struct initialdata),1,fw);
 
 
 
        fclose(fw);
}

My intention is when i will open the test.txt file i need to see the parameter and coresponding value written in the txt file.


Hi M
there is a way to ouput into a text file and you can see parameters and valuesin the text file. use sscanf() and its opposite sprintf(). Getline(file,string) where string gets broken to fields with sscanf(line,"%s %d %d",&field1,&field2,&fields) and sprintf(line..) does exatly the opposite. outfile.write(line,strlen(line)) will do the work.

Best regards
Chong


All times are GMT +5.5. The time now is 00:05.