1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'C' started by madhugupta, Feb 29, 2012.

  1. madhugupta

    madhugupta New Member

    Joined:
    Feb 29, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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);
    }
     
    Last edited by a moderator: Feb 29, 2012
  3. madhugupta

    madhugupta New Member

    Joined:
    Feb 29, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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.
     
  5. madhugupta

    madhugupta New Member

    Joined:
    Feb 29, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
    Last edited by a moderator: Mar 1, 2012
  6. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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
     
  7. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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);
    }
     
    Last edited by a moderator: Mar 1, 2012
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
  9. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0

    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
     

Share This Page