Solved

Data Manipulation

Posted on 1998-06-08
11
189 Views
Last Modified: 2010-04-01
I am currently writing a program that will aid me in pulling a data file into MS Access.  The original data is seperated by commas and their are twelve fields which I am trying to pull in.  That is easy enough, but half way through the original data I find that some of the fields are missing.  Can you offer me any suggestions as to how I can make my program see that a field is missing and make the appropriate adjustments?
0
Comment
Question by:hawhite
  • 8
  • 3
11 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1165413
Is it that the field contents are missing or the contents and the delimiters?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165414
Is it that the field contents are missing or the contents and the delimiters?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165415
If it is just the contents that are gone and the delimiters (commas) are still there, you can just decide on an appropriate default value for each field.  
If the delimiters are not there it is much tougher,  because when you read a field's contents you hav to decide which field it really belongs in.
0
 

Author Comment

by:hawhite
ID: 1165416
The delimiters and the field contents are gone.  The first field in the file is a date field it is in the foloowing format:
1-JAN-98
If I could figure out how to get this one field to get on the next line and have everything follow thereafter than that would be a great help.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165417
Are you saying that when a field is missing, all the remaining fields for that record are missing?  For example if there are three fields, then they are the first three fields, and the remaining ones are skipped?  Its not like they are the first, fourth, and tenth fields.  If that is the case it is not too hard to do.

If that is the case, what are you doing now to the read the information?  can you post a little sample code and i will show you how to change it.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:hawhite
ID: 1165418
The record is intact; it is just that some of the fields are blank.
I have not typed the code to try to correct this problem yet but I will show you what I have.

/*************************************
   Author  :  Henry White
   Date    :  May 29, 1998
   
   Program Implementation
   ======================
   This program will process a data
   file so that it can be read by
   a more advanced application with
   improved ease.
*************************************/

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <string.h>

const int MAX = 10;                 // constant to hold a set maximum
const int FMAX = 20;                // constant to hold a set maximum for a file size

void main(void)
{
   char
      c,                            // variable to hold a character
      IFilename[FMAX],              // an array of characters to hold an input filename
      OFilename[FMAX];              // an array of characters to hold an output filename

   fstream
      Input,                        // Input file declaration
      Out,                          // Output file declaration
      Tempout,                      // Output file file declaration
      Intemp;                       // Input file declaration

   char
      Date[MAX],                    // Variable to hold the Date field from the file
      Time[MAX],                    // Variable to hold the Time field from the file
      SM[MAX],                      // Variable to hold the Spinning Machine info from the file
      Prod_Type[MAX],               // Variable to hold the Product Type
      DL[MAX],                      // Variable to hold
      DA[MAX],                      // Variable to hold
      DB[MAX],                      // Variable to hold
      DECMC[MAX],                   // Variable to hold
      HIT_PCT[MAX],                 // Variable to hold
      PIG1_HIT[MAX],                // Variable to hold Pigment 1
      PIG2_HIT[MAX],                // Variable to hold Pigment 2
      PIG3_HIT[MAX];                // Variable to hold Pigment 3

   cout <<"\nPlease enter the name of the file to be processed: ";     // ask user for input file name

   cin >> IFilename;                                                   // gets user's input file name

   Input.open(IFilename, ios::in);         // opens the file that the user entered

   Tempout.open("temp.out", ios::out);     // opens an intermediary file that the program will use
                                           // to aid it in processing this file
                                           
   while(Input.fail())                                                 // Error Checking
   {
      cerr <<"\nError opening File!"
           <<"\n Please try again\n";      
     
      cout <<"\nPlease enter the name of the file to be processed: ";  // ask user for input file name

      cin >> IFilename;                                                // gets user's input file name
   
      Input.open(IFilename, ios::in);                                  // opens the file that the user entered
   }

/*=======================================
 
  Loop Purpose
  ============
 
  This loop will grab a charcter
  from the file and compare it
  to a comma, if their is a
  match it will replace the
  comma with a blank space.
 
  ******************************
 
  The changes will be in the
  intermediary file "temp.out"
 
=======================================*/

   Input.get(c);                           // pulls in a character from the file

   while(!Input.eof())
   {
      Input.get(c);

      if(c == ',')
         Tempout << " ";

      else
         Tempout << c;
   }

   cout <<"\nPlease enter the name of the file to be created: ";      // Enter the name of the final output file

   cin >> OFilename;                        // gets user's final output file name

   Intemp.open("temp.out", ios::in);        // opens input stream for the intermediary file "temp.out"

   Out.open(OFilename, ios::out);           // opens the user specified file (final output file)

   while(!Input.fail())                                                 // Error Checking
   {
      cerr <<"\nError opening File!"
           <<"\n Please try again\n";
       
      cout <<"\nPlease enter the name of the file to be created: ";   // Enter the name of the final output file

      cin >> OFilename;                                               // gets user's final output file name
     
      Out.open(OFilename, ios::out);
   }  
   
   while(!Intemp.eof())
   {
      Intemp >> Date >> Time >> SM >> Prod_Type >> DL >> DA >> DB ;            // Extracts information from
      Intemp >> DECMC >> HIT_PCT >> PIG1_HIT >> PIG2_HIT >> PIG3_HIT;          // intermediary file "tmp.out"

      Out    << setw(7) << Date  << setw(7) << setw(7) << Time                 // and inputs the information into
             << setw(7) << SM << setw(7) << Prod_Type << setw(7) << DL         // the final output file
             << setw(7) << DA                                                  // after being formated
             << setw(7) << DB << setw(7) << DECMC << setw(7) << HIT_PCT        // for easy import.
             << setw(7) << PIG1_HIT << setw(7) << PIG2_HIT
             << setw(7) << PIG3_HIT << endl;
   }

   Intemp.close();                          // close
   Input.close();                           // all
   Out.close();                             // input & output
   Tempout.close();                         // files.


// end of program
}

