Thread: using txt file to add and edit accounts

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    8

    using txt file to add and edit accounts

    This is not my actual code but a knock off of what i made so that i dont go copying and pastin whatever ppl send me... this is kind of a short version.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct acct
        {
    	int accountNumber;
    	char givenName[20];
    	float balance;
        };
    
    struct allAccounts
        {
    	struct acct a000001;
    	struct acct a000002;
        }accounts;
    
    void clearKeyboardBuffer()
        {
        char ch;
        while ((ch = getchar()) != '\n' && ch != EOF);
        }
    
    char mainMenu(int numOfAccounts)
        {
        char menuChoice;
        menuChoice = '0';
        while (menuChoice != '6')
          {
    	system("clear");
    	printf("* 1 - Print all the accounts                        *\n");
    	printf("* 2 - Edit an existing account                      *\n");
    	printf("* 3 - create new account               *\n");
    	printf("Enter your choice here (1, 2, 3,):  ");
    	menuChoice = getchar();
    	clearKeyboardBuffer();
            if (menuChoice == '1')
    	    printFile(numOfAccounts);
            if (menuChoice == '2')
    	    edit(numOfAccounts);
            if (menuChoice == '3')
    	    newAccount(numOfAccounts);
                 }
        return menuChoice;
        }
    
    
    
    
    
    int printFile(int numOfAccounts)
        {
        system("clear");
        FILE *File;
        ipFile = fopen("info.csv", "r");
        char *t`,*t2,*t3;
        char stuff[80];
        char *search = ",";
    float balance;
    
        fgets(buf, 80, File);
        t1 = strtok(input, ",\0");
         account=atoi(t1);
         printf("%d\n", account);
    	
        t2 = strtok(NULL, search);
         printf("%s\n", t2);
    	
        t3 = strtok(NULL, search);
         balance =atof(t7);
         printf("%.2f\n", balance);
    
        fclose(ipFile);
        clearKeyboardBuffer();
        }
    
    main()
    {
        int numOfAccounts;
        numOfAccounts = 0;
        mainMenu(numOfAccounts);
    
    }
    struct info is held in a .csv file instead of placed in code with 3lines saying (goin account id,name, balance)
    4,joe,55
    3,fred,77
    2,bill,109

    I believe my printFile function is opening up my file, reading it so it can take in the info and use the strok command to extract the data. (do I have to do it again and again depending on how many lines my .csv file has?) Mainly all this function does is print everything in my file.

    Code:
    newAccount(numOfAccounts);
    char str1[160];
         
    sprintf(str1," %d, %s, %s, %s, %0.2f,",a1.accountNumber,\
                  a1.givenName,a1.balance); 
    
     opFile = fopen("data.csv","w");
     if (opFile == NULL)
     {
      printf("sorry cant open the file\n");
      return(-1);
     }
     fprintf(opFile, "%s\n",str1);
      fclose(opFile);
    
     opFile = fopen("data.csv","r");
     if (dataFile == NULL)
     {
      printf("sorry cant open the file\n");
      return(-1);
     }
     fgets(str1,160,opFile);
      fclose(opFile);
    I have this code from a previous assignment to convert it to a string, BUT what do i modify to make sure it writes into the file correctly under the rest of the file (got an error using this format by the way it looked in my .csv file) .


    Any suggestions or code snippets on how to edit previously saved text from a .csv file is helpful as i'm completly stuck on how to do that one

    thanks, and hope this isn't confusing
    Last edited by kokoro; 12-09-2009 at 05:42 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Kokoro, all we need for a start to understand your problem is:

    1) 5 examples of your input to the program - we've got 3, how about 2 more?

    2) Those same 5 examples as you want them to be changed, by your program. I still have no idea *WHAT* you want the text to be changed into, exactly.

    3) The final output of those 5 examples (rows of text perhaps).

    How the data originally got there, and all the rest, doesn't matter - those are things you'll integrate. If you have a specific question about that, please ask.

    But you need to make your posts more like C itself - concise, and to the point. About halfway through your posts, my eyes just begin to glaze over.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    it does look complicated doesnt it.. mainly want help in my newaccount function.sorry should be
    Code:
    sprintf(str1," %d,  %s, %0.2f,",a1.accountNumber,\
                  a1.givenName,a1.balance);  // had copied piece of my code that uses 5 inputs
    i was putting in code but changing it up so when i do it to my original code i understand it better and its not just a copy and paste.

    What I really want is to know how to add more info (add another account) and store it into the text pad.

    i believe the above function snippet would save it onto struct a1 so to make new accounts would i make a for loop that replaces 1 to like a[i].accountnumber and would that allow me to put in a new as many accounts as i would want?

    sorry im really bad at explaining code and saying what i want it to do

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For new accounts, you can use just a single struct for the data:

    1) create an instance of the struct in that function
    2) Open the data file, in append mode - "at" for append text mode.
    3) start a loop (do, while, or for)
    4) get the data inside the loop
    5) at the bottom of the input loop, write out the data to the file.

    I'm not sure what "store it in the text pad" means. You mean add data to the data file, I hope.

    A programming forum is no place for hemming and hawing - speak clearly and straightforward, and speak your mind.

    We don't always agree, but we're all sure that we can't get any help understanding what you want, beyond the words you use.

    You can do this - make 'em count.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doesn't write in file when call GetSaveFileName()
    By randall81 in forum Windows Programming
    Replies: 1
    Last Post: 03-28-2009, 01:34 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Menu like file, edit, help, etc...
    By Livijn in forum Windows Programming
    Replies: 44
    Last Post: 01-23-2007, 05:49 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM