Thread: Fixed-Length Files

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    5

    Fixed-Length Files

    Hello. I'm working on a project that requires data to be read from and written to a "fixed-length .dat file." I've googled this and have found a very general idea of what it is but not exactly how to create one. Is it simply just a standard text file? The project specifies that the data file must contain only the following fields and be in this format:
    Code:
    Field1          Max length for value          Value
    Field2          Max length for value          Value
    
    Field1          Max length for value          Value
    Field2          Max length for value          Value
    There are several different sets of data, all of which have the same fields. (Does this make sense? The above is the general format of the file)

    Is there any special type of format or code that I need to use to make this a fixed-length file? Would it involve using arrays to limit the maximum number of values or something? If so, what would be a good way to go about differentiating between the different sets?
    Sorry if this is an obvious question or something. Let me know if I didn't explain something clearly or if I need to elaborate more. I would greatly appreciate any help with this problem.

    Thanks.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    A fixed length file is made up of fixed length records. If a record is 10 bytes long, and you have 5 records, the file is 50 bytes long. It's that simple.

    When you read the file length and divide by the record length, you get the exact number of records in the file.

    It could be a .txt file or any file extension. I would be inclined to read and write in binary mode, and it would be silly not to if there is binary data in your records, otherwise, the data could be interpreted at control characters.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    I think I see exactly how these files work now. Thank you very much for clearing that up. Is there any type of function or code that could be used to convert numbers (as char types) from a text file input into integers/doubles/etc? The only way I can think of doing this is by using the isdigit() function and casting to int if the function returns true. Is there any other way of doing this?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use a stringstream. There are other options as well, although IMO a stream is best.

    In fact, when reading in the values in the first place you may be able to use the input stream to do the conversion itself.

    If you're talking about writing out and reading in ints/doubles to and from your dat file, you shouldn't have to do any conversion.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The usual method is to use something like fscanf() [1], but if you don't know what format the data is in, then you will have to use more complicated methods.

    [1] If you want to ensure safety against incorrectly formed files, use fgets() and then sscanf() on the input, or even better, strtol(), strtod() to convert the numbers.

    Edit: my answer is more applicable for C than C++, in C++ stringstream corresponds to sscanf() [roughly], and will do the job for you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Is it simply just a standard text file?
    That would be my guess.

    >Is there any special type of format or code that I need to use to make this a fixed-length file?
    For writing the file you can use setw() to set the width of a field, which is in <iomanip>. And you can use std::left or std::right to left or right justify a field:
    Code:
    std::ofstream out("file.dat");
    out << std::left << std::setw(16) << field1 << std::setw(10) << max_len << setw(10) << value << '\n';
    For reading, one option would be to use the whitespace as your delimiter:
    Code:
    ifstream in("file.dat");
    std::string field1;
    int max_len, value;
    in << field1 << max_len << value;
    A second option would be to read everything as strings using std::getline(), and convert the numbers afterward. A third option would be to use some C functions like fscanf(), or fgets() plus sscanf().

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think working with a binary file using read and write is probably what the project is asking for. Using setw will help give columns a fixed width, but that's different than what I would think a "fixed length dat file" is.

    For a fixed length dat file, you keep your records in a struct that uses all PODs. A POD is just a built-in datatype or struct or array of built-in types that doesn't have extra data.

    That means strings would be stored in character arrays rather than in the string class. (Technically you could also use the string class, but it would be harder to read back in.)

    You just read and write each instance of your struct directly to the file, it is rather easy.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Thanks for all the suggestions, guys. I believe I'll be able to do this now. Using the whitespace as the delimiter with an ifstream object is probably what I'm going to do, as the file that is required to be read into the program isn't in binary format and will need to be able to be interchanged with a different file (still in text format) with the same layout but different values.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  3. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  4. Fixed Length Binary I/O
    By justins in forum C Programming
    Replies: 3
    Last Post: 03-29-2008, 11:28 AM
  5. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM