none
如何在windows8应用中像c++一样处理文件? RRS feed

  • 问题

  • 有这样一段c++代码:

    #include "stdio.h" int main(){ FILE *in,*out; in=fopen("d:\\a.txt","r"); out=fopen("d:\\b.txt","w"); int a; fscanf(in,"%d",&a); fprintf(out,"%d",a);

    }

    请问如何在windows8应用(XAML)中实现打开文件d:\\a.txt和并进行读写?

    (我曾经尝试直接写这段代码,结果in和out是NULL)


    2014年4月3日 13:01

答案

  • ?????

    2014年4月3日 13:29
  • 首先先确认你有d盘读写权限

    最好用"rb"

    代码没什么问题,如果还是NULL,用GetLastError或者errno看看是什么错


    0xDEADBEEF

    2014年4月7日 1:11
  • Hi lxylxy123456,

    一般来说你可以在MSDN上找到对应的例子。你的问题可以分解为两个步骤:

    第一部分:如何通过文件读取器打开文件

    原理: http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh771180.aspx 

    具体实现方法:

    FileOpenPicker^ openPicker = ref new FileOpenPicker(); 
    openPicker->ViewMode = PickerViewMode::Thumbnail; 
    openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary; 
    openPicker->FileTypeFilter->Append("*"); 
       
    create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file) 
    {
        if (file) 
        { 
            m_PickedFileName = file->Name; 
        } 
        else 
        { 
            // Operation cancelled.
        } 
    });

    例子:http://code.msdn.microsoft.com/windowsapps/File-picker-sample-9f294cba/

    第二块:如何读写文件

    原理:http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh758325.aspx

    例子: http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

    如还有疑问请提出来,ps这种问题可以到Windows Store App论坛提出来:)

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    2014年4月7日 15:19
    版主
  • 编译有问题是指?

    StorageFile^ file = rootPage->SampleFile;
        if (file != nullptr)
        {
            String^ userContent = InputTextBox->Text;
            if (userContent != nullptr && !userContent->IsEmpty())
            {
                create_task(FileIO::WriteTextAsync(file, userContent)).then([this, file, userContent](task<void> task)
                {
                    try
                    {
                        task.get();
                        rootPage->NotifyUser("The following text was written to '" + file->Name + "':\n" + userContent, NotifyType::StatusMessage);
                    }
                    catch (COMException^ ex)
                    {
                        rootPage->HandleFileNotFoundException(ex);
                    }
                });
            }
            else
            {
                rootPage->NotifyUser("The text box is empty, please write something and then click 'Write' again.", NotifyType::ErrorMessage);
            }
        }
        else
        {
            rootPage->NotifyUserFileNotExist();
        }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年4月9日 1:07
    版主

