Хочу написать программу, которая создаст мне файл не в папке самой программы, а в заданой мной директории линукс, например, ~/workspace/file.txt. Я задаю полный путь, компилирую программу, выполняю, но файл ею не создается. Что я делаю не так?
#include <fstream>
#include <string>
int main()
{
const char *path="/workspace/file.txt";
std::ofstream file(path);
std::string data("data to write to file");
file << data;
file.close();
}
Скорее всего, у программы нет прав на запись в данную директорию. Для проверки выполните #chmod 777 /workspace и #setenforce 0
И попробуйте выполнить вот такой код:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ofstream tmpfile("/workspace/tempfile.txt");
std::string mystring("data to write to file");
tmpfile << mystring << std::endl;
tmpfile.close();
}
Код точно рабочий - проверил на своей виртуалке:
[root@svm]# rm -rf /workspace/*
[root@svm]# cat output.c
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ofstream tmpfile("/workspace/tempfile.txt");
std::string mystring("data to write to file");
tmpfile << mystring << std::endl;
tmpfile.close();
}
[root@svm]# g++ output.c -o myprog
[root@svm]# ll /workspace/
total 0
[root@svm]# ./myprog
[root@svm]# ll /workspace/
total 4
-rw-r--r--. 1 root root 22 Oct 19 20:56 tempfile.txt
[root@svm]# cat /workspace/tempfile.txt
data to write to file
[root@svm]#
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости