![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
PFO Founder
![]() ![]() |
Ok my problem is i have a struct and im tring to ready a line into the struct using fstream. here is the code i have its just a basic layout of what will come later on. but here it is
// file: main.cpp
// author: Kyle Kjorsvik
// date: 8/31/04
// class: CSIS 352
// This is the file that contains the main function for assignment 1.
#include <iostream>
#include <fstream>
#include <string>
#include "date.h"
//#include "inout.h"
//#include "agecalc.h"
//#include "data.h"
using namespace std;
struct birthdate
{
char name[];
char bdate[];
};
int main()
{
char chrName[25];
int maxvalue = 10;
birthdate bd[maxvalue + 1];
int i = 0;
ifstream inFile("data"); // input file
if (inFile.fail())
{
cerr << "An error occurred. Unable to read input file." << endl;
exit(1);
}
cout << "File opened" << endl;
while(!inFile.eof())
{
inFile.get(bd[i].name[],25,",");
//cout << bd[i].name << endl;
++i;
}
inFile.close();
return 0;
}and here is the file i am reading in Torii Hunter, 7/18/1975 Jacque Jones, 4/25/1975 Corey Koskie, 6/28/1973 Justin Morneau, 5/15/1981 Some Young Kid, 7/9/1999 Shannon Stewart, 2/25/1974 Joe Nathan, 11/22/1974 Lew Ford, 8/12/1976 Matthew LeCroy, 12/13/1975 J. C. Romero, 6/4/1976 i need to read it in sperating the name and the birthdate. using the comma as the seperator. can someone help me on how i should be reading this into the array of structs? thanks edit: if you need more info let me know ![]()
__________________
BIG K aka Kyle Programming Forums Kyle K Online Please do not PM or email me programming questions. Post them in the forums instead. |
|
|
|
|
|
#2 |
|
PFO Founder
![]() ![]() |
dont know exactly what i was doing wrong as i havent taken time to compare the code that i was give but here is what works
// file: main.cpp
// author: Kyle Kjorsvik
// date: 8/31/04
// class: CSIS 352
// This is the file that contains the main function for assignment 1.
#include <iostream>
#include <fstream>
#include <string>
#include "date.h"
//#include "inout.h"
//#include "agecalc.h"
//#include "data.h"
using namespace std;
struct birthdate{
string name;
string bdate;
};
int main() {
const int maxvalue = 10;
birthdate bd[maxvalue + 1];
int i = 0, j;
ifstream inFile("data");
if (inFile.fail()) {
cerr << "An error occurred. Unable to read input file." << endl;
exit(1);
}
cout << "File opened" << endl;
while(!inFile.eof() && i < maxvalue) {
getline(inFile, bd[i].name, ',');
getline(inFile, bd[i].bdate);
++i;
}
inFile.close();
for (j=i-1; j>=0; j--){//print backwards for giggles
cout << "\t" << bd[j].name << "\t" << bd[j].bdate << endl;
}
return 0;
}
__________________
BIG K aka Kyle Programming Forums Kyle K Online Please do not PM or email me programming questions. Post them in the forums instead. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
glad you got it working
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|