Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Sharifian, Arshya
- //3/04/2015
- //0240, MW 9-11am
- #include <stdio.h>
- #include <string.h>
- #include <string.h>
- //global structure, but why not a local structure?
- struct Person{
- char fname [100];
- char lname [100];
- //char address [200];
- char city [100];
- int zip;
- long int phoneNum;
- };
- int main(){
- //asking the user how many structures we need to create
- int numProfiles;
- printf("How many profiles would you like to create?");
- scanf("%d", &numProfiles);
- //an array of structures
- //change profile to persons
- //standard is to have a plural form of the structure
- struct Person profile [numProfiles];
- //a pointer that points to a file
- FILE * fPointer;
- //opening a new/existing file called person.txt, and the "+" allows us to read it afterwards)
- fPointer = fopen("person.txt", "w+");
- //getting variables from user for each structure/profile
- for (int i = 0; i < numProfiles; i++){
- printf("Profile # %d\n", i+1);
- //ask for first name
- printf("Enter First Name: ");
- //store first name in the first structure under fname
- scanf("%s", profile[i].fname);
- //ask for first name
- printf("Enter Last Name: ");
- //store first name in the first structure under fname
- scanf("%s", profile[i].lname);
- // //***problem inserting spaces within the address
- //8515 farralone ave
- // printf("Enter Address: ");
- // //store first name in the first structure under fname
- // gets("%s", profile[i].address);
- //***problem inserting spaces within the address
- printf("Enter City: ");
- //store first name in the first structure under fname
- fgets(profile[i].city, 160, stdin);
- //ask for the zip code
- printf("Enter Zip Code:");
- //store the zip code into the address for the first structure under zip
- scanf("%d", &profile[i].zip);
- //ask for the zip code
- printf("Enter Phone Number:");
- //store the zip code into the address for the first structure under zip
- scanf("%ld", &profile[i].phoneNum);
- //insert structure instances into the "person" text file
- 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);
- //end for loop
- }
- //closes the file
- fclose (fPointer);
- //read what is in the file
- fPointer = fopen("person.txt", "r");
- fseek(fPointer, 0, SEEK_SET);
- char userInput[200];
- //****it prints a duplicate of the last entry?
- while (!feof(fPointer)){
- fgets(userInput, 200, fPointer);
- puts(userInput);
- }
- fclose(fPointer);
- // //creating a new instance of the structure Person
- // struct Person temp [numProfiles];
- // //copying structures
- // temp[numProfiles] = profile[numProfiles];
- // fPointer = fopen("person.txt", "w+");
- //
- // struct Person temp[numProfiles];
- //
- // for (int i = 0; i < numProfiles; i++){
- // for (int j = i+1; j < numProfiles; j++){
- //
- //
- //
- // //char temp[200] = {NULL};
- // if (strcmp (profile[i].fname, profile[j].fname) > 0){
- //
- // temp[i].fname = profile[i].fname;
- // profile[i].fname = profile[j].fname;
- // profile[j].fname = temp[i].fname;
- // //end if statement
- // }
- // //end j for loop
- // }
- // //end i for loop
- // }
- //
- // fclose(fPointer);
- //
- // fPointer = fopen("person.txt", "r");
- // char sortedInput[200];
- //
- // while (!feof(fPointer)){
- // fgets(userInput, 200, fPointer);
- // puts(userInput);
- // }
- //
- // fclose(fPointer);
- }
Advertisement
Add Comment
Please, Sign In to add comment