2012-11-02 75 views
2

我有一个C++程序,它接受用户的一些文本并将其保存到文本文件中。以下是该计划的片段:在Visual Studio 2010和Windows中使用文件描述符

#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(&current); //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 
} 

有三个主要问题与此程序:

  1. Visual Studio是告诉我它无法打开源文件<unistd.h>(没有这样的文件或目录)。

  2. 标识符open未定义。

  3. 标识符write未定义。

请问我该如何解决这些问题?我在Windows 7平台上使用Visual Studio 2010。我想在我的程序中使用文件描述符。

+0

这似乎是C++。重新标记它。 – alk

+0

这不是POSIX的'write'函数。 –

回答

2

打开和写入是(Unix)平台特定的。文件访问的C标准方式是FILE *,fopen和fwrite。

如果你想使用open/write,你应该看看http://msdn.microsoft.com/en-us/library/z0kc8e3z(v=vs.100).aspx。微软已经增加了对open/write的支持,但将(非C标准)函数重新命名为_open/_write。

+0

谢谢。有没有办法在Windows中使用它们? – Matthew

+0

好的。感谢您的快速回复:) – Matthew

+0

是的,但误导。 Visual C++附带了这些函数的实现,无需编写自己的函数。 –

-2

如果您想使用Windows下此代码,而无需改变,尝试Cygwin的:http://www.cygwin.com/

但是,它是更好的使用C库文件中的函数改写这个代码,在另一个答案已经建议。这可以在任何操作系统中使用。

+1

Visual C++提供了这些函数,不需要cygwin。所有需要的是使用正确的标题。其余的答案只是重复别人说的话;这没有帮助。 –

5

Visual C++更喜欢这些函数的符合ISO的名称:_open_write。但POSIX名称openwrite工作得很好。

您需要#include <io.h>才能访问它们。

此外,您的代码没有正确使用write函数。你似乎认为这是printf的另一个名字,POSIX不同意。


此代码在Visual C++中编译得很好。

#include <time.h> 
#include <io.h> 
#include <fcntl.h> 

int file_descriptor; 
size_t nob; 

int check_file(const char* full_path) //Method to check whether a file already exists 
{ 
    return 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) // Function to write a binary time_t to a previously opened file 
{ 
    time_t current = time(0); //Getting the current date and time 

    nob = write(file_descriptor, &current, sizeof current); 
} 

如果你创建一个unistd.h文件,其中包含#include <io.h>,然后贴到您的系统包含路径,那么你将不需要任何代码更改任何(假设你的代码是符合POSIX标准开始与)。

+0

-1 - _open和_write未打开并写入。 –

+0

@亚历克斯:是的。 [旧](http://msdn.microsoft.com/en-US/library/ms235491.aspx)[名称](http://msdn.microsoft.com/en-US/library/ms235320.aspx)是甚至转发给新的代码,尽管在新代码中建议使用ISO名称。 –

+0

在没有改变的情况下在Windows中编译此代码的唯一方法是使用Cygwin,就像使用Linux系统调用的任何其他代码一样。您的答案建议通过更改函数名称和包含文件来更改代码。 –