| | |
problem reading text file to struct
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 
and here is the file i am reading in
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

C++ Syntax (Toggle Plain Text)
// 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
C++ Syntax (Toggle Plain Text)
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
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)
// 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; }
![]() |
Similar Threads
- First step in reading a text file and converting it to a binary file (C++)
- Reading in a text file -- efficiently (Java)
- reading a text file to a remote PC (ASP.NET)
- c++ reading text file (C++)
- Reading a text file Into an Array (C++)
- C++ Reading from a text file (C++)
- Reading from a text file and using it as a database (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Socket
- Next Thread: sum of series
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





