c - File Written in Chinese (I think?) -
i have assignment create own version of unix ar, in c. broke assignment several pieces , trying write file names file(which archive file) @ point. can create file, , when use printfs make sure i'm getting proper arguments command line, file names correct. when open file have created suppose hold file names, in asian language.
i've researched problem , haven't found go on. thing did find files being encoded differently, i'm pretty confused @ point.
#include <stdlib.h> #include "ar.h" #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main (int argc, char **argv) { char *file_name, *archive_name; size_t count = 50; int i, fd, num_written; //command line options later //argv[1] //get archive name archive_name = argv[2]; //get file names add archive (i = 3; i<argc; i++) { file_name = argv[i]; } //create archive file fd = open(archive_name, o_rdwr |o_creat|o_append); if (fd < 0){ perror("error while opening file.\n"); exit(exit_failure); } else printf("file %s opened successfully\n", archive_name); //write file names archive file (i = 3; i<argc; i++) { printf("file name: %s\n", argv[i]); num_written = write(fd, argv[i], 50); if (num_written == -1){ perror("error while writing archive.\n"); exit(exit_failure); } } close(fd); return 0; }
your code assuming arguments after archive name 50 bytes long. if arguments shorter that, program may crash or write random data. since didn't crash, , notepad (or whatever) seems think it's chinese, i'm going assume wrote random data. use strlen
figure out how long each of argument names are.
additionally:
check argv[2]
without checking argv
, cause crash.
nothing file_name
makes sense.
Comments
Post a Comment