ユーザーからのテキストを受け入れてテキストファイルに保存するC++のプログラムがあります。プログラムのスニペットは次のとおりです。
#include "stdafx.h"
#include <ctime>
#include <fcntl.h>
#include <iostream>
#include <string>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <Windows.h>
using namespace std;
int file_descriptor;
size_t nob;
int check_file(const char* full_path) //Method to check whether a file already exists
{
file_descriptor = open(full_path, O_CREAT | O_RDWR, 0777); //Checking whether the file exists and saving its properties into a file descriptor
}
void write_to_file(const char* text) //Method to create a file and write the text to it
{
time_t current = time(0); //Getting the current date and time
char *datetime = ctime(¤t); //Converting the date and time to string
nob = write(file_descriptor, "----Session----\n\n"); //Writing text to the file through file descriptors
nob = write(file_descriptor, "Date/Time: %s\n\n", datetime); //Writing text to the file through file descriptors
nob = write(file_descriptor, "Text: %s", text); //Writing text to the file through file descriptors
nob = write(file_descriptor, "\n\n\n\n"); //Writing text to the file through file descriptors
}
このプログラムには 3 つの主な問題があります。
Visual Studio は、ソース ファイルを開くことができないと言っています
<unistd.h>
(そのようなファイルやディレクトリはありません)。識別子
open
が定義されていません。識別子
write
が定義されていません。
これらの問題を解決するにはどうすればよいですか? Windows 7 プラットフォームで Visual Studio 2010 を使用しています。プログラムでファイル記述子を利用したいと考えています。