Thread: Editing a .txt file?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    Editing a .txt file?

    Hello. I want to edit a .txt file and I have no idea of how to do it - this is what I have come up with so far. With the .txt file reading this:

    Mary;May;12 Good Lane;02 6889 2456;[email protected];4567 7956 4562 4568;12/17;124;5559864582456;010134;
    David;Bowie;In Space;09000000;[email protected];0000587945642134;10/99;458;795;010376;
    Mr.;Tree;The Jungle; 12456;[email protected];46546464;65/12;656;5683655;010588;

    When I run it, it prompts me to enter the new name but nothing happens.

    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    
    
    typedef struct{
            char fname[32];
            char lname[32];
            char address [64];
            char phone [16];
            char email [64];
            char credit_num[32];
            char credit_expiry[16];
            char credit_bsb[4];
            char licence_num[32];
            int ID;
            }customer;
    
    
    int main(){
        
        customer custData[128];
        int ch;
        char surname[32];
        char fname[32];
        FILE *cust;
        char address[64];
        char num[16];
        char mail[64];
        char crednum[32];
        char expiry[16];
        char bsb[4];
        char licnum[32];
        char line[18];
        char *item;
        int i = 0;
        int reccount = 0;
        int ID;
        int a;
        char newName[28];
    int no;
        
        cust = fopen("customer.txt", "r");
        while(fgets(line,126,cust))
            {
                
                //for copying strings
                item = strtok(line, ";");
                
                strcpy(custData[reccount].fname, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].lname, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].address, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].phone, item);
                item = strtok(NULL, ";");
            
                strcpy(custData[reccount].email, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].credit_num, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].credit_expiry, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].credit_bsb, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].licence_num, item);
                item = strtok(NULL, ";");
                
                custData[reccount].ID = atoi(item);
                
                reccount++;
                
       }
        fclose(cust);
        
        cust = fopen("customer.txt", "a");
        
        printf("1 - Find with customer ID");
        printf("\n2 - Find with customer name");
        scanf("%d", &ch); 
        switch(ch){
                   case 1:
                        printf("Enter customer ID>");
                        scanf("%d", &ID);
                   for(i=0;i<126;i++){
                            if(ID == custData[i].ID){
                                  
                                  printf("\n1 - Change first name\n2 - Change surname\n3 - Change address\n4 - Change phone\n5 - Change phone\n6 - Change credit card number\n7 - Change credit card expiry\n8 - Change credit card BSB\n9 - Change licence number\n");
                scanf("%d", &no);
                                 
                                   if(no==1){    
                                       printf("Enter new first name>");
                                       scanf("%s", custData[i].fname);
                                       fprintf(cust, "%s", fname);
                                  }
                    else if(no==2){
                        
    }
                                  }
                            }
    
    
                   /*
                   case 2:
                        printf("Enter customer name>");
                        fgets(name, 62, stdin);
                        
                   }
                   */
                   
        }
    fclose(cust);
        }
    the ID is the last number of the line in the .txt file.
    any hints or pointers are appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Editing a .txt file?
    By jigamuffin in forum C Programming
    Replies: 3
    Last Post: 05-08-2012, 05:47 AM
  2. C++ XML file editing
    By nick753 in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2011, 11:40 AM
  3. Editing an include.h file
    By Char*Pntr in forum C Programming
    Replies: 5
    Last Post: 08-16-2010, 11:31 AM
  4. Editing a text file
    By chimpanzee in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2006, 04:47 AM
  5. editing file
    By hmunhung in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2002, 03:02 PM