problem reading text file to struct

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2003
Posts: 307
Reputation: big_k105 is an unknown quantity at this point 
Solved Threads: 2
Team Colleague
big_k105's Avatar
big_k105 big_k105 is offline Offline
PFO Founder

problem reading text file to struct [solved]

 
0
  #1
Sep 2nd, 2004
Ok my problem is i have a struct and im tring to ready a line into the array of structs using fstream. here is the code i have its just a basic layout of what will come later on. but here it is

  1. // file: main.cpp
  2. // author: Kyle Kjorsvik
  3. // date: 8/31/04
  4. // class: CSIS 352
  5.  
  6. // This is the file that contains the main function for assignment 1.
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include "date.h"
  12. //#include "inout.h"
  13. //#include "agecalc.h"
  14. //#include "data.h"
  15.  
  16. using namespace std;
  17.  
  18. struct birthdate
  19. {
  20. char name[];
  21. char bdate[];
  22. };
  23.  
  24. int main()
  25. {
  26. char chrName[25];
  27. int maxvalue = 10;
  28. birthdate bd[maxvalue + 1];
  29. int i = 0;
  30.  
  31. ifstream inFile("data"); // input file
  32. if (inFile.fail())
  33. {
  34. cerr << "An error occurred. Unable to read input file." << endl;
  35. exit(1);
  36. }
  37. cout << "File opened" << endl;
  38.  
  39. while(!inFile.eof())
  40. {
  41. inFile.get(bd[i].name[],25,",");
  42. //cout << bd[i].name << endl;
  43. ++i;
  44. }
  45.  
  46. inFile.close();
  47. return 0;
  48. }

and here is the file i am reading in

  1. Torii Hunter, 7/18/1975
  2. Jacque Jones, 4/25/1975
  3. Corey Koskie, 6/28/1973
  4. Justin Morneau, 5/15/1981
  5. Some Young Kid, 7/9/1999
  6. Shannon Stewart, 2/25/1974
  7. Joe Nathan, 11/22/1974
  8. Lew Ford, 8/12/1976
  9. Matthew LeCroy, 12/13/1975
  10. 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
Reply With Quote Quick reply to this message  
Join Date: May 2003
Posts: 307
Reputation: big_k105 is an unknown quantity at this point 
Solved Threads: 2
Team Colleague
big_k105's Avatar
big_k105 big_k105 is offline Offline
PFO Founder

Re: problem reading text file to struct

 
0
  #2
Sep 2nd, 2004
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

  1. // file: main.cpp
  2. // author: Kyle Kjorsvik
  3. // date: 8/31/04
  4. // class: CSIS 352
  5.  
  6. // This is the file that contains the main function for assignment 1.
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include "date.h"
  12. //#include "inout.h"
  13. //#include "agecalc.h"
  14. //#include "data.h"
  15.  
  16. using namespace std;
  17.  
  18. struct birthdate{
  19. string name;
  20. string bdate;
  21. };
  22.  
  23. int main() {
  24. const int maxvalue = 10;
  25. birthdate bd[maxvalue + 1];
  26. int i = 0, j;
  27.  
  28. ifstream inFile("data");
  29. if (inFile.fail()) {
  30. cerr << "An error occurred. Unable to read input file." << endl;
  31. exit(1);
  32. }
  33. cout << "File opened" << endl;
  34.  
  35. while(!inFile.eof() && i < maxvalue) {
  36. getline(inFile, bd[i].name, ',');
  37. getline(inFile, bd[i].bdate);
  38. ++i;
  39. }
  40. inFile.close();
  41.  
  42. for (j=i-1; j>=0; j--){//print backwards for giggles
  43. cout << "\t" << bd[j].name << "\t" << bd[j].bdate << endl;
  44. }
  45.  
  46. return 0;
  47. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC