473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Help with the way a file is read.

Hello Everyone,

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
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6. using std::ios;
  7. using std::cerr;
  8. using std::fixed;
  9.  
  10. #include <fstream> //file stream
  11. //using::ifstream; //input file stream
  12. #include <cstdlib>
  13. #include <iomanip>
  14. using std::setw;
  15.  
  16. using namespace std;
  17.  
  18. struct studentData    //record format
  19. {
  20.     int studentId;            //key field
  21.     char gender[1];            //other data
  22.     char lastName[20];
  23.     char firstName[20];
  24.     char middleInt[1];
  25.     int phoneNumber;
  26.     int homeAddress;
  27.     char streetName[28];
  28.     char addUnknownA[5];
  29.     char apartmentNumber[10];
  30.     char city[20];
  31.     char state[2];
  32.     int zip;
  33.     char grade[2];
  34.     int site;
  35. };
  36.  
  37. void outputLine( ostream&, const studentData & );
  38.  
  39.  
  40. int main( void )
  41. {
  42.     //inCredit = inTrans
  43.  
  44.     ifstream inTrans( "G:VersaB.txt", ios::in | ios::binary );
  45.  
  46.     if ( !inTrans )
  47.    {  cerr << "File could not be opened.\n" << endl;
  48.       system("PAUSE");
  49.       exit( 1 );
  50.    }
  51.  
  52.  
  53.     cout << setiosflags( ios::left ) << setw( 10 ) << "StudentId"
  54.         << setw(1) << "Gender" << setw( 20 ) << "Last Name" << setw( 20 )
  55.         << "First Name" << setw(1) << "Middle Intial" << setw(10) << "PhoneNumber" << setw(7) 
  56.         << "House #" << setw(28) << "Street Name" << setw(5) << "Unknown Address" 
  57.         << setw(10) << "APT #" << setw(20) << "City" << setw(2) << "State" << setw(5) 
  58.         << "Zip" << setw(2) << "Grade" << setw(3) << "Site" << resetiosflags( ios::left )
  59.         << endl;
  60.     studentData student;
  61.     inTrans.read( reinterpret_cast<char *>( &student ), 
  62.                   sizeof( studentData ) );
  63.    while ( inTrans && !inTrans.eof() )
  64.    {
  65.       if ( student.studentId != 0 )    // don't display empty accts
  66.          outputLine( cout, student );
  67.  
  68.       inTrans.read( reinterpret_cast<char *>( &student ),
  69.                      sizeof( studentData ) );
  70.    }
  71.    inTrans.close();       // done
  72.    cout << endl;
  73.    system("PAUSE");
  74.     int character;    //use int, because char cannot represent EOF
  75.     cout << "Brad King is Amazing!" << endl;
  76.     cout << "Press Any Key and the 'Enter' Key to Exit" << endl;
  77.     scanf("%d");
  78.     return 0;
  79.  
  80.    return 0;
  81. }
  82. void outputLine( ostream &output, const studentData &s )
  83. {
  84.    output << setiosflags( ios::left ) << setw( 10 ) 
  85.           << s.studentId << endl << setw( 1 ) << s.gender << endl << setw(20)
  86.           << s.lastName << endl << setw( 20 ) << s.firstName << endl << setw( 1 )
  87.           << s.middleInt << endl << setw(10) << s.phoneNumber << endl << setw(7)
  88.           << s.homeAddress << endl << setw(28) << s.streetName << endl << setw(5)
  89.           << s.addUnknownA << endl << setw(10) << s.apartmentNumber << endl << setw(20)
  90.           << s.city << endl << setw(2) << s.state << endl << setw(5) << s.zip << endl << setw(2)
  91.           << s.grade << endl << setw(3) << s.site << endl
  92.           << setprecision( 2 ) << resetiosflags( ios::left )  
  93.           << setiosflags( ios::fixed | ios::showpoint )
  94.           << '\n';
  95. }
Sep 12 '07 #1
2 1619
Savage
1,764 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1.     studentData student;
  2.  inTrans.read( reinterpret_cast<char *>( &student ),sizeof( studentData ) );

I believe that here is your problem.

Try reading in separately every member of the struct.

Also take a look at this line:

Expand|Select|Wrap|Line Numbers
  1. scanf("%d");
and tell me, what does that line do?

Savage
Sep 12 '07 #2
Thank you!

I am going to change around that read statement.

The scanf was there for me to stop the program before it was finished. I had a problems with my new Vista machine and Visual Studio 2005. I put a simple Cout and scanf to just get something to complie finally. I haven't programmed in a little bit and wanted to make sure the problem wasn't me.

I will have this done in the next few days.
Sep 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: MattP | last post by:
Ok, with the help of some examples found on the web and some minor modifications on our own, we have a simple and working encrypt and decrypt solution. It runs as a service, watches for files with...
1
by: wtnt | last post by:
Hello. I've searched all over and haven't seen another thread with this problem. Please bear with me as I try to explain. thanks. :) I have some programs that need to be cross-platform...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
1
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
1
by: al2004 | last post by:
Write a program that reads information about youth soccer teams from a file, calculates the average score for each team and prints the averages in a neatly formatted table along with the team name....
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.