Help. Just getting started

E

Eric

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
 
V

Victor Bazarov

Eric said:
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 said:

V
 
A

Alf P. Steinbach

* 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

ajk

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
 
I

Ioannis Vranos

Eric said:
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>

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).
 
E

Eric

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

Ioannis Vranos

Eric said:
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[])
 
I

Ioannis Vranos

Eric said:
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
 
A

Alf P. Steinbach

* 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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top