Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct Books {
  5. char* id;
  6. char* title;
  7. char* pages;
  8. char* year;
  9. char* author;
  10. char* subject;
  11. } book;
  12. char* filename;
  13. int bookcount=4;
  14. int main(int argc, char* argv[]){
  15. if (argc < 1)
  16. return -1;
  17. filename=argv[1];
  18. char* sptr;//pointer for text size
  19. char* lptr;//to point the size of the library
  20. char tempstring[1024],*token;
  21. int i=0;
  22. lptr=(char*) malloc(bookcount*sizeof(book));
  23. *lptr=bookcount;
  24. book books[*lptr];
  25. FILE *fptr;
  26. fptr=fopen(filename,"r");
  27. if(fptr==NULL)
  28. return-1;
  29. for(i=0;i<bookcount;++i){ //suppose to get all the books into the book array
  30. fgets(tempstring,1024,fptr);
  31. token = strtok(tempstring, ",\n");
  32. strcpy(books[i].id,token);
  33. token = strtok(NULL, ",\n");
  34. strcpy(books[i].title,token);
  35. token = strtok(NULL, ",\n");
  36. strcpy(books[i].pages,token);
  37. token = strtok(NULL, ",\n");
  38. strcpy(books[i].year,token);
  39. token = strtok(NULL, ",\n");
  40. strcpy(books[i].author,token);
  41. token = strtok(NULL, ",\n");
  42. strcpy(books[i].subject,token);
  43. token = strtok(NULL, ",\n");
  44. }
  45. fclose(fptr);
  46. printf("%s",books[i].id);
  47. return 1;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement