By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

Help. Just getting started

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
* 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?
Jul 23 '05 #3

P: n/a
ajk
On 1 Feb 2005 13:49:40 -0800, "Eric" <ri*****@yahoo.com> 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.
Jul 23 '05 #4

P: n/a
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
Jul 23 '05 #5

P: n/a
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" );
}

Jul 23 '05 #6

P: n/a
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
Jul 23 '05 #7

P: n/a
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/publ...nner_s_c__.htm


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #8

P: n/a
* 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?
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.