I am having trouble reading a file. My eventual plan is to read in the file, fix a formatting problem with a regex and export the changed data to a new file. However my importing skills are not working.
Can anyone help me in the right direction? I believe my problem is that I have set lengths for the import and what I should have done is look for the next space.
Can anyone show me a snippet that looks for the next space?
Below is what the command prompt shows:
this file did not import. But it is scrambled and repeats each entry and shows me garbage.
This is an example of the import file:
70039 F Abbott Cheyenne J 9185107925 6106 81 E Ave G Tulsa OK 74133 0K 105
70302 M Aberson Joshua S 9188728068 8714 59 St S Tulsa OK 74145 01 105
58381 M Aguila Gervace M 9183071924 5660 S 82 E Ave Tulsa OK 74145 01 105
58382 M Aguila Kaleb R 9183071924 5660 S 82 E Ave Tulsa OK 74145 01 105
56893 M Alsrehi Tarig K 9186989775 7948 Sheridan Rd 1112 Tulsa OK 74133 02 105
55502 F Alatorre Alexia M 9183848826 13382 E 32 Pl S Tulsa OK 74134 03 105
61977 M Albik Bshar M 9182499593 8921 E 58 St S Tulsa OK 74145 01 105
52038 M Aldrich Christopher M 9182490461 5633 S 83 E Ave Tulsa OK 74145 05 105
68435 M Alejandrorodrigu Joab 9186307325 5931 S 87 E Ave Tulsa OK 74145 01 105
68434 M Alejandrorodrigu Jose M 9186307325 5931 S 87 E Ave Tulsa OK 74145 05 105
Here is my code so far:
Expand|Select|Wrap|Line Numbers
- #include "stdafx.h"
- #include <iostream>
- using std::cout;
- using std::cin;
- using std::endl;
- using std::ios;
- using std::cerr;
- using std::fixed;
- #include <fstream> //file stream
- //using::ifstream; //input file stream
- #include <cstdlib>
- #include <iomanip>
- using std::setw;
- using namespace std;
- struct studentData //record format
- {
- int studentId; //key field
- char gender[1]; //other data
- char lastName[20];
- char firstName[20];
- char middleInt[1];
- int phoneNumber;
- int homeAddress;
- char streetName[28];
- char addUnknownA[5];
- char apartmentNumber[10];
- char city[20];
- char state[2];
- int zip;
- char grade[2];
- int site;
- };
- void outputLine( ostream&, const studentData & );
- int main( void )
- {
- //inCredit = inTrans
- ifstream inTrans( "G:VersaB.txt", ios::in | ios::binary );
- if ( !inTrans )
- { cerr << "File could not be opened.\n" << endl;
- system("PAUSE");
- exit( 1 );
- }
- cout << setiosflags( ios::left ) << setw( 10 ) << "StudentId"
- << setw(1) << "Gender" << setw( 20 ) << "Last Name" << setw( 20 )
- << "First Name" << setw(1) << "Middle Intial" << setw(10) << "PhoneNumber" << setw(7)
- << "House #" << setw(28) << "Street Name" << setw(5) << "Unknown Address"
- << setw(10) << "APT #" << setw(20) << "City" << setw(2) << "State" << setw(5)
- << "Zip" << setw(2) << "Grade" << setw(3) << "Site" << resetiosflags( ios::left )
- << endl;
- studentData student;
- inTrans.read( reinterpret_cast<char *>( &student ),
- sizeof( studentData ) );
- while ( inTrans && !inTrans.eof() )
- {
- if ( student.studentId != 0 ) // don't display empty accts
- outputLine( cout, student );
- inTrans.read( reinterpret_cast<char *>( &student ),
- sizeof( studentData ) );
- }
- inTrans.close(); // done
- cout << endl;
- system("PAUSE");
- int character; //use int, because char cannot represent EOF
- cout << "Brad King is Amazing!" << endl;
- cout << "Press Any Key and the 'Enter' Key to Exit" << endl;
- scanf("%d");
- return 0;
- return 0;
- }
- void outputLine( ostream &output, const studentData &s )
- {
- output << setiosflags( ios::left ) << setw( 10 )
- << s.studentId << endl << setw( 1 ) << s.gender << endl << setw(20)
- << s.lastName << endl << setw( 20 ) << s.firstName << endl << setw( 1 )
- << s.middleInt << endl << setw(10) << s.phoneNumber << endl << setw(7)
- << s.homeAddress << endl << setw(28) << s.streetName << endl << setw(5)
- << s.addUnknownA << endl << setw(10) << s.apartmentNumber << endl << setw(20)
- << s.city << endl << setw(2) << s.state << endl << setw(5) << s.zip << endl << setw(2)
- << s.grade << endl << setw(3) << s.site << endl
- << setprecision( 2 ) << resetiosflags( ios::left )
- << setiosflags( ios::fixed | ios::showpoint )
- << '\n';
- }