Created
October 5, 2017 14:28
-
-
Save vngkv123/efd891858aa064cd867a40f55fe4632e to your computer and use it in GitHub Desktop.
file open, struct test1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
struct data_struct{ | |
char filename[512]; | |
char data_buffer[1024]; | |
}; | |
int main(int argc, char *argv[]){ | |
struct data_struct ds; | |
if( argc < 2 ){ | |
fprintf(stderr, "Usage : %s [target file]\n", argv[0]); | |
return -1; | |
} | |
int fd = open(argv[1], O_RDONLY); | |
if( fd < 0 ){ | |
perror("open "); | |
return -1; | |
} | |
memcpy(ds.filename, argv[1], strlen(argv[1])); | |
printf("Filename : %s\n", ds.filename); | |
while(read(fd, ds.data_buffer, 1024) > 0){ | |
printf("%s\n", ds.data_buffer); | |
memset(ds.data_buffer, 0, 1024); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment