Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By hk_mp5kpdw

Editing a .txt file?

This is a discussion on Editing a .txt file? within the C Programming forums, part of the General Programming Boards category; Hello. I want to edit a .txt file and I have no idea of how to do it - this ...

  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:

    PHP Code:
    Mary;May;12 Good Lane;02 6889 2456;maryblossom@yahoo.com;4567 7956 4562 4568;12/17;124;5559864582456;010134;David;Bowie;In Space;09000000;bowie@space.com;0000587945642134;10/99;458;795;010376;Mr.;Tree;The Jungle12456;tree@jungle.com;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);
        }
    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
    32,830
    > fprintf(cust, "%s", fname);
    You have to rewrite the entire file, if you want to make changes.

    That means a for loop over your entire custData array.

    > for(i=0;i<126;i++)
    Perhaps use reccount as the loop limit.
    jigamuffin likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    333
    Ypu need two functions

    customer *readcustomers(char *filename, int *N)

    int writecustomers(char *filename, customer *customers, int N)

    Writing is easy. Just loop over the structure, calling printf() onthe fields.

    Reading is harder. You will need to call malloc() and realloc() to grow the array dynamically. Often it makes things easier if you're allowed at all a "number of records" field at the start of the file. You need to fill in N to tell caller how many recors were in the file.

    You test the functions agaisnt each other. call the write, then try to read the data you have just written, write it again, and examine the files to make sure they are the same.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    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))
    {
    Salem likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O editing problems
    By opeth in forum C Programming
    Replies: 1
    Last Post: 05-07-2012, 09:40 PM
  2. C++ XML file editing
    By nick753 in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2011, 11:40 AM
  3. Editing a text file
    By bijan311 in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2010, 12:04 PM
  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, 04:02 PM

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