LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2004, 02:38 AM   #1
xemous
Member
 
Registered: Jun 2004
Posts: 80

Rep: Reputation: 15
writing a strcutures to a file


here is my current code
it writes some variables from the record struct to a file
but i trying to find a way to write just the entire struct to the file, after the user has entered in his detials, the file dosetn have to be readable by human, just by machine


#include <iostream>
#include <fstream>
#include <stdlib.h>
//using namespace std;
using namespace std;
using std::cout;
using std::cin;
int main()
{

struct record
{
char fname[15];
char lname[20];
int cage;
char acctype[1];
int nbalance;
};

struct record rec;
cout << "you will need to enter in some information, below is a table, fill it out to proceed\n";
cout << "first name: ";
cin >> rec.fname;
cout << "\nlast name: ";
cin >> rec.lname;
cout << "\nyour age: ";
cin >> rec.cage;
cout << "\nnow we need to know your account type\n";
cout << "enter s for savings, c for cheque, i for investment: ";
cin >> rec.acctype;
cout << "\nSet your initial balance: ";
cin >> rec.nbalance;

ofstream SaveFile("record.txt");
SaveFile << "firstname: " << rec.fname;
SaveFile << " secound name: " << rec.lname;
SaveFile << " age: " << rec.cage;
SaveFile << " account type: " << rec.acctype;
SaveFile << " initial balance: " << rec.nbalance;
SaveFile.close();
 
Old 09-03-2004, 02:53 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I'm not sure about C++, but in C it would look like:
Code:
struct record rec;
FILE *of = fopen("record.txt", "wb");
fwrite(&rec, sizeof(rec), 1, of);
fclose(of);
Hope that helps you in some way
 
Old 09-03-2004, 02:57 AM   #3
xemous
Member
 
Registered: Jun 2004
Posts: 80

Original Poster
Rep: Reputation: 15
thanks alot mate,
it worked
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
writing output to file Hockeyfan Linux - General 3 10-24-2005 08:02 PM
PHP file writing benrose111488 Programming 6 07-01-2005 03:57 PM
Help reading and writing to file xiste Programming 1 04-15-2005 12:43 AM
writing to file ej25 Programming 17 11-19-2004 11:23 AM
writing a batch file ???? Micro Linux - General 3 01-09-2003 11:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration