如何正确处理C中的文件读/写错误?

时间:2013-06-23 19:35:05

标签: python c

我想重写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的新手,我不想承担制造错误异常处理程序的风险。

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分: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();
    }