Thread: access violation.. copying chars

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1

    access violation.. copying chars

    I've written a function to take spaces that i don't want out of a string.
    that part of the function works.. however when i go to copy the new string into the old one it simply falls flat on its face.. any ideas would be very much appreciated!!!

    Code:
    void removeSpaces(char line[])
    {
    	char newline[80] = {0};
    	int op = 0, opfin = 0;
    	int i = 0, j = 0, length = strlen(line);
    
    	/* remove any leading spaces and leave a space after the instruction*/
    	while(!opfin)
    	{
    		if(!isspace(line[i]) && isalpha(line[i]))
    		{
    			newline[j] = line[i];
    			op = 1;
    			j++;
    		}
    		else if(isspace(line[i]) && op)
    		{
    			newline[j] = ' ';
    			j++;
    			op = 0;
    			opfin = 1;
    		}
    		
    		i++;
    	}
    
    	/* remove any spaces in the rest of the line */
    	while(line[i] != '\0')
    	{
    		if(!isspace(line[i]))
    		{
    			newline[j] = line[i];
    			j++;
    		}
    		i++;
    	}
    
    	newline[j] = '\0';
    	//strncpy(line, newline, strlen(newline));
    	length = strlen(newline);
    	i = 0;
    	while(i < length)
    	{
    		line[i] = newline[i];
    		i++;
    	}
    }
    As you can see i've tried strncpy and a while loop.. as well as strcpy and memcpy.

    Thanks in advance

    Rocco

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It might be stupid of me to point out the obvious, but if your trying to remove leading or trailing whitespaces in a string, This thread will help you out. Seach the board next time, you might find your answer in an old thread. Just don't post in topics older than two weeks.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Have you tried a debugger? Are you certain the command will fit in newline?
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation when reading [00000000]?
    By phal in forum Windows Programming
    Replies: 12
    Last Post: 08-19-2008, 06:03 AM
  2. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 10:25 AM
  3. access violation
    By bonkey in forum C++ Programming
    Replies: 15
    Last Post: 11-20-2003, 10:22 AM
  4. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 02:40 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM