我正在尝试使用文件描述符读取文件(这可能比我需要做的工作要多得多,但尽管如此..)
我正在尝试创建一个类似于 ar 的存档器。我有一个要读取的非空文件,但是当我尝试使用 read 命令检索前 8 个字节时,返回 int 为 0,这意味着它没有读取任何字节。errno 告诉我一切都很顺利。
我要做的是读取文件开头的字符串,这样我就可以运行字符串比较。
对意大利面条代码感到抱歉,我仍在测试并试图解决问题。
问题在于语句 temp = read(archiveFD,buf,8); archiveFD 指向我的存档文件,该文件不为空,但没有读取任何内容。
命令:
./a.out r ar.c archive.a
弧:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <utime.h>
#include <errno.h>
#define BUF_SIZE 1024
int main(int argc, char *argv[]) {
int fileFD, archiveFD, openFlags;
mode_t filePerms;
ssize_t numRead;
char buf[BUF_SIZE];
char fileName[16];
const struct utimbuf *times;
char modTime[12];
char entry[29];
int temp;
char *line = NULL;
size_t len = 0;
if(strcmp(argv[1], "r") != 0 && strcmp(argv[1], "x") != 0 && strcmp(argv[1], "d") != 0 && strcmp(argv[1], "t") != 0) {
printf("%s","Not a valid command.\n");
return 0;
}
if(strcmp(argv[1], "r") == 0) {
if(argv[3] == NULL) {
printf("%s","Missing arguments.\n");
return 0;
}
fileFD = open(argv[2], O_RDONLY);
if(fileFD == -1) {
printf("%s","Error opening input file.\n");
return 0;
}
openFlags = O_CREAT | O_RDWR | O_TRUNC;
filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
archiveFD = open(argv[3], openFlags, filePerms);
if(archiveFD == -1) {
printf("%s","Error opening archive file.\n");
return 0;
}
temp = read(archiveFD,buf,8);
//printf("%s",(char *) buf);
// if(strcmp("!<arch>\n",buf) != 0)
// write(archiveFD,"!<arch>\n",8); //begins archive
// printf("%s","here");
//}
printf("%d",temp);
printf("%s",strerror(errno));
//printf("%s",buf);
sprintf(fileName,"%-16s",argv[2]);
printf("%s",fileName);
utime(argv[2],times);
//strcpy(modTime,"12345678901234");
//printf("%s",modTime);
sprintf(modTime,"%-.12lld",(long long) times->modtime);
modTime[12] = '\0';
printf("%s",modTime);
sprintf(entry,"%s%s\n",fileName,modTime);
printf("%s",entry);
write(archiveFD,entry,29);
while((numRead = read(fileFD, buf, BUF_SIZE)) > 0)
if(write(archiveFD, buf, numRead) != numRead) {
printf("%s","Could not write whole buffer.\n");
return 0;
}
if(numRead == -1) {
printf("%s","Error reading.\n");
return 0;
}
if(close(fileFD) == -1) {
printf("%s","Error closing input file.\n");
return 0;
}
if(close(archiveFD) == -1) {
printf("%s","Error closing archive file.\n");
return 0;
}
}
return 0;
}`
存档.a:
ar.c 140737161196
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <utime.h>
#include <errno.h>
#define BUF_SIZE 1024
int main(int argc, char *argv[]) {
int fileFD, archiveFD, openFlags;
mode_t filePerms;
ssize_t numRead;
char buf[BUF_SIZE];
char fileName[16];
const struct utimbuf *times;
char modTime[12];
char entry[29];
int temp;
char *line = NULL;
size_t len = 0;
if(strcmp(argv[1], "r") != 0 && strcmp(argv[1], "x") != 0 && strcmp(argv[1], "d") != 0 && strcmp(argv[1], "t") != 0) {
printf("%s","Not a valid command.\n");
return 0;
}
if(strcmp(argv[1], "r") == 0) {
if(argv[3] == NULL) {
printf("%s","Missing arguments.\n");
return 0;
}
fileFD = open(argv[2], O_RDONLY);
if(fileFD == -1) {
printf("%s","Error opening input file.\n");
return 0;
}
openFlags = O_CREAT | O_RDWR | O_TRUNC;
filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
archiveFD = open(argv[3], openFlags, filePerms);
if(archiveFD == -1) {
printf("%s","Error opening archive file.\n");
return 0;
}
temp = read(archiveFD,buf,8);
//printf("%s",(char *) buf);
// if(strcmp("!<arch>\n",buf) != 0)
// write(archiveFD,"!<arch>\n",8); //begins archive
// printf("%s","here");
//}
printf("%d",temp);
printf("%s",strerror(errno));
//printf("%s",buf);
sprintf(fileName,"%-16s",argv[2]);
printf("%s",fileName);
utime(argv[2],times);
//strcpy(modTime,"12345678901234");
//printf("%s",modTime);
sprintf(modTime,"%-.12lld",(long long) times->modtime);
modTime[12] = '\0';
printf("%s",modTime);
sprintf(entry,"%s%s\n",fileName,modTime);
printf("%s",entry);
write(archiveFD,entry,29);
while((numRead = read(fileFD, buf, BUF_SIZE)) > 0)
if(write(archiveFD, buf, numRead) != numRead) {
printf("%s","Could not write whole buffer.\n");
return 0;
}
if(numRead == -1) {
printf("%s","Error reading.\n");
return 0;
}
if(close(fileFD) == -1) {
printf("%s","Error closing input file.\n");
return 0;
}
if(close(archiveFD) == -1) {
printf("%s","Error closing archive file.\n");
return 0;
}
}
return 0;
}