Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Newbie needs help correcting code (http://www.velocityreviews.com/forums/t440743-newbie-needs-help-correcting-code.html)

zotkara 01-03-2006 08:20 PM

Newbie needs help correcting code
 
Hello. I need help correcting the following C source code.

/* coder.c */
/* Usage: coder [filename] [action]
[action]
D decrypt
C crypt
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

void start(int argc,char argv[]);
int encode_character(int ch,int val);
int decode_character(int ch,int val);

typedef crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}engine0={1,0,0,5};

int main(int argc,char *argv[])
{
start(argc,argv); // Start engine

}

void start(int argc,char argv[])
{
crypt *engine1=&engine0; // Turn on engine
if(argc!=3)
{
printf("%s\n%s\n",TITLE,USAGE);
}

/* Note that content of if(content) is case-sensitive */
else if(argv[2]==DECRYPT)
{
(engine1->fh)=fopen(argv[1],"r"); // Opens file
if((engine1->fh)<=0)
{
printf("\n\nError opening file...");
(engine1->rv)=FILE_ERROR;
}
}
}
EOF

The following is the error message output by GNU gcc compiler in Slackware
10.2 in bash.

error: syntax error before '{' token
coder.c:29: error: parse error before '}' token
coder.c:29: error: parse error before ',' token
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
docs@pyenos:~/work_and_play/c++_learn$ gcc coder.c -o coder
coder.c:22: error: syntax error before '{' token
coder.c:29: error: parse error before '}' token
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: excess elements in scalar initializer
coder.c:29: warning: (near initialization for `engine0')
coder.c:29: warning: data definition has no type or storage class
coder.c: In function `main':
coder.c:33: warning: passing arg 2 of `start' from incompatible pointer type
coder.c: In function `start':
coder.c:39: error: `crypt' undeclared (first use in this function)
coder.c:39: error: (Each undeclared identifier is reported only once
coder.c:39: error: for each function it appears in.)
coder.c:39: error: `engine1' undeclared (first use in this function)
coder.c:48: warning: passing arg 1 of `fopen' makes pointer from integer
EOF

Please help me correcting this code as I am a newbie. Thanks in advance.


Zot

Lew Pitcher 01-03-2006 08:28 PM

Re: Newbie needs help correcting code
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

zotkara wrote:
> Hello. I need help correcting the following C source code.
>
> /* coder.c */
> /* Usage: coder [filename] [action]
> [action]
> D decrypt
> C crypt
> */
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define TITLE "coder\nAbout: encrypts or decrypts file."
> #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
> #define FILE_ERROR -2
> #define DECRYPT 'd'
>
> void start(int argc,char argv[]);
> int encode_character(int ch,int val);
> int decode_character(int ch,int val);
>
> typedef crypt


what sort of type is crypt? Yes, you typedef'ed it, but what did you
typedef it to?

> {
> int rv; // Recovery value
> int ch; // Channel
> unsigned int ctr; // Counter
> int val; // Encrypt value
> char buffer[257]; // Buffer
> FILE *fh; // File handle
> }engine0={1,0,0,5};


[snip]

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDut5WagVFX4UWr64RArzUAJ91EcAdn+QYa3J4Z5sDGD jjMZdTWQCgv2+B
aMnGBLwDqYvcvgvsypv5v4s=
=Cmpy
-----END PGP SIGNATURE-----

Mike Wahler 01-03-2006 08:43 PM

Re: Newbie needs help correcting code
 

"zotkara" <zotkara@optusnet.com.au> wrote in message
news:pan.2006.01.03.20.20.13.136258@optusnet.com.a u...
> Hello. I need help correcting the following C source code.
>
> /* coder.c */
> /* Usage: coder [filename] [action]
> [action]
> D decrypt
> C crypt
> */
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define TITLE "coder\nAbout: encrypts or decrypts file."
> #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
> file\n\tc\tCrypt file"
> #define FILE_ERROR -2
> #define DECRYPT 'd'
>
> void start(int argc,char argv[]);
> int encode_character(int ch,int val);
> int decode_character(int ch,int val);
>
> typedef crypt


typedef struct crypt

> {
> int rv; // Recovery value
> int ch; // Channel
> unsigned int ctr; // Counter
> int val; // Encrypt value
> char buffer[257]; // Buffer
> FILE *fh; // File handle
> }engine0={1,0,0,5};
>
> int main(int argc,char *argv[])
> {
> start(argc,argv); // Start engine
>
> }
>
> void start(int argc,char argv[])
> {
> crypt *engine1=&engine0; // Turn on engine
> if(argc!=3)
> {
> printf("%s\n%s\n",TITLE,USAGE);
> }
>
> /* Note that content of if(content) is case-sensitive */
> else if(argv[2]==DECRYPT)
> {
> (engine1->fh)=fopen(argv[1],"r"); // Opens file
> if((engine1->fh)<=0)
> {
> printf("\n\nError opening file...");
> (engine1->rv)=FILE_ERROR;
> }
> }
> }
> EOF
>
> The following is the error message output by GNU gcc compiler in Slackware
> 10.2 in bash.
>
> error: syntax error before '{' token
> coder.c:29: error: parse error before '}' token
> coder.c:29: error: parse error before ',' token
> coder.c:29: warning: excess elements in scalar initializer
> coder.c:29: warning: (near initialization for `engine0')
> docs@pyenos:~/work_and_play/c++_learn$ gcc coder.c -o coder
> coder.c:22: error: syntax error before '{' token
> coder.c:29: error: parse error before '}' token
> coder.c:29: warning: excess elements in scalar initializer
> coder.c:29: warning: (near initialization for `engine0')
> coder.c:29: warning: excess elements in scalar initializer
> coder.c:29: warning: (near initialization for `engine0')
> coder.c:29: warning: excess elements in scalar initializer
> coder.c:29: warning: (near initialization for `engine0')
> coder.c:29: warning: data definition has no type or storage class
> coder.c: In function `main':
> coder.c:33: warning: passing arg 2 of `start' from incompatible pointer
> type
> coder.c: In function `start':
> coder.c:39: error: `crypt' undeclared (first use in this function)
> coder.c:39: error: (Each undeclared identifier is reported only once
> coder.c:39: error: for each function it appears in.)
> coder.c:39: error: `engine1' undeclared (first use in this function)
> coder.c:48: warning: passing arg 1 of `fopen' makes pointer from integer
> EOF
>
> Please help me correcting this code as I am a newbie. Thanks in advance.


OK now it compiles. Whether it does what you want I have no idea.

/* coder.c */
/* Usage: coder [filename] [action]
[action]
D decrypt
C crypt
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

//void start(int argc,char argv[]);
void start(int argc,char *argv[]);
int encode_character(int ch,int val);
int decode_character(int ch,int val);

typedef struct
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
} crypt ;

crypt engine0={1,0,0,5};

int main(int argc,char *argv[])
{
start(argc,argv); // Start engine

}

//void start(int argc,char argv[])
void start(int argc,char *argv[])

{
crypt *engine1=&engine0; // Turn on engine
if(argc!=3)
{
printf("%s\n%s\n",TITLE,USAGE);
}

/* Note that content of if(content) is case-sensitive */
// else if(argv[2]==DECRYPT)
else if(*argv[2]==DECRYPT)
{
(engine1->fh)=fopen(argv[1],"r"); // Opens file
if((engine1->fh)<=0)
{
printf("\n\nError opening file...");
(engine1->rv)=FILE_ERROR;
}
}
}

-Mike



Emmanuel Delahaye 01-03-2006 08:54 PM

Re: Newbie needs help correcting code
 
zotkara a écrit :
> Hello. I need help correcting the following C source code.

<...>
See my comments (-ed-). I have just made your code compilable. Other
errors may exist.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename]
[action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

/* -ed- missing 'struct'. It's an object : 'typedef' removed */
struct crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}
engine0 =
{
1, 0, 0, 5
};

static void start(int argc, char argv[])
{
/* -ed- missing 'struct' */
struct crypt *engine1 = &engine0; // Turn on engine
if (argc != 3)
{
printf("%s\n%s\n", TITLE, USAGE);
}

/* Note that content of if(content) is case-sensitive */
else if (argv[2] == DECRYPT)
{
(engine1->fh) = fopen(argv[1], "r"); // Opens file
if ((engine1->fh) <= 0)
{
printf("\n\nError opening file...");
(engine1->rv) = FILE_ERROR;
}
}
}

int main(int argc, char *argv[])
{
start(argc, argv); // Start engine

}

--
A+

Emmanuel Delahaye

Emmanuel Delahaye 01-03-2006 08:58 PM

Re: Newbie needs help correcting code
 
zotkara a écrit :
> Hello. I need help correcting the following C source code.

<...>
See my comments (-ed-). I have just made your code compilable. Other
errors may exist.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TITLE "coder\nAbout: encrypts or decrypts file."
#define USAGE "Usage: coder [filename]
[action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
#define FILE_ERROR -2
#define DECRYPT 'd'

/* -ed- missing 'struct'. It's an object : 'typedef' removed */
struct crypt
{
int rv; // Recovery value
int ch; // Channel
unsigned int ctr; // Counter
int val; // Encrypt value
char buffer[257]; // Buffer
FILE *fh; // File handle
}
engine0 =
{
1, 0, 0, 5
};

/* -ed- missing '*' before argv... */
static void start(int argc, char *argv[])
{
/* -ed- missing 'struct' */
struct crypt *engine1 = &engine0; // Turn on engine
if (argc != 3)
{
printf("%s\n%s\n", TITLE, USAGE);
}

/* Note that content of if(content) is case-sensitive */

/* -ed- missing [0] */
else if (argv[2][0] == DECRYPT)
{
(engine1->fh) = fopen(argv[1], "r"); // Opens file
if ((engine1->fh) <= 0)
{
printf("\n\nError opening file...");
(engine1->rv) = FILE_ERROR;
}
}
}

int main(int argc, char *argv[])
{
start(argc, argv); // Start engine

}

--
A+

Emmanuel Delahaye

zotkara 01-03-2006 09:08 PM

Thank you very much, MKW
 
..


On Tue, 03 Jan 2006 20:43:55 +0000, Mike Wahler wrote:

>
> "zotkara" <zotkara@optusnet.com.au> wrote in message
> news:pan.2006.01.03.20.20.13.136258@optusnet.com.a u...
>> Hello. I need help correcting the following C source code.
>>
>> /* coder.c */
>> /* Usage: coder [filename] [action]
>> [action]
>> D decrypt
>> C crypt
>> */
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> #define TITLE "coder\nAbout: encrypts or decrypts file."
>> #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
>> file\n\tc\tCrypt file"
>> #define FILE_ERROR -2
>> #define DECRYPT 'd'
>>
>> void start(int argc,char argv[]);
>> int encode_character(int ch,int val);
>> int decode_character(int ch,int val);
>>
>> typedef crypt

>
> typedef struct crypt
>
>> {
>> int rv; // Recovery value
>> int ch; // Channel
>> unsigned int ctr; // Counter
>> int val; // Encrypt value
>> char buffer[257]; // Buffer
>> FILE *fh; // File handle
>> }engine0={1,0,0,5};
>>
>> int main(int argc,char *argv[])
>> {
>> start(argc,argv); // Start engine
>>
>> }
>>
>> void start(int argc,char argv[])
>> {
>> crypt *engine1=&engine0; // Turn on engine
>> if(argc!=3)
>> {
>> printf("%s\n%s\n",TITLE,USAGE);
>> }
>>
>> /* Note that content of if(content) is case-sensitive */
>> else if(argv[2]==DECRYPT)
>> {
>> (engine1->fh)=fopen(argv[1],"r"); // Opens file
>> if((engine1->fh)<=0)
>> {
>> printf("\n\nError opening file...");
>> (engine1->rv)=FILE_ERROR;
>> }
>> }
>> }
>> EOF
>>
>> The following is the error message output by GNU gcc compiler in Slackware
>> 10.2 in bash.
>>
>> error: syntax error before '{' token
>> coder.c:29: error: parse error before '}' token
>> coder.c:29: error: parse error before ',' token
>> coder.c:29: warning: excess elements in scalar initializer
>> coder.c:29: warning: (near initialization for `engine0')
>> docs@pyenos:~/work_and_play/c++_learn$ gcc coder.c -o coder
>> coder.c:22: error: syntax error before '{' token
>> coder.c:29: error: parse error before '}' token
>> coder.c:29: warning: excess elements in scalar initializer
>> coder.c:29: warning: (near initialization for `engine0')
>> coder.c:29: warning: excess elements in scalar initializer
>> coder.c:29: warning: (near initialization for `engine0')
>> coder.c:29: warning: excess elements in scalar initializer
>> coder.c:29: warning: (near initialization for `engine0')
>> coder.c:29: warning: data definition has no type or storage class
>> coder.c: In function `main':
>> coder.c:33: warning: passing arg 2 of `start' from incompatible pointer
>> type
>> coder.c: In function `start':
>> coder.c:39: error: `crypt' undeclared (first use in this function)
>> coder.c:39: error: (Each undeclared identifier is reported only once
>> coder.c:39: error: for each function it appears in.)
>> coder.c:39: error: `engine1' undeclared (first use in this function)
>> coder.c:48: warning: passing arg 1 of `fopen' makes pointer from integer
>> EOF
>>
>> Please help me correcting this code as I am a newbie. Thanks in advance.

>
> OK now it compiles. Whether it does what you want I have no idea.
>
> /* coder.c */
> /* Usage: coder [filename] [action]
> [action]
> D decrypt
> C crypt
> */
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define TITLE "coder\nAbout: encrypts or decrypts file."
> #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
> file\n\tc\tCrypt file"
> #define FILE_ERROR -2
> #define DECRYPT 'd'
>
> //void start(int argc,char argv[]);
> void start(int argc,char *argv[]);
> int encode_character(int ch,int val);
> int decode_character(int ch,int val);
>
> typedef struct
> {
> int rv; // Recovery value
> int ch; // Channel
> unsigned int ctr; // Counter
> int val; // Encrypt value
> char buffer[257]; // Buffer
> FILE *fh; // File handle
> } crypt ;
>
> crypt engine0={1,0,0,5};
>
> int main(int argc,char *argv[])
> {
> start(argc,argv); // Start engine
>
> }
>
> //void start(int argc,char argv[])
> void start(int argc,char *argv[])
>
> {
> crypt *engine1=&engine0; // Turn on engine
> if(argc!=3)
> {
> printf("%s\n%s\n",TITLE,USAGE);
> }
>
> /* Note that content of if(content) is case-sensitive */
> // else if(argv[2]==DECRYPT)
> else if(*argv[2]==DECRYPT)
> {
> (engine1->fh)=fopen(argv[1],"r"); // Opens file
> if((engine1->fh)<=0)
> {
> printf("\n\nError opening file...");
> (engine1->rv)=FILE_ERROR;
> }
> }
> }
>
> -Mike



zotkara 01-03-2006 09:08 PM

Thank you very much EMD
 
..
On Tue, 03 Jan 2006 21:58:07 +0100, Emmanuel Delahaye wrote:

> zotkara a écrit :
>> Hello. I need help correcting the following C source code.

> <...>
> See my comments (-ed-). I have just made your code compilable. Other
> errors may exist.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define TITLE "coder\nAbout: encrypts or decrypts file."
> #define USAGE "Usage: coder [filename]
> [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
> #define FILE_ERROR -2
> #define DECRYPT 'd'
>
> /* -ed- missing 'struct'. It's an object : 'typedef' removed */
> struct crypt
> {
> int rv; // Recovery value
> int ch; // Channel
> unsigned int ctr; // Counter
> int val; // Encrypt value
> char buffer[257]; // Buffer
> FILE *fh; // File handle
> }
> engine0 =
> {
> 1, 0, 0, 5
> };
>
> /* -ed- missing '*' before argv... */
> static void start(int argc, char *argv[])
> {
> /* -ed- missing 'struct' */
> struct crypt *engine1 = &engine0; // Turn on engine
> if (argc != 3)
> {
> printf("%s\n%s\n", TITLE, USAGE);
> }
>
> /* Note that content of if(content) is case-sensitive */
>
> /* -ed- missing [0] */
> else if (argv[2][0] == DECRYPT)
> {
> (engine1->fh) = fopen(argv[1], "r"); // Opens file
> if ((engine1->fh) <= 0)
> {
> printf("\n\nError opening file...");
> (engine1->rv) = FILE_ERROR;
> }
> }
> }
>
> int main(int argc, char *argv[])
> {
> start(argc, argv); // Start engine
>
> }



zotkara 01-03-2006 09:11 PM

Re: Thank you very much EMD
 
/* -ed- missing [0] */
else if (argv[2][0] == DECRYPT)
{

Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.




Richard Heathfield 01-03-2006 09:14 PM

Re: Thank you very much EMD
 
zotkara said:

> /* -ed- missing [0] */
> else if (argv[2][0] == DECRYPT)
> {
>
> Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.


Assuming this is the second argument to main:

argv[2] has type char *.
argv[2][0] has type char.
Therefore, argv[2][0] cannot, as you claim, have the same value as argv[2].

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

zotkara 01-03-2006 09:15 PM

Re: Thank you very much EMD
 
On Wed, 04 Jan 2006 08:11:16 +1100, zotkara wrote:

> /* -ed- missing [0] */
> else if (argv[2][0] == DECRYPT)
> {
>
> Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.


Sorry for multi-posting. Maybe because arv[2][(0+int line_entered)] when
line_entered from screen after the first scan exceeds one then it becomes
an issue, where argv[2][(0+n)]!=argv[2]. I think in this particular code
in the context of the program code it produces no direct errors but it is
not a correct way of coding, so I guess your point is valid.



All times are GMT. The time now is 10:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.