LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   strange c issue (https://www.linuxquestions.org/questions/programming-9/strange-c-issue-283723/)

exvor 01-29-2005 11:49 AM

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 ?

michaelk 01-29-2005 12:19 PM

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.

exvor 01-29-2005 12:26 PM

ok so if i check argc to see if its greater then 1 this should work.


ex
Code:



if (argc == 1)
        {
            code here
        }


Hko 01-29-2005 12:30 PM

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");
    }
}


randyding 01-29-2005 12:31 PM

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.

Hko 01-29-2005 12:34 PM

Quote:

Originally posted by randyding
Second problem, your puts() string is not terminated with a newline,
Not needed: puts() always appends '\n'.

randyding 01-29-2005 12:37 PM

Thanks, I forgot this.

exvor 01-29-2005 01:27 PM

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 :)

Hko 01-29-2005 02:23 PM

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.


All times are GMT -5. The time now is 03:17 AM.