strcpy() and strcat() task problems

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 765 Views - Last Post: 10 May 2011 - 03:48 AM Rate Topic: -----

#1 makeeta  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 9
  • Joined: 09-May 11

strcpy() and strcat() task problems

Posted 09 May 2011 - 11:33 AM

Hello
First of all, I want to say that I know that there are much more easy ways to do what I want my program to do, but I still want to do it this way, and ask for help concerning the strcpy and strcat functions. Is there any way I can still use those functions to work well.
I have wrote a program in C,C++ that read a text file, parse it, and print it to console.
Every line in the text file is being copied to an array of 8 strings. (cstr is a pointer to a pointer) eventually the array of stings is being printed to the console.
Everything it going well, until I try to add one char to the beginning of every string. The problem is with the functions strcat and strcpy (I have painted them in color). It's hard to explain what they do. They just don't fill right the content of the strings. If you run\debug the program step by step, you can see what they do with the content of the strings… horrible :smartass: .
I am adding a text file, for who wants to run and see the result. Just change the text destination in the code.
Thank you







#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#pragma warning(disable: 4996) 
using namespace std;

int a=10, b=16, c=14, d=17, e=42, f=17, g=9, h=12, j=0; 

void PrintTitle()
{
  cout<<right<<setw(80)<<"ALParser (access log parser) \n"
      <<setfill('=')<<setw(135)<<"="<<'\n'                   
      <<" Monitor_2  (shuting down, automaticaly, within two hours) \n"
      <<setfill('-')<<setw(135)<<"-"<<setfill(' ')<<'\n' 

      <<left
	    <<setw(a)<<" [0]"  <<setw(B)/><<" [1]"  <<setw(c)<<" [2]"  <<setw(d)<<" [3]"  <<setw(e)<<" [4]"
        <<setw(f)<<"[5]"  <<setw(g)<<"[6]"  <<setw(h)<<"[7]"<<"\n"

      <<left
	    <<setw(a+b+c+d+e)<<""  <<setw(f)<<"client" <<'\n'

      <<left
	    <<setw(a)<<" Client"  <<setw(B)/><<" IP"  <<setw(c)<<" Date"  <<setw(d)<<" Time"  <<setw(e)<<" Get"
        <<setw(f)<<" browser"  <<setw(g)<<"query"  <<setw(h)<<"data"<<'\n'

      <<left
	    <<setw(a)<<" Name"  <<setw(b+c+d+e)<<""  <<setw(17)<<" protocl"  <<setw(g)<<"respond"  <<setw(h)<<"sent"<<'\n'

      <<setfill('-')<<setw(135)<<"-"<<setfill(' ')<<'\n'; 

}



void PrintLine(fstream& in, fstream& out, char** cstr)
{
  cout<<left
      <<setw(a)<<cstr[0]  <<setw(B)/><<cstr[1]  <<setw(c)<<cstr[2]  <<setw(d)<<cstr[3]
      <<setw(e)<<cstr[4]  <<setw(f)<<cstr[5]  <<setw(g)<<cstr[6]  <<setw(h)<<cstr[7];
  cout<<'\n';
 
 } 



void Test_cstr3(char* str)
{
  if(strlen(str)>40 )
  { 
    str[40]='\0';
  }
}




void main()
{
  char ch='\n';
  for(int i=239; i>0; i--)
  {
    char* cstr[8];
    cstr[0]=new char[100];
    cstr[1]=new char[100];
    cstr[2]=new char[100];
    cstr[3]=new char[100];
    cstr[4]=new char[100];
    cstr[5]=new char[100];
    cstr[6]=new char[100];
    cstr[7]=new char[100];

    fstream in("C:\\Main Program Files\\access.txt",ios::in);
    fstream out("C:\\access.html",ios::out); 


    system("cls");   
    PrintTitle();

    while(!in.eof())
    {
      cstr[0]="unknown";

      in.getline(cstr[1],40,' ');         //name 

      in.ignore(100,'[');                 
	  in.getline(cstr[2],90,':');         //ip

//    in.ignore(100,'[');
      in.getline(cstr[3],90,']');        //time


      in.ignore(200, '/');
      in.getline(cstr[4],99,' ');       //get
      Test_cstr3(cstr[4]);

//      in.ignore(100,' ');
      in.getline(cstr[5],30,'"');        //client browser protocol

      in.ignore(100,' ');
      in.getline(cstr[6],30,' ');        //

      in.getline(cstr[7],30,'\n');       //

	  if(in.eof())
		break;

[color="#DDA0DD"]
      char* var=new char[100];
      strcpy(var, "=");
      for(int n=0; n<8; n++)
      {
        cstr[n]=strcat(var, cstr[n]);
        strcpy(var, "=");
      }
[/color]
      PrintLine(in,out,cstr);
    }

    system("PING -n 31 127.0.0.1>nul");

  }


}







Attached File(s)

  • Attached File  access.txt (1003bytes)
    Number of downloads: 62

This post has been edited by makeeta: 09 May 2011 - 11:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: strcpy() and strcat() task problems

#2 RevTorA  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 76
  • View blog
  • Posts: 246
  • Joined: 22-April 11

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 11:55 AM

View Postmakeeta, on 09 May 2011 - 11:33 AM, said:

Hello
First of all, I want to say that I know that there are much more easy ways to do what I want my program to do, but I still want to do it this way, and ask for help concerning the strcpy and strcat functions. Is there any way I can still use those functions to work well.
I have wrote a program in C,C++ that read a text file, parse it, and print it to console.
Every line in the text file is being copied to an array of 8 strings. (cstr is a pointer to a pointer) eventually the array of stings is being printed to the console.
Everything it going well, until I try to add one char to the beginning of every string. The problem is with the functions strcat and strcpy (I have painted them in color). It's hard to explain what they do. They just don't fill right the content of the strings. If you run\debug the program step by step, you can see what they do with the content of the strings… horrible :smartass: .
I am adding a text file, for who wants to run and see the result. Just change the text destination in the code.
Thank you







#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#pragma warning(disable: 4996) 
using namespace std;

int a=10, b=16, c=14, d=17, e=42, f=17, g=9, h=12, j=0; 

void PrintTitle()
{
  cout<<right<<setw(80)<<"ALParser (access log parser) \n"
      <<setfill('=')<<setw(135)<<"="<<'\n'                   
      <<" Monitor_2  (shuting down, automaticaly, within two hours) \n"
      <<setfill('-')<<setw(135)<<"-"<<setfill(' ')<<'\n' 

      <<left
	    <<setw(a)<<" [0]"  <<setw(B)/><<" [1]"  <<setw(c)<<" [2]"  <<setw(d)<<" [3]"  <<setw(e)<<" [4]"
        <<setw(f)<<"[5]"  <<setw(g)<<"[6]"  <<setw(h)<<"[7]"<<"\n"

      <<left
	    <<setw(a+b+c+d+e)<<""  <<setw(f)<<"client" <<'\n'

      <<left
	    <<setw(a)<<" Client"  <<setw(B)/><<" IP"  <<setw(c)<<" Date"  <<setw(d)<<" Time"  <<setw(e)<<" Get"
        <<setw(f)<<" browser"  <<setw(g)<<"query"  <<setw(h)<<"data"<<'\n'

      <<left
	    <<setw(a)<<" Name"  <<setw(b+c+d+e)<<""  <<setw(17)<<" protocl"  <<setw(g)<<"respond"  <<setw(h)<<"sent"<<'\n'

      <<setfill('-')<<setw(135)<<"-"<<setfill(' ')<<'\n'; 

}



void PrintLine(fstream& in, fstream& out, char** cstr)
{
  cout<<left
      <<setw(a)<<cstr[0]  <<setw(B)/><<cstr[1]  <<setw(c)<<cstr[2]  <<setw(d)<<cstr[3]
      <<setw(e)<<cstr[4]  <<setw(f)<<cstr[5]  <<setw(g)<<cstr[6]  <<setw(h)<<cstr[7];
  cout<<'\n';
 
 } 



void Test_cstr3(char* str)
{
  if(strlen(str)>40 )
  { 
    str[40]='\0';
  }
}




void main()
{
  char ch='\n';
  for(int i=239; i>0; i--)
  {
    char* cstr[8];
    cstr[0]=new char[100];
    cstr[1]=new char[100];
    cstr[2]=new char[100];
    cstr[3]=new char[100];
    cstr[4]=new char[100];
    cstr[5]=new char[100];
    cstr[6]=new char[100];
    cstr[7]=new char[100];

    fstream in("C:\\Main Program Files\\access.txt",ios::in);
    fstream out("C:\\access.html",ios::out); 


    system("cls");   
    PrintTitle();

    while(!in.eof())
    {
      cstr[0]="unknown";

      in.getline(cstr[1],40,' ');         //name 

      in.ignore(100,'[');                 
	  in.getline(cstr[2],90,':');         //ip

//    in.ignore(100,'[');
      in.getline(cstr[3],90,']');        //time


      in.ignore(200, '/');
      in.getline(cstr[4],99,' ');       //get
      Test_cstr3(cstr[4]);

//      in.ignore(100,' ');
      in.getline(cstr[5],30,'"');        //client browser protocol

      in.ignore(100,' ');
      in.getline(cstr[6],30,' ');        //

      in.getline(cstr[7],30,'\n');       //

	  if(in.eof())
		break;

[color="#DDA0DD"]
      char* var=new char[100];
      strcpy(var, "=");
      for(int n=0; n<8; n++)
      {
        cstr[n]=strcat(var, cstr[n]);
        strcpy(var, "=");
      }
[/color]
      PrintLine(in,out,cstr);
    }

    system("PING -n 31 127.0.0.1>nul");

  }


}









I don't understand why you insist on using these functions when you admit there are easier methods of manipulating strings in C++, but anyway. I think you'll find you're doing a lot of buffer overflows in this code, especially with the access.txt file you supplied. Also, I think that some of the code doesn't do what you think it does. Check out the following documentation and see if you can fix your code to do what you want:

http://www.cplusplus...cstring/strcat/
http://www.cplusplus...cstring/strcpy/

Pay particular attention to the return values of those functions.
Was This Post Helpful? 0
  • +
  • -

#3 makeeta  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 9
  • Joined: 09-May 11

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 12:18 PM

View PostRevTorA, on 09 May 2011 - 11:55 AM, said:

I don't understand why you insist on using these functions when you admit there are easier methods of manipulating strings in C++, but anyway. I think you'll find you're doing a lot of buffer overflows in this code, especially with the access.txt file you supplied. Also, I think that some of the code doesn't do what you think it does. Check out the following documentation and see if you can fix your code to do what you want:

http://www.cplusplus...cstring/strcat/
http://www.cplusplus...cstring/strcpy/

Pay particular attention to the return values of those functions.


the code does what i think it does, and works just fine(without the line included the strcpy and strcat that i have added later) because i have ren it. And the links you have gave me don't help much. I have seen them before.
Was This Post Helpful? 0
  • +
  • -

#4 RevTorA  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 76
  • View blog
  • Posts: 246
  • Joined: 22-April 11

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 12:35 PM

I don't think you're reading them well enough. For instance:

char * strcat ( char * destination, const char * source );
Returns:
destination is returned.

So the line cstr[n]=strcat(var, cstr[n]); is setting cstr[n] to var, so then when you do strcpy(var, "=");, you're copying "=" into cstr[n], which I'm guessing is not what you want. Or is it?
Was This Post Helpful? 0
  • +
  • -

#5 makeeta  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 9
  • Joined: 09-May 11

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 12:53 PM

View PostRevTorA, on 09 May 2011 - 12:35 PM, said:

I don't think you're reading them well enough. For instance:

char * strcat ( char * destination, const char * source );
Returns:
destination is returned.

So the line cstr[n]=strcat(var, cstr[n]); is setting cstr[n] to var, so then when you do strcpy(var, "=");, you're copying "=" into cstr[n], which I'm guessing is not what you want. Or is it?


no. i just try to add the char "=" to the beginning of every string (cstr[n]), without deleting their content. Do you have an idea how to do it with those two functions?
Was This Post Helpful? 0
  • +
  • -

#6 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 01:19 PM

View Postmakeeta, on 09 May 2011 - 01:53 PM, said:

View PostRevTorA, on 09 May 2011 - 12:35 PM, said:

I don't think you're reading them well enough. For instance:

char * strcat ( char * destination, const char * source );
Returns:
destination is returned.

So the line cstr[n]=strcat(var, cstr[n]); is setting cstr[n] to var, so then when you do strcpy(var, "=");, you're copying "=" into cstr[n], which I'm guessing is not what you want. Or is it?


no. i just try to add the char "=" to the beginning of every string (cstr[n]), without deleting their content. Do you have an idea how to do it with those two functions?



You really don't understand what you're doing asking a question like that.

Add '=' to the beginning of a string.

char str[80];
strcat('=',str);



That puts '=' + ' ' + str. Of course, you're not saving anything, because that's not how it works. Strcat doesn't actually return anything. It's an 'incidental' return, meaning that the return is basically null, or ignorable.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 01:23 PM

In the following snippet you have a couple of problems.

     cstr[0]="unknown";
//.........
      char* var=new char[100];
      strcpy(var, "=");
      for(int n=0; n<8; n++)
      {
        cstr[n]=strcat(var, cstr[n]);
        strcpy(var, "=");
      }


First you can not use the = sign with C-strings, you must use strcpy().

Second what is the purpose of your variable var? Why not just use static memory allocation instead of new. char var[100];. It seems that you want to add the = sign to the front of your cstr[n] variable, so this should work:
      char var[100];
      for(int n=0; n<8; n++)
      {
	strcpy(var,"=");  // Start with just '='
        strcat(var, cstr[n]); // append cstr to var.
        strcpy(cstr[n],var);  // copy var back to cstr.
      }


Also since you are using new on variables you should be deleting[] these variables. You have a very big memory leak.
  for(int i=239; i>0; i--)
  {
    char* cstr[8];
    cstr[0]=new char[100];
    cstr[1]=new char[100];
    cstr[2]=new char[100];
    cstr[3]=new char[100];
    cstr[4]=new char[100];
    cstr[5]=new char[100];
    cstr[6]=new char[100];
    cstr[7]=new char[100];



Every time through this loop you allocate new memory to this variable but you never delete the used memory. I would suggest that you use static memory allocation for this variable. char cstr[8][100];. This will eliminate the memory leak.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 makeeta  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 9
  • Joined: 09-May 11

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 01:31 PM

Quote

You really don't understand what you're doing asking a question like that.

Add '=' to the beginning of a string.

char str[80];
strcat('=',str);



That puts '=' + ' ' + str. Of course, you're not saving anything, because that's not how it works. Strcat doesn't actually return anything. It's an 'incidental' return, meaning that the return is basically null, or ignorable.



you have problems in your family or something like that? Calm down! Who do you think you are?! You are rude. You don't have to write in this forum if you suffer so much…

This post has been edited by makeeta: 09 May 2011 - 01:32 PM

Was This Post Helpful? -2
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 01:39 PM

Quote

Strcat doesn't actually return anything. It's an 'incidental' return, meaning that the return is basically null, or ignorable.


Actually strcat() returns the destination C-string.

See this link for strcat().

Quote

Return Value
destination is returned.


It is not basically null.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 makeeta  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 9
  • Joined: 09-May 11

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 01:48 PM

the purpose of the variable var is to add the = sign to the front of the cstr[n] variable.
thanks for what you have wrote, but the folowing will not work:
      char var[100];
      for(int n=0; n<8; n++)
      {
	strcpy(var,"=");  // Start with just '='
        strcat(var, cstr[n]); // append cstr to var.
        strcpy(cstr[n],var);  // copy var back to cstr.


the problem is with the line
strcpy(cstr[n],var);  


there is a problem writing cstr[n] at the destination_string place . i don't know why..

This post has been edited by makeeta: 09 May 2011 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#11 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 01:50 PM

cstr[n] has type 'char' not 'char*'. to fix this you need to make it a pointer, either (cstr+n) or &cstr[n] will work, i prefer the former of the two.

edit: nvm, char* cstr[8]; is the declaration for cstr. im wrong...

This post has been edited by ishkabible: 09 May 2011 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#12 RevTorA  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 76
  • View blog
  • Posts: 246
  • Joined: 22-April 11

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 01:57 PM

View Postishkabible, on 09 May 2011 - 01:50 PM, said:

cstr[n] has type 'char' not 'char*'. to fix this you need to make it a pointer, either (cstr+n) or &cstr[n] will work, i prefer the former of the two.


No, cstr[n] is a char*:

char* cstr[8];



Makeeta, I see what you're trying to do with var. Just remember that strcat and strcopy are modifying the first pointer. Don't assign it to anything. For instance, you might be able to do:

strcpy(var, cstr[n]);
strcpy(cstr[n], "=");
strcat(cstr[n], var);



Maybe? I'm not entirely sure. It's worth a shot though.
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 02:48 PM

I just think you're trying to be too clever here.

char* var=new char[100];
strcpy(var, "=");
for(int n=0; n<8; n++)
{
  cstr[n]=strcat(var, cstr[n]);
  strcpy(var, "=");
}


Just try this

// Create a temporary buffer
char tempVar[100] = { 0 };

// For each string in your list, use the temp buffer
// to build the new string, then copy the temp buffer
// over the existing one.
for (int n = 0; n < 8; ++n)
{
    strcpy(tempVar, "=");
    strcat(tempVar, cstr[n]);
    strcpy(cstr[n]], tempVar);
}

Was This Post Helpful? 1
  • +
  • -

#14 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 08:03 PM

View Postjimblumberg, on 09 May 2011 - 02:39 PM, said:

Quote

Strcat doesn't actually return anything. It's an 'incidental' return, meaning that the return is basically null, or ignorable.


Actually strcat() returns the destination C-string.

See this link for strcat().

Quote

Return Value
destination is returned.


It is not basically null.

Jim


It is ignorable unless you actually need a pointer, and I can think of only one time you'd use strcat in a way to need the pointer.

@makeeta no, I have a problem with people who don't look at the links to information given them and say it doesn't help when your problem is a misunderstanding of the fundamental use of this function.

This post has been edited by ccubed: 09 May 2011 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#15 makeeta  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 9
  • Joined: 09-May 11

Re: strcpy() and strcat() task problems

Posted 09 May 2011 - 10:43 PM

ccubed (Maybe you should change your name to cs_tupid) You know nothing about C programming. You are writing nonsense, and mistaking others. You need a psychologist.
Was This Post Helpful? -4
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2