Hi,

I want to read/write data from a file and I don't know how (I'm using Visual studio c++). More exactly, if I have the following code:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	ofstream newfile;
	newfile.open("exemplu.txt");
	myfile<<"I want to write this in a file\n";
	newfile.close();
	return 0;

}

it compiles and it runs without errors but...where should I write the address of the file where I want to write te sentence "I want to write this in a file"

Thank you and...Happy Christmas! :)

Dani AI

Generated

— when you give just a filename to an output stream, the file is created relative to the program's current working directory (not necessarily your source or project folder). 's absolute-path suggestion is a solid way to be explicit, and is right about backslashes being escape characters. A few practical tips to avoid surprises:

Print or inspect the working directory at runtime so you know where the file will appear. With a C++17 compiler you can do that and safely open a file while checking for errors:

#include <iostream>
#include <fstream>
#include <filesystem> // requires C++17

int main() {
    std::cout << "cwd: " << std::filesystem::current_path() << '\n';
    std::ofstream out("output.txt", std::ios::app);
    if (!out.is_open()) {
        std::cerr << "Failed to open output.txt\n";
        return 1;
    }
    out << "Sample text\n";
}

If you run from Visual Studio, the debugger's working directory can be changed in Project Properties -> Configuration Properties -> Debugging -> Working Directory. To avoid escaping backslashes you can use forward slashes in paths on Windows or a raw string literal if you prefer backslashes (raw strings were added in C++11). Use std::ios::app to append instead of overwriting; the default truncates the file.

Final checklist: make sure you write to the same stream you opened, check the stream state after opening, close or flush the stream when done, and avoid protected folders (Program Files, root of C:) unless you have the necessary permissions. These steps will make the file behavior predictable across runs and environments.

Recommended Answers

All 4 Replies

You can write files anywhere you want to on your computer as long as your login account has permissions to do that. For example newfile.open("c:\\dvlp\\test\\exemplu.txt");

Thank you for the quick answer! It works :) I had already tried to put the address right there but instead of double backslash I was typin a simple backslash.
Have a nice day!

The first backslash is an escape character, just like when you type \n you are escaping the character n and qualifying that it's a newline. So say you had for example mydir\name in your directory string (or any string for that matter) you then have to escape the \ character to tell it you just want the backslash.

Understood ! Now it seems logical :) Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.