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

2024-06-01 07:35:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

问题是yEnc使用unistd.h(UNIX)函数fcntl检查文件是否可读写。当然,它与msvisualstudio不兼容。在

我想删除的是:

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);
}

它的名称如下:

^{pr2}$

有人能帮我转换这个可读写的检查(改为try{}catch{})吗?

我相信处理文件读/写错误要比试图知道一个Windows文件是否可读写更容易,因为似乎没有简单的Windows等价于fcntl/F\u GETFL。在

这个解决方案看起来并不复杂,但由于我是C和Python的新手,我不想冒风险制作一个有缺陷的异常处理程序。在

谢谢你的帮助。在


Tags: 文件returnmodewindowsstaticfileintbool
3条回答
{
    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();
    }

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

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

这就足够了。此外,该文件已首先在Python中打开,我假定该文件已被测试是否有异常。在

你不必转换它只要安装WindowsPOSIX就可以了。 http://www.cygwin.com/

相关问题 更多 >