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

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)
-
access.txt (1003bytes)
Number of downloads: 83
This post has been edited by makeeta: 09 May 2011 - 11:39 AM