Archived

This topic is now archived and is closed to further replies.

Bacardi34

Not quite understanding this bug..

Recommended Posts

Bacardi34    122
Im sure this is a simple error, but im *Kinda* new, thanks for the help in advance! Have a program that reads words from a file, and adds them to a line buffer, then when line is full prints one line at a time. int getWord(char buffer[], FILE *getty); int output(char wbuffer[], char lbuffer[], FILE *getty); void main() { FILE *getty; getty = fopen("getty.dat", "r"); char lbuff[62] = {0}; char wbuff[25] = {0}; while (!(feof(getty))) { getWord(wbuff, getty); output(wbuff, lbuff, getty); printf("Lbuff Len: %d\n", strlen(lbuff)); //Debug } printf("Lbuff Len: %d\n", strlen(lbuff)); //Debug Code, will not print out for me printf("HEllo"); // Debug code, also not printing. program exits before this point... fclose(getty); } Well the program is exiting after the loop, will not print any code after the fact.. Any idea what the hell im doing wrong!?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Without seeing the code for your getWord or output functions I can only speculate that you might be experiencing an exception. Wrap your main function in a try..catch block and print out something from the catch(...) block. It will look something like this:

void main()
{
try
{
// place your old main code here
//
//
}
catch(...)
{
printf( "Error: Unexpected error has occured.\n" );
}
}

This should tell you if you are having an exception which is most likely being thrown from the printf access of your variable or the strlen access. Hope that helps!

Share this post


Link to post
Share on other sites
Bacardi34    122
Well Try statements are beyond me right now.. here is my entire code, maybe it will help. Im totaly lost, or maybe its just too late at night and i cant "see" the problem.
Any help is MUCH apreciated.


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

int getWord(char buffer[], FILE *getty);
int output(char wbuffer[], char lbuffer[]);

int main()
{
FILE *getty;
getty = fopen("getty.dat", "r");

char lbuff[62] = {0};
char wbuff[25] = {0};
while (!(feof(getty)))
{
getWord(wbuff, getty);
output(wbuff, lbuff);

}
printf("Lbuff Len: %d\n", strlen(lbuff)); // Debug Code, will not print out for me
printf("HEllo"); // Debug code, also not printing. program exits before this point...

fclose(getty);

return 0;
}


int getWord(char buffer[], FILE *getty)
{
char nextLetter;

for (int i = 0; (nextLetter = getc(getty)); i++)
{
if ((nextLetter == ''\t'') || (nextLetter == '' '') || (nextLetter == ''\n''))
{
buffer = ''\0'';
i = 0;
return 1;
}

else
{
buffer[i] = nextLetter;
}
}
return 1;
}


int output(char wbuffer[], char lbuffer[])
{



int lineLength = strlen(lbuffer);
int wordLength = strlen(wbuffer);

if (wbuffer[0] == ''\0'')
return 0;
if (lineLength == 0)
{

strcpy(lbuffer, wbuffer);
strset(wbuffer, NULL);
return 0;
}
if ((lineLength + wordLength) < 62)
{

lbuffer[lineLength] = '' '';
strncat(lbuffer, wbuffer, wordLength);
strset(wbuffer, NULL);
//puts(lbuffer);
//printf(" 1 ");
return 0;
}
if ((lineLength + wordLength) > 61)
{
printf(" 2 ");
printf("%s\n", lbuffer);

strset(lbuffer, NULL);
strcpy(lbuffer, wbuffer);

return 0;
}

//puts(lbuffer);

return 1;
}


BTW How on earth do you do the cool little code listings with white backrounds and all the text color and formatting saved?

Share this post


Link to post
Share on other sites
DarkAvenger    122
Change this line in the ''getWord'' function:

for (int i = 0; (nextLetter = getc(getty)); i++)

to this:

for (int i = 0; ((nextLetter = getc(getty)) != EOF); i++)

and you should be OK.

Share this post


Link to post
Share on other sites