Not quite understanding this bug..
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!?
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!
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!
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 = nextLetter; <br> }<br> }<br> return 1;<br>}<br><br><br>int output(char wbuffer[], char lbuffer[])<br>{<br> <br> <br> <br> int lineLength = strlen(lbuffer);<br> int wordLength = strlen(wbuffer);<br> <br> if (wbuffer[0] == ''\0'')<br> return 0;<br> if (lineLength == 0)<br> {<br> <br> strcpy(lbuffer, wbuffer);<br> strset(wbuffer, NULL);<br> return 0;<br> }<br> if ((lineLength + wordLength) < 62)<br> {<br> <br> lbuffer[lineLength] = '' '';<br> strncat(lbuffer, wbuffer, wordLength);<br> strset(wbuffer, NULL);<br> //puts(lbuffer);<br> //printf(" 1 ");<br> return 0;<br> }<br> if ((lineLength + wordLength) > 61)<br> {<br> printf(" 2 ");<br> printf("%s\n", lbuffer);<br> <br> strset(lbuffer, NULL);<br> strcpy(lbuffer, wbuffer);<br><br> return 0;<br> }<br> <br> //puts(lbuffer);<br> <br> return 1;<br>}<br><br><br>BTW How on earth do you do the cool little code listings with white backrounds and all the text color and formatting saved?<br> </i>
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 = nextLetter; <br> }<br> }<br> return 1;<br>}<br><br><br>int output(char wbuffer[], char lbuffer[])<br>{<br> <br> <br> <br> int lineLength = strlen(lbuffer);<br> int wordLength = strlen(wbuffer);<br> <br> if (wbuffer[0] == ''\0'')<br> return 0;<br> if (lineLength == 0)<br> {<br> <br> strcpy(lbuffer, wbuffer);<br> strset(wbuffer, NULL);<br> return 0;<br> }<br> if ((lineLength + wordLength) < 62)<br> {<br> <br> lbuffer[lineLength] = '' '';<br> strncat(lbuffer, wbuffer, wordLength);<br> strset(wbuffer, NULL);<br> //puts(lbuffer);<br> //printf(" 1 ");<br> return 0;<br> }<br> if ((lineLength + wordLength) > 61)<br> {<br> printf(" 2 ");<br> printf("%s\n", lbuffer);<br> <br> strset(lbuffer, NULL);<br> strcpy(lbuffer, wbuffer);<br><br> return 0;<br> }<br> <br> //puts(lbuffer);<br> <br> return 1;<br>}<br><br><br>BTW How on earth do you do the cool little code listings with white backrounds and all the text color and formatting saved?<br> </i>
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.
for (int i = 0; (nextLetter = getc(getty)); i++)
to this:
for (int i = 0; ((nextLetter = getc(getty)) != EOF); i++)
and you should be OK.
quote:Original post by Kippesoep
Surround your code with [ cpp ] and [ /cpp ] (without spaces).
That''s [ source ] and [ /source ] (without spaces)
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
Yeah. feof only works after a read has failed - you can''t use it to see if the next read will fail if the last one was ok.
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
quote:Original post by Oluseyi
That''s [ source ] and [ /source ] (without spaces)
Doh, yeah. Wrong forum
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement