我想使用 ofstream 写入文件,但它什么也没给我
I want to write to a file by using ofstream but it gives me nothing
我已经用 _mkdir()
创建了三个目录,我想在最后一个目录中创建一个文本文件,但是它什么也没给我。
代码如下:
#include "stdafx.h"
#include <direct.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int t;
string path, subpath;
path = "D:";
subpath = "d";
for (int i = 0; i < 3; i++)
{
path = path + "\" + subpath;
t=_mkdir(path.c_str());
if (t)
cout << "create directory\n";
else
cout << "unable to create directory\n";
}
fstream file;
file.open("filetest.txt", ios::out||ios::in);
file << "haha";
cin.get();
return 0;
}
您在工作目录而不是上次创建的目录中创建了一个文件。
替换
file.open("filetest.txt", ios::out||ios::in);
和
path = path + "\filetest.txt";
file.open(path.c_str(), ios::out|ios::in);
我已经用 _mkdir()
创建了三个目录,我想在最后一个目录中创建一个文本文件,但是它什么也没给我。
代码如下:
#include "stdafx.h"
#include <direct.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int t;
string path, subpath;
path = "D:";
subpath = "d";
for (int i = 0; i < 3; i++)
{
path = path + "\" + subpath;
t=_mkdir(path.c_str());
if (t)
cout << "create directory\n";
else
cout << "unable to create directory\n";
}
fstream file;
file.open("filetest.txt", ios::out||ios::in);
file << "haha";
cin.get();
return 0;
}
您在工作目录而不是上次创建的目录中创建了一个文件。 替换
file.open("filetest.txt", ios::out||ios::in);
和
path = path + "\filetest.txt";
file.open(path.c_str(), ios::out|ios::in);