segfault变量创建c ++

时间:2014-12-21 05:53:43

标签: c++ segmentation-fault

所以我现在遇到了一些段错误。

问题似乎在第122行

file_stream.read(binaryData, fileSizeInt);

该消息似乎无法创建变量对象。 我在CLion上使用调试器,如果有任何帮助的话。

我对C ++很新,所以请耐心等待。我已经在下面包含了我的源代码,因为我不知道它会是什么,也不会相关。

#include <fstream>
#include <iostream>
#include <math.h>

using namespace std;

//string dataLocation = "/dev/rdisk2";  // Variable for the mounting point of the SD Card.
string dataLocation = "/Volumes/Untitled/data/smith_data_backup.dat";  // Test location
int sectorSize = 512;
int bytesRead = 0;
string outDir = "/Volumes/Untitled/data/readData/";
char data_stop[10] = "Data_Stop";

char* findHeader(char* inputData) {
    int i = 1;

    //look for the end of the header
    while(true) {
        //cout << data[i] << '\n';
        if (inputData[i] == '}') {
            // found the end of the header
            cout << "found the end of the header" << '\n';
            break;
        }
        if (i > 200) { // don't get into an infinite loop here
            cout << "something went wrong here" << '\n';
            break;
        }
        i++;
    }

    // copy the header to a new char array
    char* header = new char[i+1];
    memcpy(header, &inputData[0], (size_t) (i +1));
    return header;
}

int main() {

    ifstream file_stream;
    file_stream.open(dataLocation);

    for( int j = 0; j < 10; j++ ) {


        char data[sectorSize]; // figure out a decent size for this.....
        string date = "";
        string time = "";
        string site = "";
        string intId = "";
        string totalFiles = "";
        string fileNo = "";
        string fileSize = "";

        try {

            // read the data file... this will be fun
            cout << "Reading the data file" << '\n';
            file_stream.read(data, sectorSize);
            bytesRead += sectorSize;

            if (data[0] == '{') {
                char *header = findHeader(data);
                cout << header << '\n';

                //loop over the header to find the date, time, site ID, Instrument ID, and expected file size.
                int commasFound = 0;
                for(int i = 1; header[i] != '}', i++;) {

                    // increment the number of commas that have been found
                    if (header[i] == ',') {
                        commasFound ++;

                        continue;
                    }

                    //check for the end f the header
                    if (header[i] == '}') {
                        break;
                    }

                    //for some reason the first one never gets added
                    if (i==2) {
                        date += header[1];
                    }

                    // append to appropriate strings based on the number of commas that have been passed in the header
                    if (commasFound == 0) {
                        date += header[i];
                    }
                    if (commasFound == 1 && time.length() < 6) {
                        time += header[i];
                    }
                    if (commasFound == 2) {
                        fileNo += header[i];
                    }
                    if (commasFound == 6) {
                        site += header[i];
                    }
                    if (commasFound == 8) {
                        intId += header[i];
                    }
                    if (commasFound == 16) {
                        fileSize += header[i];
                    }
                    if (commasFound == 17) {
                        totalFiles += header[i];
                    }


                    //paranoia of infinite loops
                    if (i > 150) {
                        break;
                    }
                }
                string formattedDate = "20" + date.substr(4,2) + date.substr(2,2) + date.substr(0,2);
                cout << formattedDate << " " << time << " " << " " << site << " " << intId << " " << fileSize << " " << totalFiles<< " " << '\n';

                // Read in the data size
                int fileSizeInt = atoi(fileSize.c_str()) * atoi(fileSize.c_str());
                char binaryData[fileSizeInt];
                file_stream.read(binaryData, fileSizeInt);
                bytesRead += fileSizeInt;

                string dateDir = outDir + formattedDate.substr(0,4) + "/" + date.substr(2,2) + "/" + site + "/" + date.substr(0,2) + "/";
                string fileName = dateDir + formattedDate + "_" + time + "_" + site + "_" + intId + "_Full_Data.dat";
                //cout << fileName << '\n';

                //create the directory with the date.
                string mkdirCommand = "mkdir -p " + dateDir;
                system(mkdirCommand.c_str());

                cout << "size " << sizeof(binaryData) << '\n';

                //write data to file
                try {
                    cout << "Write the data file " << '\n';
                    cout << header << " " << strlen(header) << '\n';
                    ofstream outFile;
                    outFile.open(fileName.c_str());
                    outFile.write(header, strlen(header));
                    outFile.write(binaryData, sizeof(binaryData));
                    outFile.write(data_stop, sizeof(data_stop));
                    outFile.close();
                } catch (ofstream::failure e ) {
                    cout << "Unable to write the file " << fileName << '\n';
                }

                //read till the start of the next sector
                int nextSector = (int) ceil(((double) bytesRead)/512.0);
                int startOfNextSector = nextSector * 512;
                cout << startOfNextSector << '\n';
                int bytesToRead = startOfNextSector - bytesRead;
                char dump[bytesToRead];
                file_stream.read(dump, bytesToRead);
                bytesRead += bytesToRead;


            // The first block may be empty, quick check to see if the first block needs to be skipped
            } else if (j == 0 && data[0] != '{') {
                cout << "Skipping the first block" << '\n';
                continue;

            } else {
                //cout << "no start here....." << bytesRead << '\n';
            }




        }
        catch (ifstream::failure& e) {
            cout << "Error" << "\n";
            break;
        }

    }
    file_stream.close();
    return 0;
}

0 个答案:

没有答案