- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
下面的代码应该将文件的内容转换为大写(如果需要转换,它会将原始字符替换为大写)。该代码适用于 Mac OS 和 Linux。但是在 Windows 上,转换在第二个字母处停止并将第二个字母(大写)写入文件中,永不结束。
例子
源数据:阿斯达夫
结果:ASSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS...
我不明白,为什么它在其他平台上工作,但适用于 Windows。
我非常感谢任何解释。
问候, eljobso
#include <stdio.h>
#include <stdlib.h>
int to_upper_file(FILE *fp)
{
char ch = ' ';
if (fp == NULL)
{
perror("Unable to open file");
exit(0);
}
else
{
while (ch != EOF)
{
ch = fgetc(fp);
if ((ch >= 'a') && (ch <= 'z'))
{
ch = ch - 32;
fseek(fp, -1, SEEK_CUR);
fputc(ch, fp);
}
}
}
fclose(fp);
return 0;
}
int main(void)
{
FILE *fp;
int status;
char filename[20];
printf("Enter filename:");
scanf("%s", filename);
fp = fopen(filename, "r+");
if(!fp)
{
printf("File error.\n");
exit(0);
}
else
{
status = to_upper_file(fp);
if (status == 0)
{
printf("Conversion success.\n");
}
if (status == -1)
{
printf("Conversion failure.\n");
}
}
return 0;
}
最佳答案
由于对 FILE *
操作的缓冲性质,在读写或读写之间切换时有必要执行文件定位操作或(对于后者)仅刷新.
来自MSDN documentation to fopen()
:
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch from reading to writing, the input operation must encounter an EOF marker. If there is no EOF, you must use an intervening call to a file positioning function. The file positioning functions are fsetpos, fseek, and rewind. When you switch from writing to reading, you must use an intervening call to either fflush or to a file positioning function.
要让您的代码满足此规则,您可能需要在放置字符后添加一个 0
-seek,如下所示:
fseek(fp, -1, SEEK_CUR);
fputc(ch, fp);
fseek(fp, 0, SEEK_CUR);
或者你也可以刷新文件:
fseek(fp, -1, SEEK_CUR);
fputc(ch, fp);
fflush(fp);
还应该提到的是,应该对所有相关的系统调用进行错误检查。
更新:
来自 C11 标准(7.21.5.3/7 的 fopen
函数)(斜体 由我):
When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters endof- file. [...]
关于c - 文件崩溃导致的 Windows 大写转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23854501/
请帮助我..我被困在这里.. 我真正想要的是检查密码是否重复单个字符或数字。 重复要求 aaaa = 假, abbb = 假 abag = 假 a33f = 假 abcd1234 = 真 一个字符在密
在 Ant 中,我有一个名为 ' some_property 的属性。 ',假设它的值为“hello”。 我正在尝试 替换文本文件中的占位符 将此属性的值(“ hello ”)作为 大写 . 所以,我
var temp=["dy34","fd","FD","av","AV","12esu",1,"DY34",1011,123,101]; 当我对数组进行排序时,我得到的结果是: result = [1
var font_name = $(".font").val(); 我的 JavaScript 代码中有这个。在我的 html 中,我有一个带有 .font 类的输入表单。 我想将 .font 中每个
有人可以解释一下我如何实现文本转换:大写; sIFR 3 的功能? 在文档上,它解释了如何使用它,但我真的不知道如何实现它。有人可以举个例子吗? Specifes text transformatio
这个问题已经有答案了: String contains - ignore case [duplicate] (5 个回答) 已关闭 3 年前。 我需要定义我的查询是 DDL 还是 DML。为此,我需要
所以我一直在用 Java 编写一小段代码,它从用户那里获取输入,计算大写字母、小写字母和其他部分(例如空格、数字,甚至括号),然后返回每个部分的数量用户。 我遇到的问题是,如果我输入“Hello Th
在土耳其语中,有两个 i 无点:ı,I 虚线:i,© 问题:每次我将 i 大写时,我得到的是 I。 当我将 i 大写时,我想得到一个 İ(仅限土耳其语),以及一个I 当我将 ı 大写时。 我有一个函数
使用点符号向数组添加属性是否会将其更改为对象? var arr = []; arr.something = "test"; 是数组吗? 我不这么认为,但 underscore.js 说是 consol
我希望 TextBlock、Label、MenuItem.Header 中的所有文本都以大写显示。字符串取自 ResourceDictionary 例如: 等等。 (也适用于 Label 和其他控
我正在尝试计算包含用户定义范围内所有大写字符的单元格实例的数量,我已经有一些代码可以循环并正确突出显示这些大写单元格,但我正在努力应用该逻辑到VBA 的 Countif 函数。这是我得到的代码,但它给
我正在使用 dplyr 进行数据清理。 我想做的一件事是将某些列中的值大写。 data$surname john Mary John mary ...
为什么低于一个不起作用?我需要转换 IsValue转换成大写值,然后需要用 NO 检查它值(value)。我该怎么做? {{item.IsValue}} 最佳答案 确保 uppercase在比较
我需要将相同的变量转换为大写|小写|大写。 /** * @package ${1 default="Hello"} * @subpackage ${com}_${1 capitalize
显然,拉丁字母表也是如此。但我是在概念上提出这个问题,跨越语言和 Unicode 规范。 实际上,这是为了比较两个字符串。如果你已经知道它们的字节数不同——在所有语言中——你能认为这足以保证它们不是同
今天我更新了我的 Octopress 博客,当我运行时: rake new_post["This is a test of title"] 它在 source/_post/2013-02-18-thi
除了大写部分之外,我的程序正在运行:以下是将英语单词 englishWord 翻译为 Pig 拉丁语单词 pigLatinWord 的方法:A。如果英语单词中没有元音,那么pigLatinWord就是
通过使用一个输入文本框,输入类型只允许字母。输入的值为'a',它应该在文本框外显示为'A'? 如果我们在输入文本中输入小写字母“a”,那么它会希望在框外显示大写字母“A”...以下是我的html代码:
我正在开发一个特殊的脚本来修复文本区域内的字母。我在堆叠时发现了一个问题。在 Stackoverflow 上,我找不到解决方案,需要帮助。 我的脚本有一个系统,用于识别每个以大写首字母开头的单词,以及
我在 bash 中有以下行: echo "Manufacturer: $(echo ${family:-$name}|cut -d' ' -f1)" 我想使用 ${var^} 语法将回显字符串大写,但
我是一名优秀的程序员,十分优秀!