c++ - 使用结构?

标签 c++ struct

我在处理结构中的字符串 (char*) 时遇到问题。我似乎也无法调用我想要的正确数据。

processFile 下,它正确显示了结构成员;在 main 下它没有。

这是我的代码:

#include <iostream>
#include <io.h>
#include <string>
#include "dirent.h"
#include "Stream.h"
#include "Compression.h"
#include "Definitions.h"

using namespace std;
using namespace System::IO;
using namespace System::Funcs;

bool isRawfile(char* ext);
void ProcessDirectory(string directory);
void ProcessFile(char* file);
void ProcessEntity(struct dirent* entity);

typedef struct
{
    char *name;
    int usize;
    int csize;
    BYTE *data;
} rawfile;

string path = "";
string source;
int numrawfiles = 0, numstringtables = 0;
rawfile *rawfiles = new rawfile[0x400];

FILE * zone = fopen( "C:\\Users\\jake\\Desktop\\patch_mp.zone" , "wb" );

int main(int argc, char **args)
{
    if(args[1] != NULL)
    {
        source = string(args[1]) + "\\"; //maybe move under else here..
        if(strchr(args[1], '.') != NULL)
        {
            cout<<"Unable to compile files, please drag a folder to compile."<<endl;
            cin.get();
            return 0;
        }
        else
        {
            int header[] = {1,0,0x3B4,0,0,0,1,0,0x1000,0,0,0,-1};
            for(int i=0; i<13; i++)
                fwrite(Converter::Int32ToBytes(header[i]), 1 , 4 , zone );

            ProcessDirectory(args[1]);

            for(int i=0; i<numrawfiles; i++)
                cout<<"Name: "<<rawfiles[i].name<<" Length: "<< rawfiles[i].usize << " - in main()"<<endl;

            fclose(zone);
        }
    }
    else
    {
        cout<<"No folder selected to compile. Press any Key to quit."<<endl;
        cin.get();
        return 0;
    }
    cin.get();
    return 0;
}

void ProcessDirectory(string directory)
{
    string dirToOpen = path + directory;
    auto dir = opendir(dirToOpen.c_str());
    path = dirToOpen + "\\";
    if(NULL == dir)
    {
        cout << "could not open directory: " << dirToOpen.c_str() << endl;
        return;
    }
    auto entity = readdir(dir);
    while(entity != NULL)
    {
        ProcessEntity(entity);
        entity = readdir(dir);
    }
    path.resize(path.length() - 1 - directory.length());
    closedir(dir);
}

void ProcessEntity(struct dirent* entity)
{
    if(entity->d_type == DT_DIR)
    {
        if(entity->d_name[0] == '.')
            return;

        ProcessDirectory(string(entity->d_name));
        return;
    }

    if(entity->d_type == DT_REG)
    {
        string fullpath = path + entity->d_name;
        ProcessFile(const_cast<char *>(fullpath.c_str()));
        return;
    }
    cout << "Not a file or directory: " << entity->d_name << endl;
}

void ProcessFile(char* file)
{
    char* extension = strrchr(file, '.');

    if(isRawfile(extension))
    {
        rawfile raw;
        raw.name = (char *)&file[source.length()];
        raw.usize = File::getFileSize(file);
        rawfiles[numrawfiles] = raw;
        cout<<"Name: "<<rawfiles[numrawfiles].name<<" Length: "<< raw.usize << " - in ProcessFile()"<<endl;
        fwrite(Converter::Int32ToBytes(0x23),1,4,zone);
        fwrite(Converter::Int32ToBytes(-1),1,4,zone);
        numrawfiles++;
    }
}

bool isRawfile(char* ext)
{
    char *exts[11] = {".gsc",".cfg",".txt",".news",".png",".vision",".rmb",".script",".arena",".atr",".csc"};
    for(int i=0; i<10; i++)
        if(strncmp(ext,exts[i],strlen(exts[i]))==0)
            return true;
    return false;
}

这是一个示例图片:http://puu.sh/2vYt7/7698fd05f1

我做错了什么?

最佳答案

使用 std::string 可以省去很多麻烦:

#include <string>
struct thing
{
   std::string name;
   int age;
};

你也可以避免动态分配的数组:

#include <vector>
std::vector<thing> things(3);

关于c++ - 使用结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15867919/

相关文章:

c++ - 如何在 OpenCV 中对齐 Kinect 的 RGB 和深度图像?

c++ - 模板链接错误

c++ - 为什么 INT_MIN 的绝对值与 INT MAX 不同?

templates - 如何将一片结构传递给模板并在 Golang 中迭代它们?

具有可变长度项的 vector 的 C++ 内存管理

c - 结构中的 Malloc

c++ - 执行 cin 后计算标准输入中的字符数

C++ 设置 "blank"或重置 ifstrean (ios) 的异常掩码

pointers - 跨包将结构传递给函数

c - 在 C 中的结构数组中搜索