writing to file

This is a discussion on writing to file within the C++ Programming forums, part of the General Programming Boards category; hey, I am pretty new to c++ I am using the dev c++ compiler (if that makes a difference) Anyway ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    29

    Exclamation writing to file

    hey,
    I am pretty new to c++
    I am using the dev c++ compiler (if that makes a difference)

    Anyway I've been doing a bunch of beginner tutorials and finally reached the part about writing and reading files. Here's the code I'm using

    Code:
    #include <stdio.h>
    #include <string>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
    ofstream asdf("C:\asdf.dat");
    asdf << "hello";
    asdf.close();
    
      return 0;
    }
    I run this and it does not have any errors. It runs, but nothing actually happens. As I understand it should create a file called asdf.dat with the words hello in it.

    I've looked around at several tutorials, and they're all using this code. If someone can help that would be realy appriciated

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    "c:\\asdf.dat" You also may want to look for a file in C:\ named " sdf.dat" or [something]sdf.dat "\a" is the "alarm" character. std::cout << '\a'; beeps (on some consoles)

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "\a" is the "alarm" character.
    ...but since you are escaping the '\' character, hence the double slashes, which says: "hey, this isn't an escape character, it's literally a slash mark", would you still run into that problem?
    Last edited by 7stud; 02-17-2005 at 10:42 AM.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    naturally, '\\' is just one slash. Often "c:/asdf.dat" will work, even on dos liniage OS's, but I stll prefer to go with the leaning toothpick sindrome. It's more likely to work in windowsville and breaks on unix, where your paths would have to be moved around anyway, but this is less likely to suprise you. Headers get real slashes
    i.e. #include <boost/shared_ptr.hpp>

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    thanks guys that fixed it.
    Why is it C:\\ though?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Why is it C:\\ though?
    which says: "hey, this isn't an escape character, it's literally a slash mark"
    cout << '\a'; beeps
    A slash is an 'escape' character which has a special meaning. For instance, \n is a newline, \t is a tab, and \a is an alert bell. In the 'escape sequence':

    \a

    the \ character says:"this is a special escape sequence and the letter to follow is not to be interpreted as an 'a'--instead ring the bell". So, your file name with the slash looks like you are trying to tell C++ that the next character should not be literally interpreted, but rather take some special action instead. Of course, that leaves the problem of how to signal that you want a slash to be literally interpreted. The method decided upon was that a double slash would mean a literal single slash.
    Last edited by 7stud; 02-17-2005 at 03:10 PM.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    that makes sense, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21