1

我想重写yEnc代码,使其可在 Win32 上使用 Visual Studio 2008 进行编译。

问题是 yEnc 使用unistd.h (UNIX) 函数fcntl来检查文件是否可读或可写。它当然与 MS Visual Studio 不兼容。

这是我要删除的内容:

static Bool writable(FILE *file)
{
    int mode = fcntl(fileno(file),F_GETFL) & O_ACCMODE;
    return (mode == O_WRONLY) || (mode == O_RDWR);
}

static Bool readable(FILE *file)
{
    int mode = fcntl(fileno(file),F_GETFL) & O_ACCMODE;
    return (mode == O_RDONLY) || (mode == O_RDWR);
}

这是它的名称:

FILE* infile = PyFile_AsFile(Py_infile);
FILE* outfile = PyFile_AsFile(Py_outfile);

if(!readable(infile) || !writable(outfile) ) {
    return PyErr_Format(PyExc_ValueError, "file objects not writeable/readable");
} 

/* File stuff including */    
fread(&read_buffer, 1, in_ind, infile);
if(ferror(infile) || ferror(outfile)) {
    return PyErr_Format(PyExc_IOError, "I/O Error while encoding");
}
fputc(CR, outfile);
fputc(LF, outfile);
fflush(outfile);
/* End of file stuff */

有人可以帮我转换这个可读/可写的检查(用相当于 try {} catch {} 代替)吗?

我相信处理文件读/写错误比试图知道 Windows 文件是否可读/可写更容易,因为 fcntl/F_GETFL 似乎没有简单的 Windows 等效项。

解决方案似乎并不复杂,但由于我是 C 和 Python 的新手,我不想冒险制作一个有问题的异常处理程序。

谢谢你的帮助。

4

3 回答 3

0

最后,我认为以下检查就足够了:

infile == NULL
outfile == NULL 
fread != read_count
fwrite != write_count
ferror

这应该足够了。此外,该文件已首先在 Python 中打开,我认为该文件打开已经过异常测试。

于 2013-06-23T20:27:35.850 回答
0

您不必转换它只需安装 windows POSIX。 http://www.cygwin.com/

于 2013-06-23T19:42:34.073 回答
0
{
    public string writepath;
    public string readpath;
    public string line;
    public List<Reciepient> reciepients = new List<Reciepient>();//linking my ist from my rec class
    public int index;

    public Form1()
    {
        InitializeComponent();
        writebutton.Enabled = false;//disables write button
    }

    public void createbutton_Click(object sender, EventArgs e)
    {
        writebutton.Enabled = true;//enables write button
        folderBrowserDialog1.ShowDialog();//open folder browser
        writepath = folderBrowserDialog1.SelectedPath+ @"\test.txt";//generate path
        StreamWriter sw = new StreamWriter(writepath);//open new sw
        textBox1.Text = writepath;//write path to textbox1
        sw.Close();//close my sw
    }

    public void readbutton_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();//open my file browser
        readpath = openFileDialog1.FileName;//grabbing file name
        StreamReader sr = new StreamReader(readpath);//creating new sr
        textBox2.Text = readpath;//putting readpath in textbox2

        while(sr.Peek()!= -1)//will stop reading if noo more lines
        {
            line = sr.ReadLine();//tells to read lines listed
            Console.WriteLine(line);
            /*if (line.Length > 0)
                continue;  messing up!!
            else
                MessageBox.Show("Line Cannot be Read");
        */
                string fname = line.Substring(0, 5);
                string lname = line.Substring(6, 6);
                int loccode = Int32.Parse(line.Substring(13, 1));
                int wcode = Int32.Parse(line.Substring(15, 1));
                double itemcost = double.Parse(line.Substring(17, 5));
                int acode = Int32.Parse(line.Substring(23, 1));

        Reciepient file = new Reciepient(fname, lname, loccode, wcode, itemcost,
        acode);
        reciepients.Add(file);//add to list

        }

        sr.Close();//closes streamreader
    }

    public void writebutton_Click(object sender, EventArgs e)
    {
        StreamWriter sw = new StreamWriter(writepath);
        Console.WriteLine(reciepients.Count);
        for (int index = 0; index < reciepients.Count(); index++)
        {
            Reciepient file = reciepients.ElementAt(index);
            sw.WriteLine(file.fname + " " + file.lname +" "+ "="+ file.totalcost);
        }
        sw.Close();
    }
于 2016-06-23T04:06:35.953 回答