memcpy and strcpy confusion in C

Does memcpy and strcpy manipulate memory of char[]s?

Page 1 of 1

3 Replies - 2833 Views - Last Post: 21 May 2009 - 08:18 AM Rate Topic: -----

#1 matrixaffiliate   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-April 09

memcpy and strcpy confusion in C

Post icon  Posted 21 May 2009 - 07:36 AM

I'm writing a program in C to monitor the updating of a text file. The program opens the file that is being uploaded, and a file that keeps track of the last line of text that the program last saw being uploaded. It checks every line of text in the uploading file against the last recorded line of text in the second file. When it finds a match, it moves down one more line on the updating file and then replaces the old last recorded line with the new last recorded line of text. I've been able to get just about everything to work except that whenever I run the program in or out of debug I keep getting the following error.

**Debug Assertion Failed!

Program: ...s\Visual Studio 2008\Projects\monitoring\
Debug\monitoring.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\fgets.c
Line: 57

Expression: (str != NULL)** This happens at the last line of code below: fgets(xxx,BUFN,fpa);

Here is the code. It looks this way because the program is for work and the program belongs to the company, so it's basically been censored by my manager for security reasons.

 char xxx[BUFN]
char yyy[BUFN]
char zzz[BUFN]
FILE *fpa
FILE *fpb

//both files are opened and fgets is used to retrieve xxx[] from *fpa and yyy[] from *fpb, zzz[] is and empty constant

		if(strcmp(xxx,yyy)==0 || strcmp(yyy,zzz)==0){	 //check if the text in xxx[] and yyy[] are the same or if yyy is null
				fgets(xxx,BUFN,fpa);			   //increase xxx[]
				memcpy(yyy,xxx,'n',BUFN);	 //copy the text in xxx[] into yyy[]  (I've tried strcpy here too with no luck)
				}
			fgets(xxx,BUFN,fpa);	 //increase xxx[]  This if statement is in a while loop so it repeats with the new xxx[] value 


I'm beginning to wonder if strcpy and memcpy affect the memory allocated for the different char[]s, because if I take out the memcpy/strcpy and just tell it to printf xxx[] and yyy[] every time it enters the if statement then the program runs. However I've only been programming in C for about 5 weeks now so it's probably and more likely that I'm misunderstanding this entirely. I've also tried changing that last fgets to fread and scanf with the same results.

I apologize for not posting all of the code. I understand that it would be easier to assist me if it was all there. Any suggestions would be greatly appreciated.

Thank you,
Matrixaffiliate

This post has been edited by matrixaffiliate: 21 May 2009 - 07:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: memcpy and strcpy confusion in C

#2 Mowgef   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: memcpy and strcpy confusion in C

Posted 21 May 2009 - 07:49 AM

The first problem I could see is that memcpy only takes 3 input parameters, not 4.
http://www.cplusplus...cstring/memcpy/

so then
memcpy(yyy,xxx,'n',BUFN); 


should be
memcpy(yyy,xxx,BUFN); // I dunno what the 'n' was. (?)

This post has been edited by Mowgef: 21 May 2009 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 matrixaffiliate   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-April 09

Re: memcpy and strcpy confusion in C

Posted 21 May 2009 - 08:17 AM

The 'n' was there from what I thought VS 2008 help told me, obviously I misunderstood. Thank you very much Mowgef! That got me past this bug and on to the next.

Thank you again!
Matrixaffiliate
Was This Post Helpful? 0
  • +
  • -

#4 Mowgef   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: memcpy and strcpy confusion in C

Posted 21 May 2009 - 08:18 AM

Ha ha, no problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1