944,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14231
  • C++ RSS
Sep 2nd, 2004
0

problem reading text file to struct [solved]

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Team Colleague
Reputation Points: 36
Solved Threads: 2
PFO Founder
big_k105 is offline Offline
308 posts
since May 2003
Sep 2nd, 2004
0

Re: problem reading text file to struct

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

C++ Syntax (Toggle Plain Text)
  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. }
Team Colleague
Reputation Points: 36
Solved Threads: 2
PFO Founder
big_k105 is offline Offline
308 posts
since May 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Socket
Next Thread in C++ Forum Timeline: Single brace use?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC