CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2003
    Posts
    417

    Please spot out the problem

    I am writing a program that will take a string that has a C source file and it would format all the multi-line comments like these:

    Code:
    //Jingle bells, jingle bells, jingles all the way
    //O what fun it is to ride in a one-horse open sledge
    //yeah, jingle bells....
    into

    Code:
    /*Jingle bells, jingle bells, jingles all the way
    O what fun it is to ride in a one-horse open sledge
    yeah, jingle bells....*/
    or the lines,

    Code:
    i=0; //Isn't it nice
    //today? hehehehe....
    into

    Code:
    i=0; /*Isn't it nice
    today? hehehehe...*/

    The code can be downloaded from the attachment. I've finished coding it but I'm still not through debugging it.

    I've taken a linked list to represent a single comment block that needs to be formatted. Each node would represent one line of source code that contains a comment. The list is represented by a struct LOC that looks like this:

    Code:
    /*Represent a line of code that has a // comment in it
    The purpose of this structure is to store those lines of
    source code that:
    
      (1) Contain a // in them
      (2) Appear in a series
      (3) Are a part of one multi-line comment that is to be formatted
    
      For more information, please read the "Comments to be targetted" section
      in the specification file "Read Me.txt" in this project
    */
    struct LOC
    {
    	char *Text; //Text of the line containing the comment
    
    	struct LOC *NextLine; //Pointer to the next comment
    
    	int Len; //length of the line containing the comment
    
    	int Position; //Starting position of the comment in the line of text
    
    	int WasFirstNonSpaceChar; /*A boolean value indicating if the comment 
    							  was the first non-space character in the line 
    							  of text*/
    };
    I'm facing a problem in my FormatBlock function. Particularly in this while loop within the FormatBlock function.


    Code:
    while(head!=NULL)
    	{
    		if(strcat(NewBlock, Replace(head->Text, head->Position, head->Position+1, "  "))==NULL)
    			return NULL;
    		
    		if(head->NextLine==NULL)
    			strcat(NewBlock, "*/");
    		else
    			strcat(NewBlock, sNewLine);
    		head=head->NextLine;
    	}

    What happens is that in this while loop, somehow, while i'm only traversing the loop and replacing, say, node 3 out of 5 nodes in my linked list, the text of the 5th node in the list gets changed somehow. Just before this loop, all the elements are intact. I put printf statements at many steps and discovered that the problem is only when it comes to the 2nd element of the linked list within this loop that it changes the 5th element.

    For a test, I am currently reading the comments from a file that I created called "Code.c" and put in C:.

    You will find the file Code.c also within the attachment. Please download it and put the Code.c file in C: and then walk through the code.


    Thoughts as to what might be causing this problem?
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2003
    Location
    india
    Posts
    12

    Re:

    I think you are creating it a bit more complex the it is you dont need to create a linked list for it.

    follow these steps

    open the file in read write mode.

    move to location of comment that is //
    store the location some where
    now call seek funtion to move to that postion and replace first / with *
    if you find // on next line you can replace it with blank.


    well this approch wont sole the whole problem but it is the basic logic and is simple, well you would need to create a temp file for full logic

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured