2013-02-10 7 views
1

Je suis en train de lire un fichier en utilisant des descripteurs de fichiers (ce qui est probablement beaucoup plus de travail que je dois faire, mais quand même ..)Lire Renvoie 0 Octets

Je suis en train de créer un archiveur similaire à ar. J'ai un fichier non vide que j'essaye de lire mais quand j'essaye la commande de lecture pour récupérer les 8 premiers octets le retour int est 0 qui signifie qu'il n'a pas lu aucun bytes. Et errno me dit que tout s'est bien passé. Ce que j'essaie de faire est de lire la chaîne au début du fichier afin que je puisse exécuter des comparaisons de chaînes. Désolé pour le code spaghetti, je suis encore en train de tester et essayer de comprendre les choses.

Le problème est à l'instruction temp = read (archiveFD, buf, 8); archiveFD pointe vers mon fichier d'archive, qui n'est pas vide mais rien n'est lu.

commande

:

./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,"123456789"); 
    //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,"123456789"); 
    //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

Ceci est trop long, quelques personnes vont le lire .. Essayez d'isoler le problème réel et éditez votre question. Juste un conseil. –

+0

N'utilisez pas printf pour imprimer des erreurs. À tout le moins, utilisez 'fprintf (stderr, ...', mais encore mieux, faites: 'if ((fileFD = open (chemin, drapeaux) == -1) {perror (chemin); exit (1); } ' –

Répondre

1

Dans ce bloquer: (vérification d'erreur omise par souci de concision)

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); 

Je m'attendrais absolument à lire pour retourner zéro. Vous venez d'ouvrir le fichier avec O_TRUNC, donc même si le fichier n'était pas vide, c'est après l'avoir ouvert. Si vous ne souhaitez pas supprimer toutes les données existantes, supprimez O_TRUNC de openFlags.

+0

Merci, je ne reçois pas assez de temps avec mes devoirs.Par conséquent, la coupe rapide et bâclée et coller et sauter des détails sans les comprendre .. Merci beaucoup – efiniti