Skip to content

Instantly share code, notes, and snippets.

@vngkv123
Created October 5, 2017 14:28
Show Gist options
  • Save vngkv123/efd891858aa064cd867a40f55fe4632e to your computer and use it in GitHub Desktop.
Save vngkv123/efd891858aa064cd867a40f55fe4632e to your computer and use it in GitHub Desktop.
file open, struct test1
#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