Thread: How to write data from file to structure, This case is different.

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Unhappy How to write data from file to structure, This case is different.

    ************************************************** ********
    Code:
    #include<iostream>
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    struct address{
           int h,st;
           char city[30],country[25];
           };
    
    struct info{
           char name[20];
           char fname[20];
           char desig[20];
           struct address add;
           }; 
           
    int main(){
        struct info zia;
        FILE*fp;
        fp=fopen("new.txt","w");
        if(fp!=NULL){
         cout<<"Name           : ";
      
         gets(zia.name);
         cout<<"Father Name    : ";
    
          gets(zia.fname);
         cout<<"Designation    : ";
       
          gets(zia.desig);
         cout<<"House #        : ";
         cin>>zia.add.h;
         cout<<"Street #       : ";
         cin>>zia.add.st;
         fflush(stdin);   
         cout<<"City           : ";
    
         gets(zia.add.city);
         cout<<"Country        : ";
    
          gets(zia.add.country);
      
         fprintf(fp,"%s %s %s %d %d %s %s",zia.name,zia.fname,zia.desig,zia.add.h,zia.add.st,zia.add.city,zia.add.country);           
                     }
        else{
             cout<<"File can't be Created";
             }
        
        getche();
        return 0;
        }
    ************************************************

    File Create with the Output. Ok the word with in the parenthesis are just to make clear that it was where in the structure while writing to file.

    ************************************************
    Tanzeelur Rehman (name) Ubaidullah (fname) Software Developer (desig)1 (add,h) 3 (add, st) Islamabad (add,city) Pakistan (add,country)
    ************************************************

    now i want to write these from file to structure, The issue is that with in the name i have spaces, in designation i have spaces too, I want to read the complete name in one string and complete designation in one string.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First, decide if you want C...
    Code:
    fprintf(fp,"%s %s %s %d %d %s %s",zia.name,zia.fname,zia.desig,zia.add.h,zia.add.st,zia.add.city,zia.add.country);           
                     }
        else{
    Or C++:
    Code:
             cout<<"File can't be Created";

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Hahaha

    It is good that you have pointed out, That i am combining both c and C++,

    Sorry

    I need solution in C

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    SourceForge.net: Fflush - cpwiki
    SourceForge.net: Gets - cpwiki

    As for the spaces, then you need to write the file using something other than spaces as the field separator.

    Say
    Code:
    fprintf(fp,"%s,%s,%s,%d,%d,%s,%s\n",
        zia.name,zia.fname,zia.desig,
        zia.add.h,zia.add.st,zia.add.city,zia.add.country);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Thank you Saleem,


    Your solution clicked my mind, I have to build custom functions for that, any other solution, if there persists any library function

    regards
    Tanzeelur Rehman

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just read and write in binary mode, and write/read the whole structure at once?


    Qzuah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Thumbs up

    Quzah
    Thank you, you are right ,

    Regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Packet Container Class
    By ChaoticXSinZ in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2010, 12:07 AM
  2. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM

Tags for this Thread