Help. Just getting started

Discussion in 'C++' started by Eric, Feb 1, 2005.

  1. Eric

    Eric Guest

    I have a book Learn C in 21 days. But I'm using visual C++ as the
    compiler.

    Here is my code. What do I need to change to make it work with Visual
    C++.

    // list_it.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"

    void display_usage(void);

    int line;

    int _tmain(int argc, _TCHAR* argv[])
    {
    char buffer[256];
    FILE *fp; <----(PROBLEM SEEMS TO BE HERE)

    if( argc < 2)
    {
    display_usage();
    exit(1);
    }

    /* if (( fp = fopen( argv[1], "r" )) == NULL )
    {
    fprintf( stderr, "Error opening file, %s!, argv[1] );
    exit(1);
    }
    */
    line = 1;

    while( fgets( buffer, 256, fp ) != NULL )
    fprintf( stdout, "%4d:\t%s", line++, buffer );
    fclose(fp);
    return 0;
    }

    void display_usage(void)
    {
    fprintf(stderr, "\nProper Usage is: " );
    fprintf(stderr, "\n\nLIST_IT filename.ext\n" );
    }


    Thanks,
    Eric
     
    Eric, Feb 1, 2005
    #1
    1. Advertising

  2. Eric wrote:
    > I have a book Learn C in 21 days.


    C or C++? It matters, you know.

    > But I'm using visual C++ as the
    > compiler.
    >
    > Here is my code. What do I need to change to make it work with Visual
    > C++.
    >
    > // list_it.cpp : Defines the entry point for the console application.
    > //
    >
    > #include "stdafx.h"
    >
    > void display_usage(void);
    >
    > int line;
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > char buffer[256];
    > FILE *fp; <----(PROBLEM SEEMS TO BE HERE)


    'FILE' is a type defined in <stdio.h>. You need to include that header.

    > [...]


    V
     
    Victor Bazarov, Feb 1, 2005
    #2
    1. Advertising

  3. * Eric:
    > I have a book Learn C in 21 days.


    There is more than one book with that title. One of them is
    extremely bad. Probably that's the one you've got.


    > But I'm using visual C++ as the compiler.


    Doesn't matter.


    > Here is my code. What do I need to change to make it work with Visual
    > C++.


    Don't.

    It will only suck you further down.

    To get on track, see (blatant plug for my own tutorial)

    <url: http://home.no.net/dubjai/win32cpptut/html/>

    and get yourself a decent book, e.g. "Accelerated C++".

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 1, 2005
    #3
  4. Eric

    ajk Guest

    On 1 Feb 2005 13:49:40 -0800, "Eric" <> wrote:

    >I have a book Learn C in 21 days. But I'm using visual C++ as the
    >compiler.
    >
    >Here is my code. What do I need to change to make it work with Visual
    >C++.
    >
    >// list_it.cpp : Defines the entry point for the console application.
    >//
    >
    >#include "stdafx.h"
    >
    >void display_usage(void);
    >
    >int line;
    >
    >int _tmain(int argc, _TCHAR* argv[])
    >{
    >char buffer[256];
    >FILE *fp; <----(PROBLEM SEEMS TO BE HERE)
    >
    >if( argc < 2)
    >{
    >display_usage();
    >exit(1);
    >}


    missing

    #include <stdio.h>

    which isn't something vc++ specific, it is std c containing the
    declaration of FILE among other things.

    hth/ajk
    --
    "Those are my principles. If you don't like them I have others."
    Groucho Marx.
     
    ajk, Feb 2, 2005
    #4
  5. Eric wrote:

    > I have a book Learn C in 21 days. But I'm using visual C++ as the
    > compiler.



    That's nice. This is how I got started with C90.



    > Here is my code. What do I need to change to make it work with Visual
    > C++.
    >
    > // list_it.cpp : Defines the entry point for the console application.
    > //



    C files should be saved with .c extension. C++ files should be saved
    with .cpp extension.



    > #include "stdafx.h"



    Not standard C header. You should not create a new project in VC++, but
    only a new source code file.


    #include <stdio.h>

    #include <stdlib.h>


    >
    > void display_usage(void);
    >
    > int line;
    >
    > int _tmain(int argc, _TCHAR* argv[])



    int main(int argc, char *argv[])



    > {
    > char buffer[256];
    > FILE *fp; <----(PROBLEM SEEMS TO BE HERE)



    That one is OK.



    >
    > if( argc < 2)
    > {
    > display_usage();
    > exit(1);



    or better: return EXIT_FAILURE; (or exit(EXIT_FAILURE); but there is no
    need to use EXIT() here).




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Feb 2, 2005
    #5
  6. Eric

    Eric Guest

    Thanks to everyone. My problem is solved. Thanks for the tutorial Alf
    P. Steinbach. Here is what I ended up with. I program in Visual basic a
    lot and trying to learn Visual C++. Thought I would learn C since I had
    a book handy and climb the old ladder of knowledge.

    Knowledge is power! and tons of responsibility

    God Bless

    #include "stdafx.h"
    #include <stdio.h> <---Added this

    void display_usage(void);

    int line;

    main(int argv, char *argc[]) <---changed the syntax of this
    {
    char buffer[256];
    FILE *fp;

    if( argv < 2)
    {
    display_usage();
    exit(1);
    }

    if (( fp = fopen( argc[1], "r" )) == NULL )
    {
    fprintf( stderr, "Error opening file, %s!", argc[1] ); <---found a
    missing quote(Stupied quotes)
    exit(1);
    }

    line = 1;

    while( fgets( buffer, 256, fp ) != NULL )
    fprintf( stdout, "%4d:\t%s", line++, buffer );
    fclose(fp);
    return 0;
    }

    void display_usage(void)
    {
    fprintf(stderr, "\nProper Usage is: " );
    fprintf(stderr, "\n\nLIST_IT filename.ext\n" );
    }
     
    Eric, Feb 2, 2005
    #6
  7. Eric wrote:

    > Thanks to everyone. My problem is solved. Thanks for the tutorial Alf
    > P. Steinbach. Here is what I ended up with. I program in Visual basic a
    > lot and trying to learn Visual C++. Thought I would learn C since I had
    > a book handy and climb the old ladder of knowledge.



    Since you are talking about C, and C and C++ are *different* languages
    (although C++ retains most of ISO C 1990 standard as a subset), you had
    better refer to comp.lang.c.




    >
    > Knowledge is power! and tons of responsibility
    >
    > God Bless
    >
    > #include "stdafx.h"
    > #include <stdio.h> <---Added this
    >
    > void display_usage(void);
    >
    > int line;
    >
    > main(int argv, char *argc[]) <---changed the syntax of this



    int main(int argc, char *argv[])




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Feb 2, 2005
    #7
  8. Eric wrote:

    > Thanks to everyone. My problem is solved. Thanks for the tutorial Alf
    > P. Steinbach. Here is what I ended up with. I program in Visual basic a
    > lot and trying to learn Visual C++. Thought I would learn C since I had
    > a book handy and climb the old ladder of knowledge.



    Also learning C is not required to to learn C++, and in fact is not
    recommended.


    If you are interested in C++ try get a C++ book and learn it.


    Check this page of mine:

    http://www23.brinkster.com/noicys/learningcpp.htm


    And these book reviews:

    http://www.accu.org/bookreviews/public/reviews/0sb/beginner_s_c__.htm




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Feb 2, 2005
    #8
  9. * Ioannis Vranos:
    >
    > Also learning C is not required to to learn C++, and in fact is not
    > recommended.


    If I might add to that:

    it's so common a mistake to try to learn C first that this is a FAQ:

    <url: http://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html#faq-28.2>.

    <quote>
    [learning C] will not only waste your time, but it will teach you a bunch of
    things that you'll explicitly have to un-learn when you finally get back on
    track and learn OO/C++ (e.g., malloc(), printf(), unnecessary use of switch
    statements, error-code exception handling, unnecessary use of #define
    macros, etc.).
    </quote>

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 2, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave

    Just getting started

    Dave, Aug 8, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    402
  2. abillmeier

    Just getting started

    abillmeier, Mar 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    390
    abillmeier
    Mar 7, 2006
  3. W. Watson
    Replies:
    18
    Views:
    561
    Jussi Salmela
    Jan 8, 2007
  4. Sheldon Rabin

    Just getting started in C++, C+

    Sheldon Rabin, Jan 7, 2007, in forum: C++
    Replies:
    3
    Views:
    325
    fungus
    Jan 8, 2007
  5. paul

    just getting started

    paul, Dec 18, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    184
Loading...

Share This Page