Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading in files and then merging with another file. 1

Status
Not open for further replies.

b0xx

Programmer
Nov 13, 2007
19
US
I am working on a project where I have 2 files, a .db file, and a .txt file. The DB file contains a 'tag' and a 'value'. The txt file contains a tag, and other info, like location, units, etc so you know what the value means. I need to read in the txt file and the db file, and display them so that everything from the txt file is associated with the db file. I am pretty new to borland. Right now I have the db file displayed, but dont know how to get the text file read-in and then merged with the db file. Any tips?
 
Look at thread101-1426237 for some help in reading a text file. As far as matching them, we'll need more info.



James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
I prety much figured out reading in the file. Here is a more clear version of what I am talking about, hopefully it isnt just more confusing.

I have the db file already displayed in the app. I need to read in the txt file somehow, through an open dialog I guess, and store it somewhere. I would normally just create a class with the variables I wanted, then create an array of objects to hold all the entries.

Here is where I really get stuck. Both the db and txt files have a tag field. I need to display the value (the 2nd column) of the db file with the rest of the data that I got from the text file (which is now stored in an object array). The value is just a number and is meaningless without the 3 fields from the text file also. They dont have to actually be merged, they just have to display that way.

Right now, I have a db with two columns, and a txt file with 4 fields per line. 1 of the fields is redundant, and doesnt need displayed, it just links the records together. I would like to display a 4 column table, with the 'value' field from the db, and the 3 other fields from the text file.


-------------

That may be a lot to answer. My main issue right now is how do you use the data in the class objects inside borland. Just for simplicity, If I have a class object with some strings in it, how do just just display them on the screen? What component do I display them in? Thanks.
 
OK. Let's start with the last question first.
...how do just just display them on the screen? What component do I display them in?

You have a variety of choices. MemoBox, ListBox, or a StringGrid for example. The MemoBox is very easy to use but from the sound of what you want to do, the StringGrid may be more apropos. It's a bit complicated but very powerful. Look over that and then check back with more questions.

Also check out the free articles at C++ Developer's Journal and BCB CAQ.



James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
Thanks. That appears to be exactly what I am looking for. And thanks for the link, I hadnt run across that site yet.
 
Excellent on-line rag. Worth subscribing to, IMHO.



James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
Ok, so I made a program in regular c++, that reads in the file the way I want. It uses a class, arrays, functions etc.

Where do I put all that within borland?
Can I even do that?
Where do I put class definitions? (in bcb I have main.cpp and main.h)
If I want to have a button that will read in the file, can I just paste the code into the __fastclick function for the button?
What if I need more parameters in the function call? Can I just add them?
 
I have figured out the things in my previous post, but I have another major question.

I am using a TStringGrid. I would like to take a column from a .db file and put it into the string grid. If I could do it just that easily, that would be great.

However, if there is a way to convert each field in a db file to a string (ansistring specifically), I can make that work.
 
What version of Borland's C++ Builder are you using, Personal, Pro, Developer, or Enterprise?

The Pro and above have a DB component that links to a data base and displays it in a string grid just like you described.



James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
In case it helps, let me say exactly what I currently have. I have a string grid with some data I read in from a column. I also have a .db displayed (DataSource linked to a table linked to a TDBGrid). I would like to somehow merge them. Either into a tdbgrid or a tstring grid. The easiest for me would be to convert the fields into a string, so I can display them in the string grid. I dont need to have the table and dbgrid, thats just the way I currently have it displayed. I found tdbmemo, but I dont think that is exactly what I want...
 
Off the top of my head I can think of a couple of ways. You can use another string grid and when you press a button, the two grids are merged into one.

Another way is to use a button that will merge them into a single output file.

Which would you prefer or do you have another option? Once you decide which, we can proceed from there.

Another question or two, are you filtering the data going into the DB grid and how is the data going into the string grid?



James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
I am reading the data that is in the string grid in from a text file, and displaying it. I am not filtering the data in the DB grid.

I think merging the two grids into one is exactly what I am looking for.
 
OK. Here is an overview of what I believe you want to do. Make any corrections if I'm wrong.

After you have read the text file into the string grid and the database table into the DB grid, you can press and button and mix the two into another string grid.

The button will read each record in the db grid (called a dataset) and each line in the string grid and merge them into the new string grid.

Now for some particulars. How are the merged, line by line, field b field, or what? In other words, how do you want the end result to look?

What happens if there are more lines in one grid than the other? For example, what happens if there are more lines in the DB grid than the string string?

What kind of errors can you for see that you want to prevent or inform the user of?

Once we get these cleared up, we can get into the guts of the program.


James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
I really appreciate the help man.

The idea of the program is to just be a data viewer. I have a db file with just a 'tag' and a value. The text file could best be called a cross reference file. It contains a tag as well (I am assuming every tag in the db file will appear in the Xref file). It also contains location, value type, and unit. The idea is to display the data from both files together. So, I need to display everything in one grid.

The current data I have is very short, 8 lines in each file, but it needs to be able to handle a lot more than that. I think, but am not positive, that there can be more than one of the same tag in each db file (with different values associated with it), but only one of each tag in the Xref file.

So, ideally, I would like to start with the record in the db file. Use the tag and search through the contents of the text file (it is all stored in a class array), and display the cross reference info for that tag. At this point, I am not worried about speed, I just want it to all work.

I would like for the end result to have 5 columns: tag, value type, value, unit, location.

After thinking about it more, purely merging the two grids maybe isnt the best solution, since I would like to be able to handle different number of lines in the two grids.

As for other errors, besides different numbers of rows in each grid, I would probably want to alert the user if a tag exists in the db and doesnt exist in the Xref, and vice versa.

Right now I have the path of the file hardcoded into the program. I can add an open dialog later.

Hopefully that answers everything.
 
OK, I think I might do this online since others may benefit from the discussion. Let's see how much I can help. I don't know all the details of your setup but that's OK. I can give you the general idea. With the holiday coming up and my work schedule I can't guarantee anything.

Let's start with the database first. I don't know how you connect your DB grid to you database but the ideas below can be adapted to whatever method you use, dataset, table, SQL, etc.

For each record in your table you want to find a corresponding tag in your string grid. (If you really wanted to, you could skip both the DB and string grid and read the table and file directly and only output the results.)

The example below is using a table instead of a dataset. If you are using SQL, the example will need to be changed slightly as will some of the called methods depending on whether you are using BDE, etc.

First loop through each record in the table. I do this in a try...catch statement in order to be a little safer on errors. If the table is not empty, get the first record. Go into a while loop checking for the end of the data in the table. Once you have a tag, we can get record(s) from the string grid in phase two.

Code:
try
{
    if (MyOwnTable->IsEmpty()) // Nothing to get
    {
        // do something to inform the user
    }  // Nothing to get

    // Set up temp variables
    AnsiString TagStr, ValueStr;

    // Get first record;
    MyOwnTable->First();

    // Now loop through all bundles
    while (!MyOwnTable->Eof)
    {
        TagStr = MyOwnTable->FieldByName("Tag")->AsString;
        ValueStr = MyOwnTable->FieldByName("Value")->AsString;

        // Collect data from string grid in phase 2

        // Next record
        MyOwnTable->Next();
    }
}
catch (...)  // Opps
{
    // Perform some sort of error cleanuo
    // For example
    MessageDlg("You have ... an ERROR!", mtError, TMsgDlgButtons() << mbOK, 0);

    // and/or throw the error
    throw;
}           // Opps

You could also use a for loop instead of a while loop

Code:
for (MyOwnTable->First(); !MyOwnTable->Eof; MyOwnTable->Next())
{
...
}

Chew on this for a while and then we can move on to phase two.


James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
Ok, I will work on this for the time being.

I was also thinking about writing some sort and filter functions, unless they are already provided.

Also, the way I was going to do the filter and sort was to clear the string grid and then re-write it. Is there any simple way to clear it?
 
Also, when are you done for the day (central time here) and when are you done for the week.
 
Is there any simple way to clear it?
Hmm.... I've only used the string grid once. I cleared it by taking the number of rows and columns and starting past the fixed number, then looped through to clear everything out.

Code:
for (I = StringGrid1->FixedCols; I < StringGrid1->ColCount; I++)
    for (J = StringGrid1->FixedRows; J < StringGrid1->RowCount; J++)
      StringGrid1->Cells[I][J] = "";

As far as my hours, I work 10-12 hours a day starting at 7:00 am EST. I usually give up looking at the web around 5:00 pm. How much I'll be able to help you depends a lot on what happens at work.

This Thursday and Friday (and all Saturdays and Sundays) I hope to come in only as long as necessary (an hour or two usually). I may check here from home but I wouldn't count on it. When they'll let me go on Wednesday depends on what happens during the day.




James P. Cottingham
-----------------------------------------
I'm number 1,229!
I'm number 1,229!
 
Couple other little things while you are at it...

Can you resize the fixed column width in tstringgrid?

I tried to make some buttons that would go through the records, but I couldnt get the images to come up. I was using imagelist, and i thought the images were built in, but I may be wrong.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top