Thread: Program works for stdin but fails for a file.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    10
    Thanks,
    Such a dumb error.

    But hey, why did you say it's obvious I didn't write it?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Quote Originally Posted by felixthunder View Post
    But hey, why did you say it's obvious I didn't write it?
    It struck me as an old-fashioned style. Maybe you're using an old book. Or have an old teacher. Whatever.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by john.c View Post
    It struck me as an old-fashioned style. Maybe you're using an old book. Or have an old teacher. Whatever.
    @felixthunder

    The program could be written much simpler. getopt() is not needed. gcc seems to being used, assuming argv[0] does contain the program name.
    argc == 1, use stdin
    argc == 2, attempt to open the alleged filename provided as argv[1]
    argc > 2, too many arguments, print an error message and exit

    For a one .c file program, I would not bother with static functions, not wrong, but not needed

    @john.c
    Maybe you're using an old book. Or have an old teacher.
    "Old teacher"???

    The age of the teacher is not the problem! Bad, so-called, teachers, instructors, professors, etc... can be of any age, young or old!

    Teachers teaching using Turbo C, using K&R as the primary textbook, etc... Yes, it is still being done!

    An individual attempting to learn C using painfully inadequate resources, such as, online tutorials, YouTube videos, poorly written online code, etc..., rather than studying from an up to date textbook. Seen this too many times.

    BTW, yes, I am an older teacher! ;^)

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    10
    Yes, I am using old books, most from Kernighan and Rob Pike.
    I wrote this program for practicing with stacks, but I have another version that uses arrays: https://pastebin.com/raw/mmi0CCZR

    I am also learning about BSD extensions, such as err(3).

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by felixthunder View Post
    Yes, I am using old books, most from Kernighan and Rob Pike.
    I wrote this program for practicing with stacks, but I have another version that uses arrays: https://pastebin.com/raw/mmi0CCZR

    I am also learning about BSD extensions, such as err(3).
    In addition to my comments above, rather than using an old book, I would recommend one of the following up to date books on the C Programming Language:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    Choose one, study cover to cover, and do all the exercises at the end of each chapter!
    I am also learning about BSD extensions, such as err(3).
    I also would recommend sticking to Standard C, and avoid any Non-Standard extensions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-20-2014, 03:04 PM
  2. Compile under gcc fails, but VS2010 works
    By Sorin in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2012, 02:45 AM
  3. This works fine with IP 127.0.0.1 but fails with my REAL IP.
    By azjherben in forum Networking/Device Communication
    Replies: 15
    Last Post: 05-19-2009, 10:28 PM
  4. Replies: 6
    Last Post: 05-08-2009, 08:51 PM
  5. Replies: 5
    Last Post: 04-13-2009, 09:55 AM

Tags for this Thread