Open files by passing variable as file name

Asked

Viewed 1,285 times

3

I have the following situation, a file "tables.txt" where I have around 2000 lines with filename to which I want to pass row by row taking the name of the files and check which ones exist and which ones not.

The problem occurs when after catching the file name to be checked try to pass it to open it later ifstream obj (arq), the strange thing is that if I type passing the string with the path and not the variable arq that is string the file is read smoothly.

Follows the code used:

#include <iostream>
#include <fstream>
#include <string>
#include <string>
using namespace std;


int main () {
    string line;    
    ifstream myfile ("tabelas.txt"); 
    if (myfile.is_open()){
        while (! myfile.eof() ) 
        {
            getline (myfile,line); 
            string arq = "..\\dados\\"+ line+".csv";

            ifstream obj (arq);
            if (!obj) 
                cerr<<"Ocorreu um erro ao abrir o arquivo"<<endl;
        }
    myfile.close();
    }

    else cout << "Unable to open file"; 

    return 0;
}

Follows image demonstrating error:

http://prntscr.com/by4opb

  • I could not reproduce the http://ideone.com/VCbQyoerror

  • are using which IDE? I am using Dev-C++

  • I have not used any IDE, it does not matter. http://answall.com/q/101691/101

  • I understand the difference between the terms, but I’ve seen an error in dev c which did not happen in Code::Blocks, it was more of curiosity. I tried several ways and it was not possible, I ended up doing in python however, if possible I would like to know how to do in C++ too, you have some other way(eg another function) that I can get the result I seek?

1 answer

3


I rebuilt it to read files the way I normally do.

Follow the code below:

#include <iostream>
#include <fstream>
using namespace std;

int main () {
    ifstream myfile ("x.txt", ifstream::in);
    string line;
    if(myfile.is_open()){
        while(getline(myfile, line)){
            line = "..\\dados\\" + line + ".csv";
            ifstream testfile (line.c_str(), ifstream::in);
            if(testfile.is_open()){
                cout << "O arquivo '"<< line <<"' existe\n";
            }else{
                cout << "O arquivo '" << line << "' nao existe\n";
            }
        }
    }
    myfile.close();


    return 0;
}
  • I’m sorry, I didn’t understand what you meant by the characters being encoded.

  • Jeez, it was working now .-.

  • Oops!!! I saw only now that you edited hahah. Your code worked perfectly and I noticed that the difference between our code was ". c_str()", just adding this worked, besides my code presented a bigger performance. Where it took a few seconds to demonstrate the files that didn’t exist while mine was "instant". I really appreciate your help. And I’ll give you a mix between the codes with the best part of each, because yours has visibly gotten much better than mine.

  • rectifying: both were "snapshots", I was running some heavy processes on the machine at the time I ran your !!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.