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) 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 ? |
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. |
ok so if i check argc to see if its greater then 1 this should work.
ex Code:
|
Re: strange c issue
Quote:
Code:
else if (argv[1] == NULL) /* Maybe OK, but I'd do: else if (argc < 2) */ |
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. |
Quote:
|
Thanks, I forgot this.
|
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 :) |
Quote:
Quote:
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. |
All times are GMT -5. The time now is 01:39 PM. |