我有一个任务,要求我使用缓冲I/o复制一个文件。它有多个要求:
- 接受一个参数和一个可选的第二个参数
- 打开读取 的第一个参数
- 打开第二个写入
- 如果没有第二个参数,创建一个名为prog1.out的新文件
- 使用20字节的缓冲区大小
- 复制文件时,打印任何以字符"rwxr"开头的缓冲区
- 退出前关闭所有打开的文件。
我遇到的问题是6号,我四处看了看,还是不明白。我试过memchr,但我觉得我的方向不对。如果有人能帮我指点一下方向,我会很感激的。
这是我的代码:
# include <stdlib.h>
# include <stdio.h>
int main(int argc, char *argv[])
{
FILE *readfile, *writefile;
char buffer[1024];
int fileSize;
int readResult;
int writeResult;
// making sure arguments exist
if (argc < 2|| argc > 3){
printf("This program takes either 1 or 2 arguments.n");
exit(1);
}
//Opening file for reading
readfile = fopen(argv[1], "r");
if (!readfile) {
printf("Unable to open file %s.n", argv[1]);
exit(1);
}
//finding the file size
fseek (readfile, 0, SEEK_END);
fileSize = ftell (readfile);
fseek (readfile, 0, SEEK_SET);
// read the file
readResult = fread(buffer, 20, fileSize/20, readfile);
if (readResult == 0) {
printf("A read error occured.n");
exit(1);
}
//check to see if there is a second parameter (argument)
if (argc == 3) {
writefile = fopen(argv[2], "w");
if (!writefile) {
printf("Unable to open file %s.n", argv[2]);
exit(1);
}
writeResult = fwrite(buffer, 20, fileSize/20, writefile);
if (writeResult != readResult) {
printf("A write error occured.n");
exit(1);
}
printf("File %s successfully copied to %s.n", argv[1], argv[2]);
}
else {
writefile = fopen("program1.out", "w");
if (!writefile) {
printf("Unable to open file program1.outn");
exit(1);
}
writeResult = fwrite(buffer, 20, fileSize/20, writefile);
if (writeResult != readResult) {
printf("A write error occured.n");
exit(1);
}
printf("File %s successfully copied to %s.n", argv[1], "program1.out
}
fclose(readfile);
fclose(writefile);
exit(0);
}
有一种朴素的方法:
if(buffer[0] == 'r' && buffer[1] == 'w'
&& buffer[2] == 'x' && buffer[3] == 'r') {
//do something
}
但是看一下strncmp(),您可以使用它来比较字符串的各个部分。
请记住首先检查是否已将至少4个字符读入缓冲区。例如,如果文件是21字节长,你的2。read可能只读取1个字符,并且您不应该与缓冲区中的其他3个字符进行比较。
如果你使用printf或puts或任何其他需要字符串的函数打印出缓冲区,缓冲区需要以' '字节结束,否则字符串函数不知道何时停止。
我将首先回答您实际提出的问题:memcmp是比较两个缓冲区的好方法。一些警告:
- 您还必须确保缓冲区的大小至少与目标字符串的大小相同
- memcmp返回0如果两个缓冲区匹配,这可能是违反直觉的。
例如,如果你想知道一个缓冲区是否等于字符串"rwxw",你可以写
if (readresult >= strlen("rwxw") && !memcmp(buffer, "rwxw", strlen("rwxw"))) {
// buffer match occurred!
}
我个人会使用"#define"或const char来确保字符串出现的三个地方实际上是同一个字符串。例如:
#define MATCH_STRING "rwxw"
if (readresult >= strlen(MATCH_STRING) && !memcmp(buffer, MATCH_STRING, strlen(MATCH_STRING))) {
// buffer match occurred!
}
然而,你的代码有几个其他的问题。一个是需要一个循环,它可以不断地从输入文件中读取数据并从输出文件中写入数据,直到输入耗尽为止。例如,沿着
一行while (true) {
readResult = fread(buffer, 20, 1, readfile);
if (readResult == 0) {
// end of file
break;
}
// put your check for the "rwxr" string here!
writeResult = fwrite(buffer, readResult, 1, writefile);
if (writeResult != readREsult) {
printf("errorn");
}
}
最后,你有一个可能被称为"风格"的错误。在您的程序中有两种情况:指定的文件名和默认的文件名。这两种情况共享了许多相同的代码,但是您进行了剪切和粘贴。这使得代码更难以理解,并且如果将来对其进行更改,则更容易出现错误。如果你在剪切和粘贴代码,那你就做错了!可以考虑这样做,这样可以最大化共享代码路径:
char *outFileName;
if (argc == 3) {
outFileName = argv[2];
} else {
outFileName = "prog1.out";
}
writefile = fopen(outFileName, "w");
if (!writefile) {
printf("Unable to open file %s.n", writeFileName);
exit(1);
}