CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: String problems

  1. #1
    Join Date
    Jan 2009
    Posts
    25

    String problems

    I'm using Borland C++ for DOS 3.1, and lately I have been having some weird problems with strings. For example, I have a function that copies a file binary
    Code:
     int copyfile(char source[], char dest[])
    and when I access the function:
    Code:
     copyfile("CTA2\\SYSTEM\\POINTER.CTA", "CTA2\\SYSTEM\\TEMP.CTA");
    In the step by step debugging I discovered that the source is changed to
    Code:
    "IL\\UNZIP.EXE"
    That is the final part from a previous execution of the function.

    Why is that???
    It is not the only string-related problem, it took me a long while to fix a problem where it was reading from a string and writing in two others at the same time, when I only asked it to write in one string.

    How can this be fixed?
    Last edited by chibicitiberiu; February 17th, 2009 at 04:27 AM.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: String problems

    I think we need some example code that exhibits the problem.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Jan 2009
    Posts
    25

    Re: String problems

    Well, this is the copyfile function:
    Code:
    //COPYFILE FUNCTION
    //=================
    // - Copies a file from the source location to the destination in binary mode
    
    int copyfile(char source[], char dest[])
    {
    	ifstream input(source, ios::in|ios::binary);
    	ofstream output(dest, ios::out|ios::binary);
    
    	if (!input) return -1;
    	if (!output) return -2;
    
    	char* a;
    
    	input.read(a, 1);
    	while(!input.eof()) {
    		output.write(a, 1);
    		input.read (a, 1);
    		}
    
    
    	input.close();
    	output.close();
    
    	return 0;
    
    }
    And this is where the function is accessed (at the end of this function):
    Code:
    int addfile_pointer(char* pat, char* bin)
    {
    	//eliminate common error from path (use of / instead of \)
    	for (int i=0;i<=strlen(pat);i++)
    		if (pat[i]=='/')
    			pat[i]='\\';
    
    	//check if file exists
    	ifstream test(pat);
    	if (!test) return -1;
    	test.close();
    
    	//get the file name from path
    	int lastslash=0;
    	for (i=0;i<=strlen(pat);i++)
    		if (pat[i]=='\\') lastslash=i;
    
    	char* filename;
    	if (lastslash!=0) lastslash++;
    	for (i=lastslash;i<=strlen(pat);i++)
    		filename[i-lastslash]=pat[i];
    
    	//create the new path
    	  //declarations
    	int s1l, s2l;
    	char* newpat="CTA2\\";
    
    	if (strcmp(bin, "")!=0) {
    
    		//add folders to path
    		s1l=5;
    		s2l=strlen(bin);
    
    		for (i=s1l;i<=s1l+s2l;i++)
    			newpat[i]=bin[i-s1l];
    
    		//make necessary folders
    		makebins(newpat);
    
    		//add a slash
    		s1l=strlen(newpat);
    		newpat[s1l]='\\';
    		s1l++;
    
    		//add filename to path;
    		s2l=strlen(filename);
    		for (i=s1l;i<=s1l+s2l;i++)
    			newpat[i]=filename[i-s1l];
    		}
    
    	else {
    		s1l=5;
    		s2l=strlen(filename);
    		for (i=s1l;i<=s1l+s2l;i++)
    			newpat[i]=filename[i-s1l];
    		}
    
    
    	//copy the file
    	int cf;
    	cf=copyfile(pat, newpat);
    	if (cf!=0) return -2;
    
    
    	//add file to pointer file
    	cf=copyfile("CTA2\\SYSTEM\\POINTER.CTA", "CTA2\\SYSTEM\\TEMP.CTA");
    	if (cf!=0) return -3;
    
    	ifstream temp("CTA2\\SYSTEM\\TEMP.CTA");
    	ofstream pointer("CTA2\\SYSTEM\\POINTER.CTA");
    
    	char input[128];
    	while (!temp.eof()) {
    		temp.getline(input, 127, '\n');
    		pointer<<input<<endl;
    		}
    
    
    	pointer<<newpat;
    
    	pointer.close();
    	temp.close();
    
    	//cleanup
    	delfile("CTA2\\SYSTEM\\TEMP.CTA");
    	return 0;
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: String problems

    Code:
    char* a;
    input.read(a, 1);
    Writing to an uninitialized pointer -- undefined behaviour.
    Code:
    char* filename;
    if (lastslash!=0) lastslash++;
    for (i=lastslash;i<=strlen(pat);i++)
    	filename[i-lastslash]=pat[i];
    Writing to an uninitialized pointer "filename" -- again, undefined behaviour.

    Are you sure you know how to handle pointers correctly? Since you obviously have a problem, why not just use std::string instead of char arrays and pointers?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2009
    Posts
    25

    Re: String problems

    Quote Originally Posted by Paul McKenzie View Post
    Code:
    char* a;
    input.read(a, 1);
    Writing to an uninitialized pointer -- undefined behaviour.
    Code:
    char* filename;
    if (lastslash!=0) lastslash++;
    for (i=lastslash;i<=strlen(pat);i++)
    	filename[i-lastslash]=pat[i];
    Writing to an uninitialized pointer "filename" -- again, undefined behaviour.

    Are you sure you know how to handle pointers correctly? Since you obviously have a problem, why not just use std::string instead of char arrays and pointers?

    Regards,

    Paul McKenzie
    Declaring 'string' variables in BC3.1 is not supported. I can only use char arrays.
    And char* behaves (or at least should) like a string, and unlike arrays should have a big number of characters, maybe unlimited.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: String problems

    Quote Originally Posted by chibicitiberiu View Post
    And char* behaves (or at least should) like a string,
    A char* is a pointer to a single character. It is not a string.
    and unlike arrays should have a big number of characters, maybe unlimited.
    Where did you get this information? Do you dispute the problems you're having with your code (i.e. undefined behaviour).

    When you declare a pointer at local scope, it points to a random address in memory. You have to point it to a valid piece of memory. Your pointers are uninitialized -- you cannot write to an uninitialized pointer.
    Code:
    char a;
    input.read(&a, 1);
    This is probably what you should have done. The read function is looking for an address of a character. Just because a function requires a pointer doesn't mean you declare a pointer and send it to the function. All that's required is the address of a valid char.

    Second, char* is not a string. You need to allocate memory and point the pointer to the beginning of this buffer. This simulates string handling, since there is no real "strings" in your version of C++ (which is very old). That's why this code is no good:
    Code:
    char* filename;
    
    if (lastslash!=0) lastslash++;
    for (i=lastslash;i<=strlen(pat);i++)
    	filename[i-lastslash]=pat[i];
    What buffer is "filename" pointing to? It's pointing to a random section of memory. You then attempt to write to this random memory, and who knows what will happen.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jan 2009
    Posts
    25

    Re: String problems

    I found this information in several programming books.
    Anyway, how do I make this thing work?

  8. #8
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: String problems

    Paul told you how to fix it. Reread his post.

    In addition to what paul told you, you also make another mistake and that is controlling your file read loop with eof(). eof() is not for controlling loops, its for distinguishing errors from eof conditions. You probably notice that the file copied grows in size by a small amount.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: String problems

    Quote Originally Posted by chibicitiberiu View Post
    I found this information in several programming books.
    The information you stated could never be in a programming book, as it is totally wrong. You are more than likely misreading or incorrectly using what you read.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: String problems

    Quote Originally Posted by chibicitiberiu View Post
    Anyway, how do I make this thing work?
    You can make it "work" in many ways. The problem is that which way are you going to use?

    The bottom line is that filename must point to a buffer big enough for all of those characters. Where that buffer comes from is the issue -- it could be a global array or allocated memory, or some other place. That should have been up to you and your program design, but you left no provisions for this since you are not aware of how to handle pointers correctly.

    So the ball is in your court -- where does this buffer come from that filename will be pointing to?

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    May 2007
    Posts
    811

    Re: String problems

    I'm using Borland C++ for DOS 3.1,
    Why?

  12. #12
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: String problems

    I see you are using ifstream ofstream and ios classes in your code, but in your post on the other thread you state that Borland C++ 3.1 didn't support STL 'std' functions. Are you sure you were correct?

    If it only supports a small subset of STL classes, then why not expand the string class example I posted and dump the 'C style' strings. Mind you, you'll have to understand memory management a bit better to expand it correctly.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  13. #13
    Join Date
    Jan 2009
    Posts
    25

    Re: String problems

    Quote Originally Posted by Russco View Post
    Paul told you how to fix it. Reread his post.

    In addition to what paul told you, you also make another mistake and that is controlling your file read loop with eof(). eof() is not for controlling loops, its for distinguishing errors from eof conditions. You probably notice that the file copied grows in size by a small amount.
    It did grow with 1 byte, but not any more since it first writes and then reads, so it dumps the extra byte .
    Last edited by chibicitiberiu; February 18th, 2009 at 05:07 AM.

  14. #14
    Join Date
    Jan 2009
    Posts
    25

    Re: String problems

    Quote Originally Posted by Paul McKenzie View Post
    Second, char* is not a string. You need to allocate memory and point the pointer to the beginning of this buffer. This simulates string handling, since there is no real "strings" in your version of C++ (which is very old). That's why this code is no good:
    Code:
    char* filename;
    
    if (lastslash!=0) lastslash++;
    for (i=lastslash;i<=strlen(pat);i++)
    	filename[i-lastslash]=pat[i];
    What buffer is "filename" pointing to? It's pointing to a random section of memory. You then attempt to write to this random memory, and who knows what will happen.

    Regards,

    Paul McKenzie
    Than how do I do that? I don't really know how to allocate memory, i know very few information about pointers.
    How do I know how much memory will this string need?

  15. #15
    Join Date
    Jan 2009
    Posts
    25

    Re: String problems

    Quote Originally Posted by JohnW@Wessex View Post
    I see you are using ifstream ofstream and ios classes in your code, but in your post on the other thread you state that Borland C++ 3.1 didn't support STL 'std' functions. Are you sure you were correct?

    If it only supports a small subset of STL classes, then why not expand the string class example I posted and dump the 'C style' strings. Mind you, you'll have to understand memory management a bit better to expand it correctly.
    I now see that I said the wrong thing . It doesn't need 'using namespace std;' at the beginning of the program or 'std::' to access functions. What it doesn't have is 'vector', 'string' and other functions.

Page 1 of 2 12 LastLast

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