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! :)

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.