如何用数字替换文件中连续出现的字符 [英] How to replace successive occurrences of characters in a file by the number
问题描述
在我的代码中,我成功打开了文件,但是现在我不明白如何使用read()函数执行循环以获取file1
中的所有字符.我设置了一个大小为8的缓冲区,但是文件内容更多.
谢谢.还请使用类似的syscall来帮助我做到这一点
您在char
和指向char
的指针之间感到困惑:
char *buffer[4096];
是一个指向chars的指针数组,一个应该保存指针,而不是chars
.您想要的是一个char
:
char buffer[4096];
现在,当添加\0
时,将出现编译器错误.这也意味着它包含一个字符串,这意味着您可以对printf
使用%s
转换来打印它,不涉及循环,只需执行以下操作即可:
printf("%s", buffer);
另外,像这样执行您的read
(同样不需要循环):
ssize_t count = read(fd1, buffer, sizeof buffer - 1);
if(count < 0)
return -1;
这将最多读取4095个字节到您的数组中,具体取决于文件的大小.这为您的\0
留出了空间.
因此,要确保您已读取所有文件,请执行以下操作:
ssize_t count;
while((count = read(fd1, buffer, sizeof buffer - 1)) != 0)
{
if(count < 0)
return -1;
buffer[count] = '\0';
printf("%s", buffer);
// Do your processing here
}
这个想法是从文件中读取一个块,然后根据需要对其进行处理,然后读取另一个块等.到达文件末尾时,read
将返回0
,并且您的处理将停止. /p>
执行此循环的一种等效方法可能更容易理解:
ssize_t count = read(fd1, buffer, sizeof buffer - 1);
while(count != 0)
{
// .....
count = read(fd1, buffer, sizeof buffer - 1);
}
这使您更清楚地知道直到count
为零为止.我使用的技巧是将count = read(...)
部分放在圆括号内.评估括号(count = read(...))
中的内容的结果是赋值的结果(即,被赋给count
的内容,这是read
的结果).将其放在while
语句中意味着括号中的部分首先被求值(即,它执行read
并分配count
).然后检查赋值的结果(即count
)是否为零.
In my code, I successfully opened the file, but now I don't understand how to perform a loop using the read() function, to get all the characters in file1
. I set a buffer of size 8, but the file contents are more.
Thanks. Also please help me do this using syscalls like that
You are getting confused between a char
and a pointer to a char
:
char *buffer[4096];
Is an array of pointers to chars, an is supposed to hold pointers, not chars
. What you want is an array of char
:
char buffer[4096];
Now when you add the \0
you will get a compiler error. This also means that it contains a string, meaning you use the %s
conversion for printf
to print it, no loop involved, just do:
printf("%s", buffer);
Also, do your read
like this (again no loop needed):
ssize_t count = read(fd1, buffer, sizeof buffer - 1);
if(count < 0)
return -1;
This will read up to 4095 bytes into your array, depending on the size of the file. This leaves room for your \0
.
So to make sure you read all your file, do something like:
ssize_t count;
while((count = read(fd1, buffer, sizeof buffer - 1)) != 0)
{
if(count < 0)
return -1;
buffer[count] = '\0';
printf("%s", buffer);
// Do your processing here
}
The idea is to read a chunk from the file, then process it as required, then read another chunk etc. When the end of the file is reached, read
will return 0
and your processing will stop.
An equivalent way to do this loop, which may be easier to understand is:
ssize_t count = read(fd1, buffer, sizeof buffer - 1);
while(count != 0)
{
// .....
count = read(fd1, buffer, sizeof buffer - 1);
}
This makes it more clear that you are looping until count
is zero. The trick I have used is to take the count = read(...)
part and put it inside the parentheses. The result of evaluating what is inside the parentheses (count = read(...))
, is the result of the assignment (ie what gets assigned to count
, which is the result of read
). Putting this inside while
statement means the part in parentheses gets evaluated first (ie it does the read
and assigns count
). The the result of the assignment (ie count
) then gets checked to see if it is zero.
这篇关于如何用数字替换文件中连续出现的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!