OS X 优胜美地 10.10.5 XCode 7.2
我整天都在阅读和试验关于 C 中的宽字符/字符串,但我仍然无法让它工作。
我正在尝试读取仅由宽字符组成的文件,如下所示:
んわらやま (Japanese)
我想一次只读取一个字符,然后立即写入另一个文件。
int main(int argc, const char * argv[])
{
FILE *source, *dest;
source = fopen( argv[1], "r");
if (source == NULL) {
printf ("could not open source file \n");
exit (1);
}
// if [dest] does not exist it is created
dest = fopen( argv[2], "w+");
if (dest == NULL) {
fclose(source);
printf ("could not open dest file \n");
exit (1);
}
fwide(source, 1);
fwide(dest, 1);
fileManipulator(source, dest);
fclose(source);
fclose(dest);
return 0;
}
void fileManipulator(FILE* source, FILE* dest)
{
wint_t token;
while ( WEOF != (token = getwc(source))) {
manipulateToken(token, dest);
}
}
void manipulateToken(wint_t token, FILE* dest)
{
char* pre = "- ";
char* post= " -\n";
if ( EOF == fputs(pre, dest))
{
// error handling
}
if ( WEOF == fputwc(token, dest))
{
// error handling
}
if ( EOF == fputs(post, dest))
{
// error handling
}
}
这是输出:
- „ -
- Ç -
- ì -
- „ -
- Ç -
- è -
- „ -
- Ç -
- â -
- „ -
- Ç -
- Ñ -
- „ -
- Å -
- æ -
我可以理解我的问题可能与我如何读取数据有关,但如果我考虑替代方案,我会完全陷入困境。
- 我试过使用fgetws但我无法将字符彼此分开;
- 我曾尝试将fwscanf与%ls一起使用,但最终得到了一个空文件;
- 我注意到 MAC OS 不提供fgetwc实现,即使相关手册页提到它,AFAIK getwc应该是 fgetwc 的宏实现;
- 不确定这是否重要,但我使用touch命令创建了源文件;
你能帮助我吗?
PS:也非常感谢有关该论点的进一步阅读的链接。关于此事的文档非常稀缺。
XCode 问题
这个问题最初让我认为Jonathan Leffler解决方案不起作用。事实上,如果我通过 XCodeCMD+R或通过Terminal运行代码,它会产生不同的输出。
AFAIK问题一定是XCode在运行时使用的某种属性/属性/设置,因为硬编码源和目标参数仍然会产生错误的输出。
为了清楚起见,我为我的代码提供了导出的方案:
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "DA36663A1CCF4F8200615958"
BuildableName = "FileManipulator"
BlueprintName = "FileManipulator"
ReferencedContainer = "container:FileManipulator.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "DA36663A1CCF4F8200615958"
BuildableName = "FileManipulator"
BlueprintName = "FileManipulator"
ReferencedContainer = "container:FileManipulator.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
enableAddressSanitizer = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "DA36663A1CCF4F8200615958"
BuildableName = "FileManipulator"
BlueprintName = "FileManipulator"
ReferencedContainer = "container:FileManipulator.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "/Users/Paul/TestDirectory/Source.txt"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "/Users/Paul/TestDirectory/Destination.txt"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
<AdditionalOptions>
<AdditionalOption
key = "NSZombieEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
<AdditionalOption
key = "NSDOLoggingEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "DA36663A1CCF4F8200615958"
BuildableName = "FileManipulator"
BlueprintName = "FileManipulator"
ReferencedContainer = "container:FileManipulator.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>