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
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
.dat files can contain anything. You can simply just rename the extension .dat instead of .txt and access it the same through your program.
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?
you could input the contents of file.txt to a variable then save it to file2.dat
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
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?
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..
it seems that you set your windows settings so it doesn't show the extension.filename.dat.txt
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.
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");
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'
warning! warning! dont use system() they are a huge bufferoverflow security risk!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");
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.