Here is a sample of the record (one complete and one screwed up)

3-MAY-1998  22:50    30W  C405A-0.8367-0.0917 0.0217 0.4083   99.8    -13    -96   -127
4-MAY-1998  00:40     27  D405A-1.0671 0.6500 0.2100 0.6375   78.1   -157   -3514-MAY-1998
  00:50    28N  C123A-0.3064-0.0543-0.3800 0.6779   99.9   -249   -293     394-MAY-1998

I am sorry if it displayes incorrectly.  Tell me your thoughts.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165419
First of all, you said the data was comma delimited.  But I don't see commas.  I see spaces.

Could you e-mail me the source code AND the data file.  I think if I can see it better I can do a better job.  Don't worry, we can get this to work, I just need to get a better look at it.

My e-mail address is nietod@theshop.net
0
 

Author Comment

by:hawhite
ID: 1165420
The original data is comma delitited, but I removed the commas thinking that all of the records were complete.  Under this assumption, I removed the commas and put them in a temp file then I placed the new space delimited input into the final file which I formated into columns using iomanip.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1165421
Okay.  I'm not too familiar with the C++ streams, so I could be wrong here, but to the best of my knowledge they aren't going to be of much help in getting the information.  

What you will need to do is to read one record into a string at a time and examine that string for fields.  To do this you can use the getline() procedure, like

char Lin[128];

intemp.getline(Lin,128);

this will read one line from the intemp string.  (or 127 characters, whichever comes first.  You want the size to be longer than the longest line in the file.)  You then need to exract the information from that line.

continues.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1165422
For this you can use the strtok() function.  It is used to parse a long string into smaller strings besed on delimiters.  (you could keep the comma delimiters if you want or use spaces if you like.  I'll assume you will keep the commas.)

char *PrmPtr = strtok(Lin,","); // -> current parameter.

if (PrmPtr) // If there is a first parameter.
{
   // Get the first parameter with PrmPtr.
   PrmPtr = strtok(Lin,",") // Get -> 2nd parameter.
}
if (PrmPtr) // If there is a 2nd parameter.
{
   // Get the 2nd parameter with PrmPtr.
   PrmPtr = strtok(Lin,",") // Get -> 3rd parameter.
}
if (PrmPtr) // If there is a 3rd parameter.
{
   // Get the 3rd parameter with PrmPtr.
   PrmPtr = strtok(Lin,",") // Get -> 4th parameter.
}

and so on.   I hope this helps.  Let me know if you have any questions.
   

0
 
LVL 22

Expert Comment

by:nietod
ID: 1165423
How is this working?
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now