[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Helpwith writing data to file

Posted on 2004-07-31
6
Medium Priority
?
278 Views
Last Modified: 2010-04-01
Hello Experts,

what exactly does this line of code do;

dataIn.write((char*) (&person),sizeof (person));

I got a code form PSC, and I need to understand exactly what this line of code is doing to be able to work it into my program.
Thing is I hardly know anything about c++ at the moment,and I have been kind of thrown off the deep end with a program am creating, anyway...

QUESTION 2;
This code, I got from PSC, the details written to the file is name, address and telephone number, all are char data types.

I need to incorporate an account number (int datatype) for the program i am working on, but don't know how to make the above line of code write the account number in the file as well.

I am generating the account number in code and placing this in a variable. so How would I make the contents of the account number variable write to the file.

0
Comment
Question by:claracruz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 11686076
It looks like there is a struct variable named "person", and that statement writes the structure to the file.

If you want to add an account number, just add "int accountnumber;" to the structure definition.  Then it will get written to the file.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11686082
I guess 'person' is a structure instance.
write() writes a string of chars to a file, but as person is not a string of chars, you have to "cast" to make write works. Sizeof is a compiler-time function that returns the sizeof any data type, in this case, the size of person structure.  
Notice the ampersand (&) before person, it will give you a "pointer" (memory address) to structure. But as write requires a pointer to a char array, it has been casted to (char *), that is: convert person pointer to char pointer.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11686085
Maybe you can post the person structure definition.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:claracruz
ID: 11686169
I don't really understand any of the above, but here is the code the rest of the code:


 /*--------------------\
| users data entry here |
 \--------------------*/
void inputData(void)
{
      Contact person;
      char continueAdding='Y';
      ofstream dataIn("addressBook.dat",ios::ate);//create binary file
      
      do
      {
            system("CLS");
            cout << "New contact person entry";
            cout << "\n------------------------\n";
            person.add(1);

            dataIn.write((char*) (&person),sizeof (person));
            
            cout << "\nDo you want to add another person? [Y/N] :";
            cin >>continueAdding;
            continueAdding = toupper(continueAdding);
      
      }while (continueAdding!='N');
      
            
      dataIn.close();//close file stream
}


some code from contact.h file;

class Contact {
      char name[25];
      char address[50];
      char telephone[15];
      
      /*the below method is private and used only from within this
       class and transparent to oustide function*/
      bool stringCheck(char*,int);

public:

      void add(int);
      bool compare(char*);
      void display();
      char* returnName();

};

void Contact::add(int flag)
{
      while (!cin.get()) {};

      if (flag==1)
      {
            
            do
            {
                  cout << "\n Enter name of contact person : ";
                  cin.getline(name,1000,'\n');
                  strupr(name);
            }while (!stringCheck(name,1));
                   }
}

char* Customer::returnName(void)
{
      return name;
}

How and where do I get account number into the scheme of things.
Please explain with as little technical speak as possible.


All the data input function is done from contact.h file.
0
 
LVL 22

Accepted Solution

by:
grg99 earned 1500 total points
ID: 11687656
Add the line "char accountnumber[20];" to the class definition for "Contact".

Add right after the get name of contact person:
               cout << "\n Enter account number of contact person : ";
               cin.getline(accountnumber , 1000,'\n');
   
.. but it looks like you're rather low on the C++ familiarity, maybe you should go back and read the textbook?

0
 
LVL 4

Author Comment

by:claracruz
ID: 11691943
Was afraid you were goin to mention the text book thing.... wouldn't be asking if I understood the greek in the textbook. but thanks anyway.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question