简体   繁体   中英

error while reading input from file c

I had been trying to read input from a file, but it seems that something doesn't work correctly...Instead of reading the word "Words" that exists in the text,the printf is always showing 2 additional random characters not included in the file...

The function is:

void search(struct word *w,FILE *f){

    char *c;
    char c2;
    int i,j,k,l;
    c=(char*)malloc(120*sizeof(char));                     

    i=1;             
    while(f!=NULL) { 
        c2=fgetc(f);

        while(c2!=EOF) {
           while(c2!='\n') {  
              k=0;
              while(c2!=' ') {
                  *(c+k)=c2;
                  k=k+1;
                  c2=getc(f);
              }
              if(w->name==c) 
                  insert(i,j+1,name,&w);
              }

              memset(c, 0, sizeof(c));
              j=j+k+1;
          }
          i=i+1;
       }
    }
}

the main function is

int main()
{
    struct word *s;
    s=(struct word*)malloc(sizeof(struct word));
    s->name=(char*)malloc(20*sizeof(char));
    s->result=NULL;
    scanf("%s",s->name);
    search(s);
    printres(s);
    system("pause");
    exit(0);
}

and the structs are

struct position
{
    char *filename;
    int line;
    int place;
    struct position *next;
};
struct word
{
    char *name;
    struct word *right;
    struct word *left;
    struct position *result;
};

Why do these additional 2 characters appear? What should I do?

at first glance, this seems wrong to me

memset(c, 0, sizeof(c));

mainly because c it's a char* , then sizeof(c) will depend of compilation details... And this test is wrong also:

if(w->name=c)  

you probably want

if(strcmp(w->name,c) == 0)

after terminating the buffer by '\\0'.

Also, you should check to not overflow your buffer, otherwise results will be unpredictable.

Indentation means you're not arranging your code in a properly readable manner.

I assume you're trying to store words line by line and word by word from a text file. But I'm unable to make out what your insert() function does. Besides, this code has a lot of faults.

I'll list them out first, and then you can state what you're exactly doing in this.

  1. in main(): search(s)->file pointer parameter missing.

    The prototype is : void search(struct word *w,FILE *f).

    You have to do the following:

    • first open the file by using fopen() function .
    • use the pointer obtained from step i. and insert it as a parameter in your search() function.

    This is just the solution I'm giving, you lack knowledge in this. You'll have to read a lot more on using files in c.

  2. statement if(w->name=c) :

    • It's "==" and not "=". Here you only assigned w->name as c.
    • You were trying to compare pointers and not the characters in them! Comparing pointers will be no use. You allocated different memory for both, how can the addresses be same? Read more on comparing strings in c.
    • Before comparing, you have to terminate a string with '\\0'(null character). This often leads to unwanted characters being printed otherwise.

There'll be a lot of resources online which will have the exact answer to what you want to do. You can use a Google search. I can only point out so many faults, since your entire code has faults here and there. Learn more I'd say.

I haven't studied the logic of your code, so I'm just going to point out some of the problems:

c=(char*)malloc(120*sizeof(char));                     

The cast here is mostly useless. It can be used to warn you when you try to allocate data of the wrong type, but many people recommend the following way to allocate memory:

c = malloc (120 * sizeof *c);

This automatically allocates 120 items of the right size, regardless of the actual type of c . You should also check the return value of malloc() . Allocations can fail.

char c2;
c2=fgetc(f);

while(c2!=EOF) {

The fgetc() function returns a signed int representing a character in the range of an unsigned char , or EOF which is a negative value.

Plain char is either compatible with signed char or unsigned char . If it is unsigned , then it can never store the value of EOF , so the comparison c2 != EOF will always be false. If it is signed , then it can (typically, not necessarily) store EOF , but it will have the same value as an actual character.

You should store the return value of fgetc() in an int variable, compare it to EOF , and then convert it to char .

if(w->name==c) 

Is this meant to be a string comparison? It doesn't work that way in C. You're only comparing pointers. To compare the actual strings, you'd have to call strcmp() or similar.

insert(i,j+1,name,&w);

(Undefined function)

memset(c, 0, sizeof(c));

I assume that you're trying to set the entire buffer to 0 , but the size is wrong. This zeroes sizeof (char *) bytes, not the 120 bytes you allocated.

s=(struct word*)malloc(sizeof(struct word));
s->name=(char*)malloc(20*sizeof(char));

Pointless casts an no check for allocation failure. This should be ( in my opinion ):

s = malloc (sizeof *s);
if (s) {
  s->name = malloc (20 * sizeof *s->name);
}
if (s && s->name) {
  /* Go to work */
} else {
  /* Allocation failure */
}

/* All done. Free memory. */
if (s) {
  free (s->name);
}
free (s);

Because free(0) is a no-op, this cleanup works with all of the branches above.

search(s);

The search() function wants a second argument ( FILE * ).

printres(s);

(Undefined function)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM