LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-29-2005, 11:49 AM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.
 
Old 01-29-2005, 12:19 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,813

Rep: Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958
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.
 
Old 01-29-2005, 12:26 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
ok so if i check argc to see if its greater then 1 this should work.


ex
Code:
 

if (argc == 1) 
        {
             code here
         }
 
Old 01-29-2005, 12:30 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 01-29-2005, 12:31 PM   #5
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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.
 
Old 01-29-2005, 12:34 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by randyding
Second problem, your puts() string is not terminated with a newline,
Not needed: puts() always appends '\n'.
 
Old 01-29-2005, 12:37 PM   #7
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Thanks, I forgot this.
 
Old 01-29-2005, 01:27 PM   #8
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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
 
Old 01-29-2005, 02:23 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strange FTP issue tanmaya Linux - Networking 2 05-23-2005 07:15 AM
strange kernel issue bograt Linux - Software 2 02-16-2005 01:32 PM
help me on a strange issue. newpenguin Linux - General 11 10-06-2002 03:42 PM
Strange sound issue UKer Slackware 1 09-21-2002 09:52 AM
Strange HTTP issue cyberkatis Linux - Networking 2 02-07-2002 10:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration