#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;
}
}
Read/Write data to fileread/write data to file
Page 1 of 1
3 Replies - 1054 Views - Last Post: 03 December 2009 - 04:50 PM
#1
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.
Replies To: Read/Write data to file
#2
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.
#3
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;
#4
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
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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|