积极答复者
如何在windows8应用中像c++一样处理文件?

问题
-
有这样一段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)
- 已编辑 lxylxy123456 2014年4月3日 13:02
- 已移动 Anna Cc 2014年4月8日 2:10
答案
-
首先先确认你有d盘读写权限
最好用"rb"
代码没什么问题,如果还是NULL,用GetLastError或者errno看看是什么错
0xDEADBEEF
- 已标记为答案 Jamles HezModerator 2014年4月14日 14:20
-
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.- 已编辑 Jamles HezModerator 2014年4月7日 15:20
- 已标记为答案 Jamles HezModerator 2014年4月14日 14:20
-
编译有问题是指?
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.- 已标记为答案 Jamles HezModerator 2014年4月14日 14:21
全部回复
-
首先先确认你有d盘读写权限
最好用"rb"
代码没什么问题,如果还是NULL,用GetLastError或者errno看看是什么错
0xDEADBEEF
- 已标记为答案 Jamles HezModerator 2014年4月14日 14:20
-
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.- 已编辑 Jamles HezModerator 2014年4月7日 15:20
- 已标记为答案 Jamles HezModerator 2014年4月14日 14:20
-
您可以去下载一下例子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. -
编译有问题是指?
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.- 已标记为答案 Jamles HezModerator 2014年4月14日 14:21
-
我用的是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 -
能不能把读文件的代码也写一下?或者看一看这个代码为什么错了?
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(); } });
还有,如何在已有文件的后面写?