String sorthing, file opening and saving.

This is a discussion on String sorthing, file opening and saving. within the C Programming forums, part of the General Programming Boards category; I just have 3 quick questions if you can help me with any of these it would be great. 1. ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    String sorthing, file opening and saving.

    I just have 3 quick questions if you can help me with any of these it would be great.

    1. How do I make a series of strings within a structured array sort themself in alphebical order?

    2. I have the user being prompted for a filename to load. How do I make it so the filename that is typed in can be loaded and scanned in?

    3. How would I save the dada that I collected into an array when I am finished with it?

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    These are all fairly simple answers:

    1. Make a value for each letter in the alphabet (or better yet just use ASCII values), then open each string and check the first letter of the string, and sort according to the number value.

    2. Just make a character array to input the name, then use the array in fopen()

    Code:
    char filename[20];
    
    printf("Enter file name to open");
    gets(filename);
    fpin=fopen(filename,"r");

    3. Please be more specific with your question. Do you want to save the array into a separate file?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    gets(filename);
    Meanwhile, I run the program by redirecting the input stream to a text file, and kill your program by dropping a ten meg non-newline-containing file as input for your gets call...

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    alright.. to be more specific on #3. I am creating a phonebook that you can open, edit, search, delete, add, and have it sort it's self. The last name, first name and phone numbers are all stored in a structure with arrays for each of them. How would I make it so it would save the whole structure?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Open the file.
    Write the data.
    Close the file when done.

    Any C book should cover this in detail. You really should read one.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    I have read them... but I hate the way my book explains it and i always get confused when I try to implement them.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by j0hnb
    I have read them... but I hate the way my book explains it and i always get confused when I try to implement them.
    Then show what you've implemented (by posting your code) and someone will help you with it. It's hard to help someone when you can't see what they're doing...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    All I have really done is layed it out so I can see what I need to do and what I need to know before I really start workin on it... I know some of the code is wrong below. I've never really got a good understanding of structures on how to pass them and all that. so the passing is wrong, also everything is global for the moment... I'll fix that later on.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char lname[20], fname[15], phoneNo[12];
    int menuchoice, record, i, cnt, totalcnt;
    FILE *ifp;
    
    
    	struct record{
    			char lname[20];
    			char fname[15];
    			char phoneNo[12];
    	};
    
    void addrecord(struct record char, char, int);
    void deleterecord(struct record char, char, int);
    void editrecord(struct record char, char, int);
    void search(struct record char, char, int);
    void view(struct record char, char, int);
    void sort(struct record char, char, int);
    void menu(struct record char, char, int);
    
    int main (void)
    {
    
    struct record temp;
    char filename[20];
    
    printf("Enter file name to open");
    gets(filename);
    fpin=fopen(filename,"r");
    
    scanf("%s%s%d", &temp.lname[0],&temp.fname[0],&temp.phoneNo[0]);
    cnt = 1;
    while (scanf("%s%s%d", &temp.lname[cnt],&temp.fname[cnt],&temp.phoneNo[cnt]);
    != EOF){cnt++}
    cnt = totalcnt;
    
    menu(struct record char, char, int);/*Edit*/
    
    return 0;
    }
    
    
    void menu(struct record char, char, int){
    printf("\nPhonebook menu\n1)  Add new record\n2)  Delete record\n3)  Edit record\n4)  Search phone book\n5)  View phone book\n6)  Quit\nEnter choice #: ");
    	scand("%d", &menuchoice);
    
    switch (menuchoice) 
    		{
    
    	case 1: addrecord(struct record char, char, int); break;/*Edit*/
    
    	case 2: deleterecord(struct record char, char, int); break;/*Edit*/
    
    	case 3: editrecord(struct record char, char, int); break;/*Edit*/
    
    	case 4: search(struct record char, char, int); break;/*Edit*/
    
    	case 5: view(struct record char, char, int); break;/*Edit*/
    
    	case 6: return 0;
    		}
    
    	return 0;
    	}
    
    
    
    void addrecord(struct record char, char, int){
    printf("\nAdd Record\n");
    printf("Last Name: ");
    	scanf("%s", &temp.lname[totalcnt+1] ); /*Edit*/
    printf("\nFirst Name: );
    	scanf("%s", &temp.fname[totalcnt+1]); /*Edit*/
    printf("\nPhone Number: );
    	scanf("%s", &temp.phoneNo[totalcnt+1]); /*Edit*/
    printf("\nPhone number added");
    	++totalcnt;
    }
    
    void deleterecord(struct record char, char, int){
    printf("\nDelete Record\n");
    printf("Last Name: ");
    	scanf("%s", &deletelname);
    		/*Edit*/
    		/*Edit*/
    		/*Edit*/
    
    
    }
    
    void editrecord(struct record char, char, int){
    printf("\nEdit Record\n");
    printf("Last name: ");
    	scanf("%s", &editlname");
    		/*Edit*/
    		/*Edit*/
    		/*Edit*/
    
    
    }
    
    void search(struct record char, char, int){
    printf("\nSearch\n");
    printf("Search for: ");
    	scanf("%s", &searchfor);  
    		/*Edit*/
    		/*Edit*/
    		/*Edit*/
    	
    }
    
    void view(struct record char, char, int){
    printf("\nView Records\n");
    	for (i=0; i < totalcnt; i++)
    		{
    			printf("\n%s %s   %d", temp.lname[i], temp.fname[i], temp.phoneNo[i]); /*Edit*/
    		}
    }
    
    void sort(struct record char, char, int){
    		/*Edit*/
    		/*Edit*/
    		/*Edit*/
    }


    Code tags added by Hammer

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    It is better to use fgets.

  10. #10
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532
    Code:
    int menuchoice, record, i, cnt, totalcnt;
    FILE *ifp;
    
    
    	struct record
    Error: ??
    And why do you want to use global ? Not necessary.

    Ohh.. I guess you need space in
    scanf("%s %s %d", &temp.lname[0],&temp.fname[0],&temp.phoneNo[0]);
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filestreams - opening them and peek()
    By Crystylla in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2006, 01:19 PM
  2. open and edit file at command prompt
    By nmullane in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2005, 11:56 PM
  3. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  4. Saving and opening txt file...
    By Kitu in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2004, 12:01 PM
  5. getting the file name
    By mr ed in forum C++ Programming
    Replies: 0
    Last Post: 09-18-2001, 05:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21