Read/Write data to file

read/write data to file

Page 1 of 1

3 Replies - 1545 Views - Last Post: 03 December 2009 - 04:50 PM Rate Topic: -----

#1 virusinfektion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 08

Read/Write data to file

Posted 03 December 2009 - 04:08 PM

Hello, I am learning how to read & write data to a file. The problem is when i go to open up the file the information I entered doesnt appear like it should, but it appears in the file i saved.

#include <iostream>
#include <fstream>

using namespace std;

int main()

{
	char fname[15], lname[15];
	char file [50];
	int age;
	int openf;
	int savef;


	cout << "Load previous data?" << endl;
	cout << "1-Yes 0-No ";
	cin >> openf;


		if (openf)
	{

	cout << "Enter the name of the file you want to open: ";

	cin >> file;

	ifstream people(fname);

	people >> fname >> lname >> age;



	cout << "\nFirst Name: " << fname;

	cout << "\nLast Name:  " << lname;

	cout << "\nEnter Age:  " << age;



	cout << "\n\n";
	system("PAUSE");

	return 0;
	}
		else
		{

	cout << "First Name: ";
	cin >> fname;

	cout << "Last Name: ";
	cin >> lname;

	cout << "Your age: ";
	cin >> age;

	cout << "\nEnter the name of the file you want to create: ";
	cin >> file;

	ofstream people(file, ios::out);

	people << fname << "\n" << lname << "\n" << age;

	cout << "\n\n";
		return 0;
		}

}


Is This A Good Question/Topic? 0
  • +

Replies To: Read/Write data to file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Read/Write data to file

Posted 03 December 2009 - 04:14 PM

Problem is that you're reading from the file as if all the data were on a single line, separated by spaces, but you're writing your data with each field on a different line. You need to decide which format you're going to go with and go with it.
Was This Post Helpful? 0
  • +
  • -

#3 virusinfektion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 08

Re: Read/Write data to file

Posted 03 December 2009 - 04:44 PM

Got it. thanks.

	cout << "Enter the name of the file you want to open: ";

	cin >> file;


	ifstream people(file, ios::out);

	people >> fname >> lname >> age;



	cout << "\nFirst Name: " << fname;

	cout << "\nLast Name:  " << lname;

	cout << "\nEnter Age:  " << age;


Was This Post Helpful? 0
  • +
  • -

#4 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 556
  • Joined: 24-July 08

Re: Read/Write data to file

Posted 03 December 2009 - 04:50 PM

Note that ifstream and ofstream are both classes so they both have "Methods" or Functions in them, you should look them up...here are some that will help you out

ifstream::getline(char * Buffer, int Amount_To_Read);
ifstream::get(char Buffer);
ifstream::seekg(int Spaces, ios::mode);
ifstream::tellg();

there are plenty that are usefull for different occasions, such as getline would help you with your problem here.

Happy Coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1