Solved

First chance exception and string concatenation: two related questions

Posted on 2004-04-10
8
769 Views
Last Modified: 2010-04-15
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 !
0
Comment
Question by:mrwad99
  • 4
  • 3
8 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 30 total points
ID: 10797139
Hi mrwad99,

1a)  You initialize y[] with spaces.  You should initilize the first charter to 0 ('\0').
1b)  Define y[] as y[40] and test for strlen(y) <= 30.  You're reading 6 characters (plus end-of-string, plus possibly carriage-return) into a 10 character array.  You might not fill the array exactly full.  Plus, even if you do put exactly 30 characters into the array, you're writing 31-characters to terminate the string and overflowing the array.

2)  The program is using ASCII characters.  Why are you testing for wide characters?


Good Luck!
Kent
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10797202
1) Right thanks Kent, it was the issue with initialising y to hold ' ' not '\0' that was causing the problem.
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 !
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 20 total points
ID: 10797219
The output would be:

line 1line2line 3line 4line 5line 6\0
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 19

Author Comment

by:mrwad99
ID: 10797226
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' ??
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10797251
Yes.
And '\0' is placed at the end of the concatenated string.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10797265
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 !
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10797269
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".
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10797284
Yep; thanks for solidifying that ankuratvb !  Greatly appreciated.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem in finding output of a program 11 106
convert char array to number in c 5 85
Problem to scan all sheets 3 104
Line meaning 9 86
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

777 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