zoukankan      html  css  js  c++  java
  • 遍历文件,寻找 struct 结构体

    代码暂时不完善,只实现了基本逻辑,字符串判断逻辑有缺陷

    #include "iostream"
    #include "fstream"
    #include "io.h"
    #include "string"
    
    #include "direct.h"
    #include "stdio.h"
    
    #define NORMAL 0
    #define ERROR -1
    
    using namespace std;
    
    class Struct_Export
    {
    public:
    	Struct_Export(const char* pathout)
    	{
    		setOutfile(pathout);
    	}
    
    	~Struct_Export()
    	{
    		;
    	}
    
    	int Struct_Export_Action()
    	{
    		
    	}
    
    	void setOutfile(const char * path)
    	{
    		Outfile = string(path);
    	}
    
    
    	string getOutfile()
    	{
    		return Outfile;
    	}
    
    
    	int FindStructFromFile(const char* path)
    	{
    		ifstream infile;
    		ofstream outfile;
    		string data;
    		size_t index;
    
    		infile.open(path);
    		outfile.open(getOutfile().c_str(), ios::app);
    
    		while (!infile.eof())
    		{
    			getline(infile, data);
    
    			if ((index = data.find("struct")) != string::npos)   //找到struct起始位置
    			{
    				string substr = data.substr(index + 6, data.length());
    				if (substr.find("{") != string::npos )
    				{
    
    					outfile << data << endl;
    
    					while (!infile.eof())                       //打印当前struct的内容至output文件
    					{
    						getline(infile, data);
    						outfile << data << endl;
    
    						if (data.find("}") != string::npos)     //打印至 } 结尾
    						{
    							break;
    						}
    					}
    
    					outfile << endl;
    				}
    			}
    		}
    
    		infile.close();
    
    		return NORMAL;
    	}
    
    	int FindFileFromDir(const char* strPath)
    	{
    		int iRet = 0;
    		intptr_t handle;
    
    		_finddata_t findData;
    
    		handle = _findfirst(strPath, &findData);
    		if (handle == -1)
    		{
    			cout << "Failed to find first file!
    " << endl;
    			return NULL;
    		}
    
    		do
    		{
    			if (findData.attrib & _A_SUBDIR && !(strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0))
    			{
    				string subdir(strPath);
    				subdir.insert(subdir.find("*"), string(findData.name) + "\");
    				FindFileFromDir(subdir.c_str());
    			}
    			else if((strcmp(findData.name, ".") != 0 && strcmp(findData.name, "..") != 0))
    			{
    				string file_pathname(strPath);
    				file_pathname = file_pathname.substr(0, file_pathname.length() - 3) + string(findData.name);
    
    
    
    				iRet = FindStructFromFile(file_pathname.c_str());
    				if (iRet != NORMAL)
    				{
    					cout << "Call FindStrctFromFile failed!
    " << endl;
    					return ERROR;
    				}
    			}
    
    		} while (_findnext(handle, &findData) == 0);
    	
    		return NORMAL;
    	}
    
    private:
    
    	string Outfile;
    
    };
    
    int main(int argc, char* argv[])
    {
    	if (argc < 3)
    	{
    		cout << "Struct_export must have input!" << endl;
    		cout << "Struct_export -dir pathname" << endl;
    		exit(1);
    	}
    
    	int iRet = 0;
    	string dir = string(argv[2]);
    	char Output[_MAX_PATH];
    
    	_getcwd(Output, _MAX_PATH);
    
    	if (dir[dir.length()] != '\')
    	{
    		dir = dir + '\';
    	}
    
    	dir = dir + "*.*";
    
    	Struct_Export Export_Acion( (string(Output)+string("\stoutput.txt")).c_str() );
    
    	if (!strcmp(argv[1], "-dir"))
    	{
    		iRet = Export_Acion.FindFileFromDir(dir.c_str());
    		if (iRet != NORMAL)
    		{
    			cout << "Call Export_Acion.FindFileFromDir failed!" << endl;
    		}
    	}
    
    	return 0;
    }
    

      

  • 相关阅读:
    HDU 1677
    HDU 1672 Cuckoo Hashing
    HDU 2586 + HDU 4912 最近公共祖先
    最大流 Dinic + Sap 模板
    网络流算法小结(转)
    Malformed network data报错解决方法
    js window.open 参数设置
    java图片高质量缩放类
    struts2 I18n问题 国际化
    java.lang.Exception: Socket bind failed 服务器端口冲突-->修改端口
  • 原文地址:https://www.cnblogs.com/xuyong437/p/12156269.html
Copyright © 2011-2022 走看看