Thread: how to convert a text file to a .dat file

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    Question how to convert a text file to a .dat file

    Hi everyone

    Is there a way I could access a .txt file from my program as a .dat file? i.e. is it possible to write a text file but then convert it to a .dat file?


    Linette

  2. #2
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    .dat files can contain anything. You can simply just rename the extension .dat instead of .txt and access it the same through your program.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    It doesn't do it.. If I just rename the file, it becomes filename.dat.txt, which is still a text file. And if I just try to open filename.dat through code without converting, it won't open anything (again because the file isn't .dat)...
    What do I do?

  4. #4
    Unregistered
    Guest
    you could input the contents of file.txt to a variable then save it to file2.dat

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    Could someone please post some code that'd show me how to do that?
    And if I read all the contents of the text file into one variable and then write that to a .dat file, would the compiler then recognize the content as individual structs? I.e. would I then be able to use read and get the contents record (struct) by record?

    Thanks
    Linette

  6. #6
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    Do you want your .dat file to be the same as the .txt one, just with a different extension, or do you want to make it a binary file or what?

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    I want it to be the kind of file that stores whatever's in it in the form of structs. The kind from which you can read contents record by record.. I don't know if that type of file is called binary, maybe.. But I want a .dat file from which I can read content record by record (as opposed to line by line)..
    I hope that explains what I need..

  8. #8
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    filename.dat.txt
    it seems that you set your windows settings so it doesn't show the extension.
    ok well an easy way is
    Start->Run->"cmd"(w\o quotes)
    type(w\o quotes):
    "CD TheDirectory\TheDirectoryofFile" press enter.
    It should say
    C:\TheDirectory\TheDirectoryofFile\>
    then type
    "rename filename.txt filename.dat" press enter.
    Where it say 'TheDirectory\TheDirectoryofFile'
    just type the real directory like C:\Folder\
    hope it helps.

  9. #9
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Right before your program exits insert the following statement (note, you need to #include <windows.h>)

    system("rename file.txt file.dat");

    Don't worry about making the file dat beforehand or anything. Then at the start of the program

    system("rename file.dat file.txt");

    Of course, this will fail if theres no file of that name (eg, first time you run the program, but you could easily put one there for starters.

    Ooh, and a nice touch would be to hide the file

    system("filename.ext attrib +h");

  10. #10
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    Or a simplified way from xterra's would be to open a folder, goto view>folder options>view then uncheck 'hide file extentions of known file types'

  11. #11
    $null
    Guest
    Originally posted by Dual-Catfish
    Right before your program exits insert the following statement (note, you need to #include <windows.h>)

    system("rename file.txt file.dat");

    Don't worry about making the file dat beforehand or anything. Then at the start of the program

    system("rename file.dat file.txt");

    Of course, this will fail if theres no file of that name (eg, first time you run the program, but you could easily put one there for starters.

    Ooh, and a nice touch would be to hide the file

    system("filename.ext attrib +h");
    warning! warning! dont use system() they are a huge bufferoverflow security risk!

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    Every file is basically the same, whatever extension it has. You can open a txt file as binary as well as you can open a dat file as text. However, the output you get will vary. If you open a binary file as text, you will only get lots of #¤$"@ stuff.
    I believe it's impossible to save data as text and then open it up as structurated data (in an easy way). If you need to store structs, you have to save it to the file as a (binary) struct and then open it in the same way.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. Function is called and I am trying to open a file
    By tommy69 in forum C Programming
    Replies: 88
    Last Post: 05-06-2004, 08:33 AM
  4. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM