文件描述符的代码存在问题。 C (Linux)
我编写的代码理想情况下应该从一个文档中获取数据,对其进行加密并将其保存在另一个文档中。
但是当我尝试执行该代码时,它不会将加密的数据放入新文件中。它只是将其留空。有人请找出代码中缺少的内容。我尝试过,但我无法弄清楚。
我认为读/写函数有问题,或者我可能错误地实现了 do-while 循环。
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
int fdin,fdout,n,i,fd;
char* buf;
struct stat fs;
if(argc<3)
printf("USAGE: %s source-file target-file.\n",argv[0]);
fdin=open(argv[1], O_RDONLY);
if(fdin==-1)
printf("ERROR: Cannot open %s.\n",argv[1]);
fdout=open(argv[2], O_WRONLY | O_CREAT | O_EXCL, 0644);
if(fdout==-1)
printf("ERROR: %s already exists.\n",argv[2]);
fstat(fd, &fs);
n= fs.st_size;
buf=malloc(n);
do
{
n=read(fd, buf, 10);
for(i=0;i<n;i++)
buf[i] ^= '#';
write(fd, buf, n);
} while(n==10);
close(fdin);
close(fdout);
}
I've written code that should ideally take in data from one document, encrypt it and save it in another document.
But when I try executing the code it does not put the encrypted data in the new file. It just leaves it blank. Someone please spot what's missing in the code. I tried but I couldn't figure it out.
I think there is something wrong with the read/write function, or maybe I'm implementing the do-while loop incorrectly.
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
int fdin,fdout,n,i,fd;
char* buf;
struct stat fs;
if(argc<3)
printf("USAGE: %s source-file target-file.\n",argv[0]);
fdin=open(argv[1], O_RDONLY);
if(fdin==-1)
printf("ERROR: Cannot open %s.\n",argv[1]);
fdout=open(argv[2], O_WRONLY | O_CREAT | O_EXCL, 0644);
if(fdout==-1)
printf("ERROR: %s already exists.\n",argv[2]);
fstat(fd, &fs);
n= fs.st_size;
buf=malloc(n);
do
{
n=read(fd, buf, 10);
for(i=0;i<n;i++)
buf[i] ^= '#';
write(fd, buf, n);
} while(n==10);
close(fdin);
close(fdout);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在 fstat、读取和写入系统调用中使用 fd 而不是 fdin。 fd 是一个未初始化的变量。
You are using fd instead of fdin in fstat, read and write system calls. fd is an uninitialized variable.
您正在读取和写入
fd
,而不是fdin
和fdout
。确保启用编译器将发出的所有警告(例如使用gcc -Wall -Wextra -pedantic
)。如果您允许,它会警告您使用未初始化的变量。此外,如果您检查
fstat()
、read()
或write()
的返回码,您可能会收到错误使用无效的文件描述符。他们很可能会因 EINVAL(无效参数)错误而出错。既然我们在这里:分配足够的内存来保存整个文件是不必要的。您在循环中一次仅读取 10 个字节,因此您实际上只需要一个 10 字节的缓冲区。您可以完全跳过
fstat()
。You're reading and writing to
fd
instead offdin
andfdout
. Make sure you enable all warnings your compiler will emit (e.g. usegcc -Wall -Wextra -pedantic
). It will warn you about the use of an uninitialized variable if you let it.Also, if you checked the return codes of
fstat()
,read()
, orwrite()
, you'd likely have gotten errors from using an invalid file descriptor. They are most likely erroring out with EINVAL (invalid argument) errors.And since we're here: allocating enough memory to hold the entire file is unnecessary. You're only reading 10 bytes at a time in your loop, so you really only need a 10-byte buffer. You could skip the
fstat()
entirely.大家都说是真的,还有一个提示。
您应该使用适合系统硬盘块的更大缓冲区,通常为 8192。
这将显着提高您的程序速度,因为您对磁盘的访问次数将减少 800 倍。如您所知,访问磁盘在时间方面非常昂贵。
另一种选择是使用 stdio 函数 fread、fwrite 等,它们已经处理了缓冲,但您仍然会有函数调用开销。
罗尼
All said it true, one more tip.
You should use a larger buffer that fits the system hard disk blocks, usually 8192.
This will increase your program speed significantly as you will have less access to the disk by a factor of 800. As you know, accessing to disk is very expensive in terms of time.
Another option is use stdio functions fread, fwrite, etc, which already takes care of buffering, still you'll have the function call overhead.
Roni