Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-29-2005, 11:49 AM
|
#1
|
|
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,466
Rep:
|
strange c issue
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.
am i doing this wrong ?
Last edited by exvor; 01-29-2005 at 12:14 PM.
|
|
|
|
01-29-2005, 12:19 PM
|
#2
|
|
Moderator
Registered: Aug 2002
Posts: 10,697
|
argv[0] is the path of the running application
argv[1] is the first command line argument
argv[2] is the second and so on.
I use argc to check for the number of command line arguments when I do not want to go to the trouble of add option letters i.e. -f
So what are the errors? I would guess its how the strings mdy and filename are defined.
Last edited by michaelk; 01-29-2005 at 12:26 PM.
|
|
|
|
01-29-2005, 12:26 PM
|
#3
|
|
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,466
Original Poster
Rep:
|
ok so if i check argc to see if its greater then 1 this should work.
ex
Code:
if (argc == 1)
{
code here
}
|
|
|
|
01-29-2005, 12:30 PM
|
#4
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
Re: strange c issue
Quote:
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");
}
}
Last edited by Hko; 01-29-2005 at 12:32 PM.
|
|
|
|
01-29-2005, 12:31 PM
|
#5
|
|
Member
Registered: May 2004
Posts: 552
Rep:
|
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.
|
|
|
|
01-29-2005, 12:34 PM
|
#6
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
Quote:
Originally posted by randyding
Second problem, your puts() string is not terminated with a newline,
|
Not needed: puts() always appends '\n'.
|
|
|
|
01-29-2005, 12:37 PM
|
#7
|
|
Member
Registered: May 2004
Posts: 552
Rep:
|
Thanks, I forgot this.
|
|
|
|
01-29-2005, 01:27 PM
|
#8
|
|
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,466
Original Poster
Rep:
|
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 
|
|
|
|
01-29-2005, 02:23 PM
|
#9
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
Quote:
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:05 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|