New C learner need help on a syntax question

This is a discussion on New C learner need help on a syntax question within the C Programming forums, part of the General Programming Boards category; I have read the Cprogramming.com to learn C, but I am facing a basic syntax problem. I need to write ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    Unhappy New C learner need help on a syntax question

    I have read the Cprogramming.com to learn C, but I am facing a basic syntax problem.

    I need to write a very simple program (mult). It need take a command line input in cmd.exe

    For example: mult 2 3 abc.txt

    Then the program will calculate 2*3 and then save the result in abc.txt.

    I know how to do it if 2,3 and abc.txt are not input variables, but I don't know how to do it now.

    Please help

    ===============================

    I just read more from Cprogramming.com Tutorial: Command-Line Arguments

    I will try it first, but I would still be very happy if someone gives me an example.
    Last edited by zmarcoz; 01-10-2012 at 08:00 PM. Reason: Just read more

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Lookup atoi or strtol for converting the command line strings into numbers.
    Lookup fopen for opening a file.
    Lookup fprintf for writing to a file.

    Feel free to post your code if you want more in depth help.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Command-line input will be accessible in the program as argv[1], argv[2], argv[3], argv[4] containing "mult", "2", "3", "abc.txt" respectively. As long as these parameters are separated by spaces and contain no spaces themselves there is no syntax problem.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Command-line input will be accessible in the program as argv[1], argv[2], argv[3], argv[4] containing "mult", "2", "3", "abc.txt" respectively. As long as these parameters are separated by spaces and contain no spaces themselves there is no syntax problem.
    Actually that would be argv[0], argv[1], argv[2] and argv[3] ... I'm betting mult is the program name.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You may be right. He did mention "mult" is program name. I was hoping increased flexibility would allow use of "mult", "add", "subtract" etc., towards a more general calculator. I also got confused with the mention of cmd.exe - as if that could be his program. I realize it's also the name the command processor.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    You may be right. He did mention "mult" is program name. I was hoping increased flexibility would allow use of "mult", "add", "subtract" etc., towards a more general calculator. I also got confused with the mention of cmd.exe - as if that could be his program. I realize it's also the name the command processor.
    Ahhh man... I've had days like that, too ... bummer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New learner Help!
    By 2001beibei in forum C Programming
    Replies: 2
    Last Post: 06-02-2008, 10:06 AM
  2. Help for the learner
    By inmaterichard in forum Game Programming
    Replies: 3
    Last Post: 08-16-2007, 01:55 AM
  3. 2 Noobish Questions from a 2nd Day Learner
    By Bucket in forum C++ Programming
    Replies: 18
    Last Post: 08-25-2006, 02:20 PM
  4. syntax question
    By 182 in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2006, 03:36 PM
  5. Learner...
    By Hugo716 in forum C++ Programming
    Replies: 13
    Last Post: 04-21-2006, 04:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21