parser


L

leeho94

hello everyone.
Who can help me telling me what's wrong in the following code?

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

#define BUFSIZE 1024
#define max 1000

void write(char *sentence);
FILE *fin,*fout;


enum gulja {man,me,ne};
const char * guljas[]= {"man","me","ne"};

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

enum gulja gul;
char delims[] = " ";
char *result;
char *str;
char name;
int i=0;

static char temp[BUFSIZE];

int fc1;

str = (char*)malloc(sizeof(char));

printf("File input:");
scanf("%s",&name);
fin=fopen(&name,"r");
fread(str,sizeof(char),BUFSIZE,fin);
result = strtok( str, delims );

fout = fopen("result.txt","a");

while(result != NULL)
{

if(strcmp(gul,result))==0){


switch(gul)
{

case 0 : fprintf(fout,"³ª", result );
break;

case 1 : fprintf(fout,"»ç¶÷", result );
break;

case 2 : fprintf(fout,"±Û½ê", result );
break;

case '4' : fprintf(fout,"4", result );
break;

case '5' : fprintf(fout,"5", result );
break;

default : fprintf(fout,"6", result );
break;
}
}

result = strtok( NULL, delims );


fc1 = fclose(fout);

return 0;

}
void write(char *sentence)
{

fprintf(fout,"%s",sentence);

}
 
Ad

Advertisements

J

jacob navia

It is obvious after you compile it. The compiler will tell you what's wrong and the
line where is the error.

Just compile it.
 
Ad

Advertisements

J

Jens Thoms Toerring

leeho94 said:
Who can help me telling me what's wrong in the following code?

Perhaps you should explain what the program is supposed to do
(and what's it doing instead)...
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 1024
#define max 1000
void write(char *sentence);
FILE *fin,*fout;
enum gulja {man,me,ne};
const char * guljas[]= {"man","me","ne"};
int main(int argc, char* argv[])
{
enum gulja gul;
char delims[] = " ";
char *result;
char *str;
char name;
int i=0;
static char temp[BUFSIZE];
str = (char*)malloc(sizeof(char));

Beside the unnecessary cast there's a big probem here. You allocate
space for a singe character only (BTW, sizeof(char) is 1 by definition).
So all you can store there is a single char (or an empty string). And
whenever you use malloc() and friends you should check if it didn't
return NULL.
printf("File input:");
scanf("%s",&name);

'name' is a single character. So there's no room there for storing
a file name! If you're lucky your program crashes here, if you're
unlucky it will look as if it's working properly and misbehave some
time later... What you need is an array long enough to hold what-
ever the user enters (or you must read only as much as that array
can hold). scanf() can be a bit difficult to use and the usual
recommendation is to use fgets().
fin=fopen(&name,"r");
fread(str,sizeof(char),BUFSIZE,fin);

And here you try to read BUFSIZE chars into a location where
you only have room for a single one. That's a sure recipe for
disaster. (And checking if fopen() did succeed would also be
advisable).
result = strtok( str, delims );

Even if 'str' would point to enough memory there's no obvious
guarantee that what you read in has a '\0' character somewhere,
i.e. that it's a string. That's especially true if your input
file is a text file. But now you use a function that requires
a string, i.e. a char array that contains a '\0' somehwere to
mark the end of the string.
fout = fopen("result.txt","a");
while(result != NULL)
{
if(strcmp(gul,result))==0){

'result' is a string pointer and 'gul' is an enumeration. But
strmp() requires two string pointers as its arguments. Your
compiler should have complained loudly about this. Moreover,
the value of 'gul' has never been set before, so you're com-
paring th string 'result' points to to a string at a random
place in memory.

I would recommend that you take another look at your textbooks
and re-read the parts about pointers and memory allocation be-
fore you set out to write a parser...

Regards, Jens
 

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

Top