I have a program in C++ which accepts some text from the user and saves it to a text file. Here are snippets of the program:
#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
}
There are three main problems with this program:
-
Visual Studio is telling me that it cannot open source file
<unistd.h>(no such file or directory). -
Identifier
openis undefined. -
Identifier
writeis undefined.
How can I solve these problems please? I am using Visual Studio 2010 on a Windows 7 platform. I would like to make use of file descriptors in my program.
openandwriteare (Unix) platform specific. The C standard way for file access isFILE*,fopenandfwrite.If you stil want to use
open/writeyou should take a look at http://msdn.microsoft.com/en-us/library/z0kc8e3z(v=vs.100).aspx. Microsoft has added support for open/write, but renamed the (non-C-standard) functions to_open/_write.