#include "WordOper.h"
#include <stdio.h> //printf
#include <string.h> //string,strlen,strcpy !
#include <iostream>
using namespace std;//string type
#include <fstream>
#include <sstream> //stringstream
//add this 2 lines, strlen still not ok!
#include <vector> // for get months
#include <time.h>
#include <stdlib.h> // string to int
//#include <windows.h>
WordOper::WordOper()
{
}
WordOper::WordOper(const WordOper& orig)
{
}
WordOper::~WordOper()
{
// delete[] buffer_char_p_temp;
}
const int WordOper::CreateFolder(const char* path)
{
// COleDateTime time = COleDateTime::GetCurrentTime();
// string path_name =path;
// if (!CreateDirectory(path_name.c_str(), NULL))
// return 1; // Failed,it has been existed
// else
return 0;// Successful, Create New Direct!
}
const char* WordOper::GetFileName(const char* full_path)
{
int fileNamePos = 0;
string full_path_str;
full_path_str = full_path;
fileNamePos = full_path_str.find_last_of("\\");
fileNamePos ++;
string str_FileName="";
str_FileName.append(&full_path_str.at(fileNamePos));
return str_FileName.c_str();
}
const char* WordOper::GetFileNamePrefix(const char* file_name)
{
int fileNamePos = 0;
/*
string full_path_str="";
full_path_str = file_name;
fileNamePos = full_path_str.find_last_of(".");
if(fileNamePos<=0)
{
return full_path_str.c_str();
}
else
{
char *value1=new char[fileNamePos];
strncpy(value1,full_path_str.c_str(),fileNamePos);
return value1;
}
*/
return NULL;
}
const int WordOper::CopyAFile(const char *dest_file_name,const char* source_file_name)
{
fstream fsCopee(source_file_name,ios::binary|ios::in);
fstream fsCoper(dest_file_name,ios::binary|ios::out);
fsCoper<<fsCopee.rdbuf();
return 0;
}
const char *WordOper::ReplaceString(const char *source,const char *find,
const char *replace)
{
int find_length = strlen(find);//find.size();
int replace_length = strlen(replace);// replace.size();
string::size_type find_location =0;
string replace_result = source;
find_location = replace_result.find(find, 0);
int i=0;
while(find_location != string::npos)
{
find_location = replace_result.find(find, i);
if( find_location != string::npos)
{
replace_result.replace(find_location,find_length,replace);
i = find_location + replace_length;
}
}
static char *str = NULL; /* this only happens once */
delete [] str; /* delete previous cached version */
str = new char[strlen(replace_result.c_str()) + 1]; /* allocate space for the string and it's NUL terminator */
strcpy(str, replace_result.c_str());
return str;
}
August 25, 2013 1:14:39 AM PDT
- 在苹果笔记本操作系统里OS X10.8版本使用GCC来制作一个可执行文件的方法:
- 原理:STEP1 编写一个C++类文件,编译成一个目标文件,然后编译成静态库文件。
- STEP2 编写一个调用这个C++的主程序,然后编译成目标文件,然后联合静态库一起编译成一个可执行文件。
- STEP3 编译C++类成一个目标文件:打开终端控制台,然后进入到要编译的源代码目录里,输入
- “g++ -c -o WordOper.o WordOper.cpp” 注释:将这个cpp文件编译成o目标文件,为生成静态或者动态库服务
- STEP4 生成静态库a文件,在控制台里输入
- "libtool -static -o WordOper.a WordOper.o" //用上一步生成的o文件来生成a文件
- STEP5 编译C++主程序,在控制台里面输入
- “g++ -c -o test.o main.cpp” //编译主要调用的程序为目标文件
- STEP5 将静态库和主程序一起编译,生成一个可执行文件(在mac os x),在控制台里输入
- “g++ test.o -o main ./WordOper.a” //可执行文件是main
- STEP6 to Run,in Terminal
- "./main"
- 就会看到输出,测试成功!
- 注释:使用通用的C++类,在主程序里面以类的方式调用对应的h文件,然后调用其函数就可以了。
- #include "WordOper.h" WordOper m_WordOper;
- 然后调用这个变量里的函数就可以了。
- 完!
/* * File: main.cpp * Author: owner * * Created on 2013年8月21日, 下午12:13 */ //#include "wordoper/WordOper.h" #include "WordOper.h" #include <cstdlib> #include <iostream> // <iostream.h> is error in vc++6! using namespace std; #include <sstream> //stringstream int main(int argc, char* argv[]) { //printf("Hello World!\n"); string text = ""; WordOper m_WordOper; string file_name =""; file_name.append("text900000.txt"); if (m_WordOper.file_if_exist(file_name.c_str())) { } else { m_WordOper.CreateFile(file_name.c_str()); } for (int i=0;i<900;i++) { cout<<"No:"<<i<<endl; text.append("No"); text.append(m_WordOper.Get_string(i)); text.append(" : "); text.append(m_WordOper.Get_string(i)); text.append(" * "); text.append(m_WordOper.Get_string(i+1)); text.append(" = "); text.append(m_WordOper.Get_string(i*(i+1))); text.append("\r\n"); m_WordOper.AppendTxtToAFile(file_name.c_str(),text.c_str()); text = ""; } return 0; }
C++类文件