-1

I have an input file that contains a list of .txt files in a folder. I loop through the input file just fine and put the .txt file filepaths in string vectors. However, when I try to open another ifstream using one of the filepaths in the sections vector (string vector value converted to cstring),

std::ifstream secFile(sections[i].c_str());

The line secFile.fail() returns true meaning it fails. If I instead use the currently commented out line that hardcodes a filepath (manually writing the string) rather than getting it from a vector,

//std::ifstream secFile("test2/main0.txt");

it no longer fails. I even tried outputting sections[0].c_str() and "test2/main0.txt" to a text file and the text for each is exactly the same. I even compared the hexadecimal values for the text file and there were no invisible characters that might cause such an issue.

Any idea what the problem might be?

Here is my code:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
//using namespace std;


int main (int argc, char* argv[]){
    if(argc != 2){
        return(0);
    }
    std::vector<std::string> sections;
    std::vector<std::string> overlaps;
    std::ifstream file(argv[1]);
    std::string str;
    std::string secLine;
    std::string overlapLine;
    std::string strLow;
    std::string wholePage = "";

    //determine if input text file is overlap text or main text
    while (getline(file, str))
    {
        if (str.find("overlap")!=-1){
            overlaps.push_back(str);
        }
        else{
            sections.push_back(str);
        }
    }

    file.clear();

    for(int i = 0; i < sections.size();i++){
        //HERE IS MY QUESTION
        std::ifstream secFile(sections[i].c_str());
        //std::ifstream secFile("test2/main0.txt");

        if(secFile.good()){
            std::cout << "\ngood4\n";
        }
        if(secFile.bad()){
            std::cout << "bad4\n";
        }
        if(secFile.fail()){
            std::cout << "fail4\n";
        }
        if(secFile.eof()){
            std::cout << "eof4\n";
        }

        int secLength = 0;
        //determine number of files in test2/
        while (getline(secFile,secLine)){
            secLength++;
        }

        secfile.clear();
        secfile.seekg(0);

        int j = 0;
        while (getline(secFile,secLine)){
            if (i == 0 && j==0){
                wholePage += std::string(secLine) + "\n";
            }
            else if(j==0){
                //do nothing
            }
            else if(i == (sections.size()-1) && j == secLength){
                wholePage += std::string(secLine) + "\n";
            }
            else if(j == secLength){
                //do nothing
            }
            else{
                wholePage += std::string(secLine) + "\n";
            }
            j++;
        }
        int k = 0;
        if(i < sections.size()-1){ 
            std::ifstream overFile(overlaps[i].c_str());
            int overLength = 0;
            while (getline(overFile,overlapLine)){
                overLength++;
            }
            while (getline(overFile,overlapLine)){
                std::cout << "Hi5";
                if(k == 0){
                    //do nothing
                }
                else if(k == overLength){
                    //do nothing
                }
                else{
                    if (wholePage.find(overlapLine)){
                        //do nothing
                    }
                    else{
                        wholePage += std::string(secLine) + "\n";
                    }
                }
            }
            k++;
        }
    }

    std::ofstream out("output.txt");
    out << wholePage;
    out.close();
    std::cout << "\n";
    return 0;


}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
bloo
  • 23
  • 2
  • 1
    Why did you dump the entire program here? Why should we care about things like `determine if input text file is overlap text or main text`? Please put a minimal piece of code which reproduces the problem. – goodvibration Dec 13 '17 at 11:09

2 Answers2

0

You haven't provided enough information to be sure, but the most likely problem is whitespace. getline doesn't strip the trailing whitespace from the lines it produces, so you might be trying to open a file named "test2/main0.txt " (trailing space), which is distinct from "test2/main0.txt". You'll want to trim trailing whitespace in most cases, likely before storing the string to your vector. Since some whitespace can legally be part of a filename, the real solution would be to make sure the garbage whitespace isn't there, but trailing whitespace is filenames is rare enough that you could just hope the file names don't use it.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thanks for the quick response! Yes that worked! Though I find it weird that when I output one of the vector values to a text file there doesn't seem to be an indication of a newline character, even when I convert the text file to hexadecimal. Oh well, problem solved so I can't complain. Thanks again! – bloo Dec 13 '17 at 11:33
  • @BoPersson: Wow, completely forgot that. Guessing the problem was some other whitespace character that trimming got rid of. – ShadowRanger Dec 13 '17 at 14:19
0

Here you are passing a filename:

std::ifstream secFile("test2/main0.txt");

Here you are passing a line of text from a file:

std::ifstream secFile(sections[i].c_str());

ifstream expects a filename, not a line of text from a file. It is failing because the text you are inputting doesn't represent a file you are trying to open.

ericcurtin
  • 1,499
  • 17
  • 20