我有一个 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
}
该程序存在三个主要问题:
Visual Studio 告诉我它无法打开源文件
<unistd.h>
(没有这样的文件或目录)。标识符
open
未定义。标识符
write
未定义。
请问我该如何解决这些问题?我在 Windows 7 平台上使用 Visual Studio 2010。我想在我的程序中使用文件描述符。