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.
im trying to wite a program that will generate log files depending on information enterd by the user.
this is the part thats not working it has to do with file creation
Code:
else if (argv[1] == NULL)
{
printf("Please enter the current date in mm-dd-yy format\n>");
scanf("%s",&mdy);
strcat(mdy,filename);
plog = fopen(mdy,"w");
fprintf(plog,"PHONE LOG\n");
errchk = fclose(plog);
if (errchk == -1)
{
puts("error closeing file after create");
}
}
what i want it to do is that when there is no second option specifyed then to create a file on the harddisk depending on user entered values.
Originally posted by exvor
am i doing this wrong ?
See the red remarks below.
Code:
else if (argv[1] == NULL) /* Maybe OK, but I'd do: else if (argc < 2) */
{
printf("Please enter the current date in mm-dd-yy format\n>");
scanf("%s",&mdy); /* remove '&': strings are already pointers */
strcat(mdy,filename); /* Make sure 'mdy' is big enough, better use strncat(). */
plog = fopen(mdy,"w");
/* More important to check for error here, then when closing */
fprintf(plog,"PHONE LOG\n");
errchk = fclose(plog);
if (errchk == -1)
{
puts("error closeing file after create");
}
}
Your scanf() is not right for one, I am assuming you have a
char mdy[???];
buffer somewhere, you must not specify &mdy as the argument to
scanf. Instead just use mdy without &.
The compiler will normally help you out in the case where you declare
mdy as a char array and do the right thing, but if mdy is a pointer to a dynamically allocated buffer then you will be taking the address of that pointer and have a segfault problem if they type in more than 3 chars plus null term (the size of a void*).
I would not use scanf() at all, instead use
fgets(mdy,sizeof(mdy),stdin);
Second problem, your puts() string is not terminated with a newline, if you don't do this then the buffered stdout may not flush to the screen until you either print something else or terminate the program.
Why ask the user for the current date, this reminds me of DOS 1.0 booting from a floppy before the days of embedded real-time-clock.
Just a rough draft for a program to help me keep track of calls here. The thing really is not supposed to be anything near the real program just a test of some of the things i will have to do.
yes and puts does term with a /n
why use fgets with stdin if im not working with a file and just the standard input seams silly to use that . But i guess its better for security reasons.
mdy was supposed to be repesentive of month day year but got changed silightly. as for strcat i did make sure mdy is big enough and no im not careing too much about memory management. The program that i created at least worked sort of the way i wanted it to.
when i compleate the program i might post it here just for some error checking
Originally posted by exvor
why use fgets with stdin if im not working with a file and just the standard input seams silly to use that . But i guess its better for security reasons.
...and to prevent segfault crashes of your program.
Quote:
as for strcat i did make sure mdy is big enough and no im not careing too much about memory management.
How big is "big enough"?
If mdy is 20 bytes big, your program can be crashed by entering a string of 20 chars or a little more.
if it's 10000000 bytes big, a string of about 10000000 (or a bit more) would crash it. And so on...
Using strncat() in a proper way would fix it. But if you just don't care, that's OK of course. It's your choice. For just practicing/learning programming it obviously not a real problem. But to learn good practice from the start would be my choice.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.