-5

I am trying this code for a few first times only. I am not able to get to the root of the error.

Here is the code:

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    fstream file;
    file.open("C:\\Users\\AfzaalAhmad\\Documents\\text.txt");
    return 0;
}

The file is present at the location. Here is a screenshot of the file system.

Screenshot of the file system

There is no exception in this case, but the file is never opened!

Where am I missing code?

12
  • Do you need the double back-slashes? Shouldn't it be something like C:\path\to\my\file ?
    – sergico
    Commented Feb 1, 2014 at 15:19
  • 1
    @sergico I need to escape the backslash .. since when I use single slash it gives error! Commented Feb 1, 2014 at 15:20
  • The double backslashes are necessary, the first makes the second literal
    – chrisb2244
    Commented Feb 1, 2014 at 15:20
  • 8
    How do you know the file doesn't open? You don't check for it? Commented Feb 1, 2014 at 15:21
  • 5
    It doesn't open in notepad or something you know - you just have a fstream object inside the C++ program that lets you read it from there. For example, ou can then say std::string line; while (getline(file, line)) std::cout << line << ''; to write the file content to the stdout (terminal) (you'd need to #include <string> and #include <iostream> for that too. Commented Feb 1, 2014 at 15:23

1 Answer 1

5

The command you've written will open a handle to a file at that location. In order to do anything with it, you'll need some sort of read or write operation. Probably your code is working fine :)

For example, following your file.open("...") line:

file << "This is some text to send to my now open file\n";
...
file.close();
5
  • You're welcome. I edited my answer with some examples - the other answer from @karakale is likely to be helpful for you there :)
    – chrisb2244
    Commented Feb 1, 2014 at 15:25
  • I would have tried out his tutorial link, but I don't know why that site is down since 3 days here: imgur.com/RveTZfP .. Commented Feb 1, 2014 at 15:28
  • 1
    @AfzaalAhmadZeeshan: No, you should use a good book. Commented Feb 1, 2014 at 15:30
  • Well, I confess to having used cplusplus.com quite a bit, but I'm told it's not very good. As @LightnessRacesinOrbit points out, SO has a question about the best C/C++ books with some pretty extensive answers.
    – chrisb2244
    Commented Feb 1, 2014 at 15:48
  • @chrisb2244: The reference, nowadays, is fairly accurate and well maintained, but the forum contributions are not inherently peer reviewed, and because of the name of the site you get a lot of entry-level contributions that will teach you bad practices and bugs, written by those who don't yet know any better. It's a vicious cycle, really. The tutorial is okay, but no substitute for a full book written by a prestigious author. Commented Feb 1, 2014 at 15:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.