mrwad99
asked on
First chance exception and string concatenation: two related questions
Hello.
Please look at the following code, intended to read 10 chars from a file and then append it to a char array. The end result is that the multiple calls to fgets should be represented as array y:
char y[30];
memset(y, ' ', 30);
FILE* ifp = fopen("input.txt", "r");
char sz[10];
while (strlen(y) != 30 && (!feof(ifp)) ) {
fgets(sz, 10, ifp);
strcat(y, sz);
}
fputs(y, stdout);
The input file is
line 1
line 2
line 3
line 4
line 5
line 6
and the output I get is
@line 1
line 2
line 3
line 4
line 5
line 6
line 6
So,
1) Why do I have the rubbish at the start of line 1 of the output ?
2) I get a first chance exception when run in debug mode *when the input file has one blank line at the end*. This does not happen in release mode, and does not happen at all without the final blank line. The code crashes at
if ( wchar > 255 ) /* validate high byte */
(XWCTOMB.c, line 116).
Any ideas ? I know that in general first chance exceptions can be ignored, but just wondered why this is happening.
(25 points per question here)
TIA !
Please look at the following code, intended to read 10 chars from a file and then append it to a char array. The end result is that the multiple calls to fgets should be represented as array y:
char y[30];
memset(y, ' ', 30);
FILE* ifp = fopen("input.txt", "r");
char sz[10];
while (strlen(y) != 30 && (!feof(ifp)) ) {
fgets(sz, 10, ifp);
strcat(y, sz);
}
fputs(y, stdout);
The input file is
line 1
line 2
line 3
line 4
line 5
line 6
and the output I get is
@line 1
line 2
line 3
line 4
line 5
line 6
line 6
So,
1) Why do I have the rubbish at the start of line 1 of the output ?
2) I get a first chance exception when run in debug mode *when the input file has one blank line at the end*. This does not happen in release mode, and does not happen at all without the final blank line. The code crashes at
if ( wchar > 255 ) /* validate high byte */
(XWCTOMB.c, line 116).
Any ideas ? I know that in general first chance exceptions can be ignored, but just wondered why this is happening.
(25 points per question here)
TIA !
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
And I take it that is because when using strcat(), the first character of the string to be concatenated is placed over any preceeding '\0' ??
Yes.
And '\0' is placed at the end of the concatenated string.
And '\0' is placed at the end of the concatenated string.
ASKER
Points split in favour of Kdo since he answered the primary questions; ankuratvb you get 20 of the 50 since you sorted my last query.
Thanks a lot both !
Thanks a lot both !
BTW,strcat() searches for the first '\0' and concatenates the second string from here.
Try this:
#include <string.h>
#include <stdio.h>
int main(void)
{
char destination[25]="a\0b";
char *blank = " ", *c = "C++", *turbo = "Turbo";
strcat(destination, turbo);
printf("%s\n", destination);
return 0;
}
The result is "aTurbo".
Try this:
#include <string.h>
#include <stdio.h>
int main(void)
{
char destination[25]="a\0b";
char *blank = " ", *c = "C++", *turbo = "Turbo";
strcat(destination, turbo);
printf("%s\n", destination);
return 0;
}
The result is "aTurbo".
ASKER
Yep; thanks for solidifying that ankuratvb ! Greatly appreciated.
ASKER
2) I am not testing for any type of character; the code I posted in the original question is all the code I have (less main() and a few #includes). The line the code crashes on is in XWCTOMB.c, line 116: I found this out by running the debugger. Anyway that is redundant now because with the new changes that does not happen.
So what I think y will look like is:
line 1\0line 2\0line 3\0line 4\0line5\0line 6\0
Or would it look like
line 1line2line 3line 4line 5line 6\0
After reading all the file in (with an appropriately sized char[])
TIA !