Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Help with variable-length fields (http://www.programmingforums.org/showthread.php?t=15252)

starbeam Feb 24th, 2008 4:27 PM

Help with variable-length fields
 
Anyone know where I can get a program that will make use of variable-length fields and variable-length records and convert a file to using variable length.

Ooble Feb 24th, 2008 4:29 PM

Re: Help with variable-length fields
 
You're going to have to rephrase that question so it actually makes sense.

starbeam Feb 24th, 2008 4:33 PM

Re: Help with variable-length fields
 
Okay so say I there is a program that generated a file but it is in fixed length records. How would I go about writting a program that changes a file into using variable lenght fields and variable lenght records.

Ooble Feb 24th, 2008 5:24 PM

Re: Help with variable-length fields
 
Starting to make more sense. What do you mean by "fixed-length records?" Can you give me an example of what you have currently and what you'd like it to be after you change it?

Ancient Dragon Feb 24th, 2008 6:48 PM

Re: Help with variable-length fields
 
Nothing wrong with using fixed-length records, they actually have a lot of advantages over variable length records. Do the fixed-length records contain binary data and you want to convert them to readable text data? If that's the case then you can fairly easily write a conversion program that reads the fixed length records and rewrites them as variable length records into a different file. I've done it several times and it pretty simple stuff, providing you know the format of the fixed length binary records.

starbeam Feb 24th, 2008 10:39 PM

Re: Help with variable-length fields
 
Okay so you have info like for example this....
Field/ Name/ Fixed length
1/ SS#/ 9
2/ Last Name/ 25
3/ First Name / 15
4/ Address / 30
5/ City / 25
6/ State code / 2
7/ Zip code / 5
8/ # courses / 2
But some of the things do not need to be fixed length. Others are fine that way but some would be better as variable length

Ancient Dragon Feb 24th, 2008 11:06 PM

Re: Help with variable-length fields
 
That looks as if someone wrote out the file in binary mode using a structure
:

struct person
{
    char SSAN[9];
    char last_name[25];
    char first_name[15];
    char address[30];
  // etc. etc
};

now to write that out to the file
:

ofstream out("filename",ios::binary);
struct person John;
out.write(&John, sizeof(struct person));


You can read that in very quickly like this
:

struct person John;
ifstream in("filename", ios::binary);
in.read(&John, sizeof(struct person));


Note that you don't have to read/write each field one at a time, and you can quickly access any record in random order by simply seeking to the desired spot.

I see no advantage of converting that to a standard text file unless you want to view it by some standard text editor such as Notepad.

But if you still want to convert the file just write a simple console program that reads in old format and writes in new format. I've already given you some code to work with that reads with the old format. Should be fairly straight-forward for you to write the code to rewrite into the new format, assuming you have the basic knowledge about how to read/write files.

Just in case you need help with that too:
:

ofstream out("newfile");
struct person John;
out << John.SSAN << " " << John.last_name << " " << John.first_name <<
  // etc. etc all the rest of the fields

The above assumes no spaces in any of the fields. Address field will more than likely have spaces, so instead of separating the fields with a space as I did above you can use some other character, such as a tab or '\n'.

starbeam Feb 24th, 2008 11:52 PM

Re: Help with variable-length fields
 
thanks that helped a lot!


All times are GMT -5. The time now is 4:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC