Solved

Debug question

Posted on 2000-04-11
10
158 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 50 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to scan all sheets 3 134
Windows Updates failing due to Diskpart not configured correctly 8 156
MS Excel duplicate input detect. 8 46
Bitwise and to sum elements 2 52
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

739 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