ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What you've called string_return is known as strdup in unix. That's just FYI, I wouldn't suggest using strdup if you're writing portable C, but you might think about the naming differently (focusing on the "return" aspect of it looks a bit funny to me).
Code:
deposit = prompt_scan_f("enter deposit value");
// make sure valid dollar value
if (string_valid_dollar(deposit)) {
deposit = string_return(deposit);
Since prompt_scan_f is already allocating its result, the call to string_return here is redundant and causes a memory leak. And there is a similar problem in name_check.
Code:
// open file for reading. if fail to open then create new file
acctinfo = fopen(ACCTFILE, "r");
Why not just open it for appending (it will automatically create the file as needed)?
Code:
// set up string to search for
char * searchstr = (char*)malloc((strlen(acctnum) + 1) * sizeof(char));
strcpy(searchstr, "acct");
strcat(searchstr, acctnum);
The allocation is too small here (you allocated room for acctnum only but you're putting 4 additional characters), so you're probably going to have memory corruption.
Why not just open it for appending (it will automatically create the file as needed)?
Because it does goes to a different function if the file needs to be created. First run it will create. After that it will always open to read. Unless there is a better way to do that.
I will continue working and exploring. I appreciate the response.
*EDIT*
Cleaned it up. Think i got the memory leaks, had more than I realized. The libraries are in the same repository as the program now so its in one place.
Because it does goes to a different function if the file needs to be created. First run it will create. After that it will always open to read. Unless there is a better way to do that.
Oh, I see, I misread the append_to_file function. It looks a bit convoluted though, do you really need to make a temp copy of your whole database just to find the next account number?
I don't have to do that for the account number now. I wrote a function to find the highest integer in a given column separated by delimiter. To calc the balance though with the file_search_func as it is I want it to get the first value that matches what it wants. As it writes from top to bottom it needs to start at the "bottom" to get the most recent.
I suppose I can stick the file flip in the file search function and leave it there. Is only place it gets used really.
*EDIT*
I suppose I could do the same as my highest_integer function and have it parse floats. Then I could drop the flip altogether. Keep the function but no need in this case...
It all seems to be working right now save one thing, oddly enough with the append_to_file function.
this is the output to the file. I just did the create new account over and over.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.