Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

Debug question

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
BCBob2002
Asked:
BCBob2002
  • 3
  • 2
  • 2
  • +3
1 Solution
 
WynCommented:
Since all words,one to five are less or equal 10 letter.why dont you simply sepcify 10 in the word?
0
 
mohitdasCommented:
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
 
terajivCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mohitdasCommented:
hope u'll try and accept my ans. : )
0
 
mahnoCommented:
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
 
mohitdasCommented:
i don't think its necessary as strcpy itself appends '\n' character at the end of every string.
0
 
kamareyCommented:
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
 
marcjbCommented:
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
 
mahnoCommented:
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
 
marcjbCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now