Guest User

Untitled

a guest
Apr 7th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. //Sharifian, Arshya
  2. //3/04/2015
  3. //0240, MW 9-11am
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <string.h>
  8.  
  9.  
  10. //global structure, but why not a local structure?
  11. struct Person{
  12. char fname [100];
  13. char lname [100];
  14. //char address [200];
  15. char city [100];
  16. int zip;
  17. long int phoneNum;
  18. };
  19.  
  20. int main(){
  21.  
  22.  
  23. //asking the user how many structures we need to create
  24. int numProfiles;
  25. printf("How many profiles would you like to create?");
  26. scanf("%d", &numProfiles);
  27.  
  28. //an array of structures
  29. //change profile to persons
  30. //standard is to have a plural form of the structure
  31. struct Person profile [numProfiles];
  32.  
  33. //a pointer that points to a file
  34. FILE * fPointer;
  35.  
  36. //opening a new/existing file called person.txt, and the "+" allows us to read it afterwards)
  37. fPointer = fopen("person.txt", "w+");
  38.  
  39. //getting variables from user for each structure/profile
  40. for (int i = 0; i < numProfiles; i++){
  41.  
  42. printf("Profile # %d\n", i+1);
  43.  
  44. //ask for first name
  45. printf("Enter First Name: ");
  46. //store first name in the first structure under fname
  47. scanf("%s", profile[i].fname);
  48.  
  49. //ask for first name
  50. printf("Enter Last Name: ");
  51. //store first name in the first structure under fname
  52. scanf("%s", profile[i].lname);
  53.  
  54. // //***problem inserting spaces within the address
  55. //8515 farralone ave
  56. // printf("Enter Address: ");
  57. // //store first name in the first structure under fname
  58. // gets("%s", profile[i].address);
  59.  
  60. //***problem inserting spaces within the address
  61. printf("Enter City: ");
  62. //store first name in the first structure under fname
  63. fgets(profile[i].city, 160, stdin);
  64.  
  65. //ask for the zip code
  66. printf("Enter Zip Code:");
  67. //store the zip code into the address for the first structure under zip
  68. scanf("%d", &profile[i].zip);
  69.  
  70. //ask for the zip code
  71. printf("Enter Phone Number:");
  72. //store the zip code into the address for the first structure under zip
  73. scanf("%ld", &profile[i].phoneNum);
  74.  
  75. //insert structure instances into the "person" text file
  76. fprintf(fPointer, "%s\t%s\t%s\t%d\t%ld\t\n", profile[i].fname,profile[i].lname, profile[i].city, profile[i].zip, profile[i].phoneNum);
  77.  
  78. //end for loop
  79. }
  80. //closes the file
  81. fclose (fPointer);
  82.  
  83.  
  84. //read what is in the file
  85. fPointer = fopen("person.txt", "r");
  86. fseek(fPointer, 0, SEEK_SET);
  87. char userInput[200];
  88.  
  89. //****it prints a duplicate of the last entry?
  90. while (!feof(fPointer)){
  91. fgets(userInput, 200, fPointer);
  92. puts(userInput);
  93. }
  94.  
  95. fclose(fPointer);
  96.  
  97. // //creating a new instance of the structure Person
  98. // struct Person temp [numProfiles];
  99. // //copying structures
  100. // temp[numProfiles] = profile[numProfiles];
  101.  
  102. // fPointer = fopen("person.txt", "w+");
  103. //
  104. // struct Person temp[numProfiles];
  105. //
  106. // for (int i = 0; i < numProfiles; i++){
  107. // for (int j = i+1; j < numProfiles; j++){
  108. //
  109. //
  110. //
  111. // //char temp[200] = {NULL};
  112. // if (strcmp (profile[i].fname, profile[j].fname) > 0){
  113. //
  114. // temp[i].fname = profile[i].fname;
  115. // profile[i].fname = profile[j].fname;
  116. // profile[j].fname = temp[i].fname;
  117. // //end if statement
  118. // }
  119. // //end j for loop
  120. // }
  121. // //end i for loop
  122. // }
  123. //
  124. // fclose(fPointer);
  125. //
  126. // fPointer = fopen("person.txt", "r");
  127. // char sortedInput[200];
  128. //
  129. // while (!feof(fPointer)){
  130. // fgets(userInput, 200, fPointer);
  131. // puts(userInput);
  132. // }
  133. //
  134. // fclose(fPointer);
  135.  
  136.  
  137.  
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment