Thread: Writing to .DAT files

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    21

    Writing to .DAT files

    I've been looking at lots of examples, but i can't seem to write variables to a .DAT file in Dev-c++. Can anyone show me an example on hoow to do this?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Can you write variables to any other file? A dat file is no different (the file extension doesn't really matter, they are there just to associate certain files with a particular program, e.g bmp->Paint)

    Code:
    std::ofstream fout("datafile.dat");
    fout << myvariable;

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    Does that work in an app?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    what do you mean?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Xinco
    Does that work in an app?
    ???

    Is your question how to write to a file or how to write to a *.dat file? If it is the former you should try to find a tutorial on file input/output with C++. If it's the latter, you do it the same you'd write to a *.txt file...

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    I meant an app(.exe) where the user enters a variable and the app saves this variable to a .dat file

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I meant an app(.exe) where the user enters a variable and the app saves this variable to a .dat file
    What do you mean by "the user enters a variable"? Basically, the file extension only means what you make it out to be. Treat a file named "filename.dat" as you would a file named "filename", and proceed as usual.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    See anon's first example.

    Also see this tutorial: http://www.cprogramming.com/tutorial/lesson10.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing and reading files
    By oldie in forum C Programming
    Replies: 1
    Last Post: 07-03-2008, 04:54 PM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. writing to binary files....
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2003, 09:58 PM
  4. Replies: 3
    Last Post: 05-03-2002, 05:18 PM
  5. Writing to files on exact spots
    By R@m0n in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 06:06 AM