全部回复

  • ?????

    2014年4月3日 13:29
  • 首先先确认你有d盘读写权限

    最好用"rb"

    代码没什么问题,如果还是NULL,用GetLastError或者errno看看是什么错


    0xDEADBEEF

    2014年4月7日 1:11
  • 那么如何通过文件选取器打开这个文件并读写?
    2014年4月7日 12:32
  • Hi lxylxy123456,

    一般来说你可以在MSDN上找到对应的例子。你的问题可以分解为两个步骤:

    第一部分:如何通过文件读取器打开文件

    原理: http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh771180.aspx 

    具体实现方法:

    FileOpenPicker^ openPicker = ref new FileOpenPicker(); 
    openPicker->ViewMode = PickerViewMode::Thumbnail; 
    openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary; 
    openPicker->FileTypeFilter->Append("*"); 
       
    create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file) 
    {
        if (file) 
        { 
            m_PickedFileName = file->Name; 
        } 
        else 
        { 
            // Operation cancelled.
        } 
    });

    例子:http://code.msdn.microsoft.com/windowsapps/File-picker-sample-9f294cba/

    第二块:如何读写文件

    原理:http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh758325.aspx

    例子: http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

    如还有疑问请提出来,ps这种问题可以到Windows Store App论坛提出来:)

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    2014年4月7日 15:19
    版主
  • 你好:

    根据你问题的描述,为了得到更好的回应,我把这个帖子移到Windows应用商店应用论坛。

    感谢你的理解。


    • 已编辑 Anna Cc 2014年4月8日 2:11
    2014年4月8日 2:10
  • 能不能将第二部分的C++代码给我?谢谢!
    2014年4月8日 6:57
  • 您可以去下载一下例子http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597 帮助你更深入的理解 :)

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年4月8日 8:12
    版主
  • 这个例子编译有问题,能不能把关键的代码写一下?
    2014年4月8日 9:29
  • 编译有问题是指?

    StorageFile^ file = rootPage->SampleFile;
        if (file != nullptr)
        {
            String^ userContent = InputTextBox->Text;
            if (userContent != nullptr && !userContent->IsEmpty())
            {
                create_task(FileIO::WriteTextAsync(file, userContent)).then([this, file, userContent](task<void> task)
                {
                    try
                    {
                        task.get();
                        rootPage->NotifyUser("The following text was written to '" + file->Name + "':\n" + userContent, NotifyType::StatusMessage);
                    }
                    catch (COMException^ ex)
                    {
                        rootPage->HandleFileNotFoundException(ex);
                    }
                });
            }
            else
            {
                rootPage->NotifyUser("The text box is empty, please write something and then click 'Write' again.", NotifyType::ErrorMessage);
            }
        }
        else
        {
            rootPage->NotifyUserFileNotExist();
        }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年4月9日 1:07
    版主
  • 我用的是windows8.1,visual studio 2013,在打开项目前说需要单向升级,升级后似乎有一些不成功。编译后的错误:

    错误 1 error C2039: “Scenario1”: 不是“SDKSample::FileAccess”的成员 c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 96
    错误 2 error C2061: 语法错误: 标识符“Scenario1” c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 96
    错误 3 error C2039: “Scenario3”: 不是“SDKSample::FileAccess”的成员 c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 108
    错误 4 error C2061: 语法错误: 标识符“Scenario3” c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 108
    错误 5 error C2039: “Scenario4”: 不是“SDKSample::FileAccess”的成员 c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 120
    错误 6 error C2061: 语法错误: 标识符“Scenario4” c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 120
    错误 7 error C2039: “Scenario5”: 不是“SDKSample::FileAccess”的成员 c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 132
    错误 8 error C2061: 语法错误: 标识符“Scenario5” c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 132
    错误 9 error C2039: “Scenario6”: 不是“SDKSample::FileAccess”的成员 c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 144
    错误 10 error C2061: 语法错误: 标识符“Scenario6” c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 144
    错误 11 error C2039: “Scenario7”: 不是“SDKSample::FileAccess”的成员 c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 156
    错误 12 error C2061: 语法错误: 标识符“Scenario7” c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 156
    错误 13 error C2039: “Scenario8”: 不是“SDKSample::FileAccess”的成员 c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 168
    错误 14 error C2061: 语法错误: 标识符“Scenario8” c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 168
    错误 15 error C2039: “Scenario10”: 不是“SDKSample::FileAccess”的成员 c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 180
    错误 16 error C2061: 语法错误: 标识符“Scenario10” c:\users\13691_000\downloads\c++\windows\generated files\xamltypeinfo.g.cpp 180

    2014年4月26日 9:39
  • 能不能把读文件的代码也写一下?或者看一看这个代码为什么错了?

    auto openPicker = ref new Windows::Storage::Pickers::FileOpenPicker();
    openPicker->FileTypeFilter->Append("*");
    create_task(openPicker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file)
    {
    	if (file != nullptr){
    		auto userContent = Windows::Storage::FileIO::ReadTextAsync(file);
    		text->Text = userContent->ToString();
    	}
    });
    还有,如何在已有文件的后面写?

    2014年4月26日 10:41