Search code examples
c++filenamesifstream

filename: "error reading characters of string" and "unable to read memory"


These errors are given when reading a filename from the parameter.

"unable to read memory" "error reading characters of string"

If the string is defined as a user entered text or as a hardkey then it works fine. But, if a portion of the filename is read from file, concatenated then passed through a parameter, these errors are given.

I tried both with and without .c_str() After removing some stray spaces in the text file, this now works in windows but not in linux?

        void batch_helper()
        {

            //batch
            vector <string> field_reports;

            ifstream field_report_batch;
            field_report_batch.open("OCR_batch.txt");
            string line;

            while (true) { //fill stack with batches
                getline(field_report_batch, line);
                if (line.empty() || !field_report_batch || field_report_batch.fail())
                    break;

                field_reports.push_back(line);
            }
            field_report_batch.close();

            for (unsigned int i = 0; i < field_reports.size(); i++) {

                parse_file("timesheets_we" + field_reports[i] + "_batch_export.csv"); //this does not work

                //parse_file("timesheets_we20121221_batch_export.csv"); //this works

            }

        }



        void parse_file(const string timecard_WE)
        {

            //count records
            int timecard_count = 0;
            string line;

            //test input file
            ifstream timecard_test;
            timecard_test.open(timecard_WE.c_str());
            getline(timecard_test, line); //burn header
            while (true){
                getline(timecard_test, line); //read in row_n
                if (!timecard_test || timecard_test.fail()) 
                    break;
                timecard_count++;
            }
            timecard_test.close();
            line.clear();
    }

A similar problem posted on stackoverflow is here, the issue was hidden white space or stray end of line char. I have checked for white space but so far have not found them.

I will try this:

           while (true) {
                if (line.back() == '\r' || line.back() == '\n') {
                    test_log << "pop: " << line.back() << "\r\n";
                    line.pop_back();
                }
            }

the resulting out file is

20120615

pop:

20120622

pop:

20120629

pop:

20120706

pop:

20120713

pop:

20120720

pop:

20120727

pop:

20120803

pop:

20120810

pop:

20120817

pop:

20120824

pop:

20120831

pop:

20120907

pop:

20120914

pop:

20120921

pop:

20120928

pop:

20121005

pop:

20121012

pop:

20121019

pop:

20121026

pop:

20121102

pop:

20121109

pop:

20121116

pop:

20121123

pop:

20121130

pop:

20121207

pop:

20121214

pop:

20121221

pop:

timesheets_we20120615_batch_export.csv BREAK timesheets_we20120622_batch_export.csv BREAK timesheets_we20120629_batch_export.csv BREAK timesheets_we20120706_batch_export.csv BREAK timesheets_we20120713_batch_export.csv BREAK timesheets_we20120720_batch_export.csv BREAK timesheets_we20120727_batch_export.csv BREAK timesheets_we20120803_batch_export.csv BREAK timesheets_we20120810_batch_export.csv BREAK timesheets_we20120817_batch_export.csv BREAK timesheets_we20120824_batch_export.csv BREAK timesheets_we20120831_batch_export.csv BREAK timesheets_we20120907_batch_export.csv BREAK timesheets_we20120914_batch_export.csv BREAK timesheets_we20120921_batch_export.csv BREAK timesheets_we20120928_batch_export.csv BREAK timesheets_we20121005_batch_export.csv BREAK timesheets_we20121012_batch_export.csv BREAK

The BREAKs in the logfile are endof file breaks.


Solution

  • the solution is easy and twp part:

    PART I: the text file where I read the partial file names from and then concatenate into a full name, there was a blank space after some of the text. I opened the file, "select all" and then manually removed the trailing blank spaces.

    PART II: this fixed it while (true)

    { if (line.back() == '\r' || line.back() == '\n') line.pop_back();} 
    

    combined solution:

    while (true) { if (line.back() == '\r' || line.back() == '\n' || line.back() == ' ') line.pop_back();}