?
Solved

Debug question

Posted on 2000-04-11
10
Medium Priority
?
164 Views
Last Modified: 2010-04-15
Could someone please look at this code and see why I am getting an error in regards to MAX_REC_SIZE.  What the code is suppose to do is open a text file with writing in it and move word to word comparing each word in the text file to the five words that have been entered as key words.  I would really appreciate the help.  Thanks

Error has to do with MAX_REC_SIZE

#include <stdio.h>
#define FILE_NAME "bob"
#include <stdlib.h>
#include <string.h>


 main ()
{

      FILE *in_file;
      char one[10];
      char two[10];
      char three[10];
      char four[10];
      char five[10];
      char word[MAX_REC_SIZE + 1];
      
      int countone = 0, counttwo = 0, countthree = 0, countfour = 0, countfive = 0, counttotal = 0;
      
      in_file = fopen(FILE_NAME, "r");
      
      if (in_file == NULL) {
            printf("Cannot open %s\n",FILE_NAME);
            exit(8);
      }
      
      
      /* Copying keywords to buffers */
      strcpy(one,"the");
      strcpy(two,"dog");
      strcpy(three,"cat");
      strcpy(four,"house");
      strcpy(five,"printer");
      
      while (fgets(word,MAX_REC_SIZE + 1,in_file) != NULL) {
            if(strcmp(one,word) == 0) {
                  countone++;
                  counttotal++;
            }
            else if(strcmp(two,word) == 0) {
                  counttwo++;
                  counttotal++;
            }
            else if(strcmp(three,word) == 0) {
                  countthree++;      
                  counttotal++;
            }
            else if(strcmp(four,word) == 0) {
                  countfour++;
                  counttotal++;
            }
            else if(strcmp(five,word) == 0) {
                  countfive++;
                  counttotal++;
            }
            else
                  counttotal++;
      }
      
      printf("%i\n",countone);
      printf("%i\n",counttwo);
      printf("%i\n",countthree);      
      printf("%i\n",countfour);
      printf("%i\n",countfive);
      printf("%i\n",counttotal);
      
      fclose(in_file);
      
      return(0);
}
0
Comment
Question by:BCBob2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 150 total points
ID: 2706262
Since all words,one to five are less or equal 10 letter.why dont you simply sepcify 10 in the word?
0
 

Expert Comment

by:mohitdas
ID: 2706473
u should define MAX_REC_SIZE to 11 and try.  
or
===
while (fgets(word,MAX_REC_SIZE + 1,in_file) != NULL) {
 change this to
while (fgets(word,MAX_REC_SIZE,in_file) != NULL) {

or
==
change the size of variables one,two etc to 11.
 
coz' may be the size of two variables. like one and word or two and word giving problems while comparing.

:)
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2706957
I agry with mohitdas
U can not put MAX_REC_SIZE + 1 in

>>char word[MAX_REC_SIZE + 1];

statement...
So correct it...and see the results
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:mohitdas
ID: 2707004
hope u'll try and accept my ans. : )
0
 
LVL 1

Expert Comment

by:mahno
ID: 2707074
Hi,

Check Ur file "bob"
May be in this file stings edns with "\n\r"? (0x0D 0x0A)

In this case replace:
strcpy(one,"the");
strcpy(two,"dog");
strcpy(three,"cat");
strcpy(four,"house");
strcpy(five,"printer");
with
strcpy(one,"the\n");
strcpy(two,"dog\n");
strcpy(three,"cat\n");
strcpy(four,"house\n");
strcpy(five,"printer\n");

or write data to file corectly

Hope this helps,
  mahno

0
 

Expert Comment

by:mohitdas
ID: 2707104
i don't think its necessary as strcpy itself appends '\n' character at the end of every string.
0
 
LVL 2

Expert Comment

by:kamarey
ID: 2707187
Hello,
I agree with MAHNO. Every string ends with the character '\n', if you open a file in text mode, and with the '\r\n', if you open a file in binary mode. By default a file opens in text mode.
And define MAX_REC_SIZE as some big value(like 100 or 1000), becouse the function
        fgets(char *buf,int max_num,FILE *file);
reads either max_num-1 characters or a newline character('\n').
0
 
LVL 3

Expert Comment

by:marcjb
ID: 2707614
strcpy does NOT append a '\n' to the end of every string.  It is a character for character copy until the NULL terminator is reached.

If you want, use strtok after reading in a line to get rid of the '\n'.  For example:

while (fgets(word,MRS,in_file) != NULL)
  {
  strtok(word, "\n");
  }

OR, once the string is read in, a less efficient method would be:

len = strlen(word);
if ( len > 0 && word[len-1] == '\n' )
    word[len-1] = 0; /* or '\0' */



Second, you do not have to declare your character arrays with MAX_REC_SIZE + 1, as kamarey correctly points out that fgets actually reads one less than whatever you pass in (leaving room to append a NULL).  Therefore, the following will work provided that MAX_REC_SIZE is big enough to hold a record.

  char var[MAX_REC_SIZE];

  fgets ( var, MAX_REC_SIZE, fp);
    /* where fp is a file pointer */


>> Every string ends with the character '\n', if you open a file in text mode, and with the '\r\n', if you open a file in binary mode.

The above statement is not correct on all platforms, as '\n' is platform specific.  On DOS (and Windows) '\n' corresponds to the CRLF pair.  On UNIX, it does not matter if you open a file in binary or text mode, as '\n' corresponds to a single character, \x0A (LF).
0
 
LVL 1

Expert Comment

by:mahno
ID: 2707712
marcjb:
>>strcpy does NOT append a '\n' to the end of every string.  It is a character for character copy until the NULL terminator is reached.

strcpy append an '\n' character to the end of string if we have written:
strcpy(one,"the\n")
0
 
LVL 3

Expert Comment

by:marcjb
ID: 2708056
i agree that as written
strcpy(one,"the\n")
will copy the '\n', as it is part of the string.  I just wanted to make sure that it was understood the '\n' is not appended as part of the function.  

Sorry I wasn't more clear :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question