读取返回0字节

时间:2013-02-10 19:10:02

标签: c

我正在尝试使用文件描述符读取文件(这可能比我需要做的工作多得多,但仍然......)

我正在尝试创建一个类似于ar的归档程序。我有一个非空文件,我试图读取,但当我尝试读取命令来检索前8个字节时,返回int为0,这意味着它没有读取任何字节。并且errno告诉我一切都很顺利。

我要做的是读取文件开头的字符串,以便我可以运行字符串比较。

对于意大利面条代码感到抱歉,我还在测试并尝试解决问题。

问题出在语句temp = read(archiveFD,buf,8); archiveFD指向我的存档文件,该文件非空,但没有读取任何内容。

命令:

./ a.out r ar.c archive.a

ar.c:

#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;
}`

archive.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;
}

1 个答案:

答案 0 :(得分:1)

在此块中:(为简洁起见,省略了错误检查)

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);
temp = read(archiveFD,buf,8);

我绝对希望读回零。您刚刚使用O_TRUNC打开了该文件, 所以即使文件不是空的,也是在你打开它之后。如果您不想丢弃所有现有数据,请从openFlags中删除O_TRUNC