- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在编写一个简单的程序来翻转文件中的所有位,但现在它只处理前 1000 个字节,直到我完成那么多工作。为什么我调用 read() 会忽略\r 字符?当我在仅包含\r\n\r\n 的文件上运行此代码时,读取调用返回 2 并且缓冲区包含\n\n。\r 字符被完全忽略。我在 Windows 上运行它(这在 Linux 机器上什至不是问题)
为什么 read(2) 在找到\r 字符时会跳过它?或者这就是正在发生的事情?
编辑:结论是 Windows 默认以“文本”模式而不是“二进制”模式打开文件。为此,调用open时,必须指定O_BINARY为模式。
谢谢,下面的代码。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
void invertBytes(size_t amount, char* buffer);
int main(int argv, char** argc)
{
int fileCount = 1;
char* fileName;
int fd = 0;
size_t bufSize = 1000;
size_t amountRead = 0;
char* text;
int offset = 0;
if(argv <= 1)
{
printf("Usages: encode [filenames...]\n");
return 0;
}
text = (char *)malloc(sizeof(char) * bufSize);
for(fileCount = 1; fileCount < argv; fileCount++)
{
fileName = argc[fileCount];
fd = open(fileName, O_RDWR);
printf("fd: %d\n", fd);
amountRead = read(fd, (void *)text, bufSize);
printf("Amount read: %d\n", amountRead);
invertBytes(amountRead, text);
offset = (int)lseek(fd, 0, SEEK_SET);
printf("Lseek to %d\n", offset);
offset = write(fd, text, amountRead);
printf("write returned %d\n", offset);
close(fd);
}
return 0;
}
void invertBytes(size_t amount, char* buffer)
{
int byteCount = 0;
printf("amount: %d\n", amount);
for(byteCount = 0; byteCount < amount; byteCount++)
{
printf("%x, ", buffer[byteCount]);
buffer[byteCount] = ~buffer[byteCount];
printf("%x\r\n", buffer[byteCount]);
}
printf("byteCount: %d\n", byteCount);
}
最佳答案
fd = open(fileName, O_RDWR);
应该是
fd = open(fileName, O_RDWR | O_BINARY);
关于c - read(2) 如何与回车交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8764046/
这里是新的,通常在 VBA 中。我创建了一个宏,它可以复制 excel 中单元格的内容并粘贴到 word 文档中的特定位置。仅供引用,我使用 word 中的书签来选择粘贴的确切位置。问题是复制的所有内
我正在阅读有关字符串文字的 MySql 文档,并尝试使用回车符进行以下操作: 选择“我想要一个\r bannana”; 为什么会返回: +--------------------+ bannana
我正在运行以下代码: #include #include #include int main(){ FILE *fp; if((fp=fopen("test.txt","r"))==N
我一直想知道为什么在运行每个应用程序时命令提示符和Powershell都预先装载了这么多回车符。您可以永远滚动(好吧,不是真的,而是几十个,如果不是数百个页面长度的空白空间)。我现在没有Unix机器,
回车、换行、换行有什么区别?它取决于操作系统吗?为什么我们需要使用所有这些来到达下一行? 最佳答案 通常,“新行”是指通常被解释为表示新行的任何字符集,其中可以包括: CR LF(在 DOS/Wind
#include #include #include struct st{ char n[100]; //Name char
我对 php/mysql 比较陌生。我正在尝试对这部分进行回车: echo "" . nl2br($row['dish_description']) . ""; “dish_description”包
我正在阅读一个普通的文本文件,并将所有单词作为数字写入另一个文本。当一行结束时,它会查找“换行符 (\n)”并从新行继续。在 Ubuntu 中它可以完美执行,但在 Windows (DevC++) 中
Python Docx 是一个非常好的库,用于为不直接处理所有 COM 内容的内容生成 Microsoft Word 文档。尽管如此,我还是遇到了一些限制。 有谁知道如何在一串文本中加入回车符? 我希
我正在使用 CLion IDE,我正在尝试执行回车。 我正在用 C 语言执行打印语句,语法如下: printf("\rHello World!"); 这是一个循环。循环仍然在自己的行上打印每个 Hel
我正在尝试使用正则表达式来匹配字符串和 \r 之间的每个字符特点 : text = 'Some text\rText to find !\r other text\r' 我要配'Text to fin
使用 knockout.js,如何在绑定(bind)到段落文本属性的文本中包含回车 元素。 在我的 ViewModel 中,我生成了一个绑定(bind)到 的文本字符串。在 View 中。我想在浏览
# make test cc test.c -o test # ./test I am a happy yam until i hit return... # ./test because
我正在使用 Moodle 2.2.1,但有一个基本的 php-mysql 问题。我试图返回 WHERE 子句中带有条件的字段的计数。 WHERE 子句条件来自 foreach 循环。这是我的代码:
我在@Html.TextBoxFor 中显示一些长文本。文本包含换行符、回车符等,但@Html.TextBoxFor 似乎不尊重这些。有什么我可能遗漏的想法吗? 最佳答案 文本框每个定义单行。 您必须
隔离问题一小时后,我可以发布这个:) 我的本地文件有问题,但在生产环境中没有。唯一的区别是在生产环境中,html 文件是内联的。 我正在使用默认的 bootstrap 3.3.5 和这个 css
在过滤选项后面加上: \t[|] \ r[|] 也就是C语言中的回车和空格 附上C语的其他转义字符以便急之需: \a 响铃(BEL) 007 \b 退格(BS) 008 \f 换页(F
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章java去除字符串中的空格、回车、换行符、制表符的小例子由作者收集整理,
我正在尝试编写一份将在 Excel 中发送的报告,该报告将两条记录连接到同一单元格中,并在每条记录之间显示回车符。我一直在尝试 ||chr(10) || chr(13) ||但这仅在我作为脚本运行时有
我的 fopen 是这样设置的。我尝试过使用“t”和不使用“t”的 fopen。由于某种原因,我的 fprintf 打印出 ^M ,它们是回车符。如何阻止 frpintf 这样做?我想只使用普通的换行
我是一名优秀的程序员,十分优秀!