#how can i get the fdata from a file with the file name?

14 messages · Page 1 of 1 (latest)

versed star
#

I need the file data to be stored in a buffer , but i dont know how to do it.

cosmic spireBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

pastel lava
#

can you be more specific

#

!howto ask

cosmic spireBOT
# pastel lava !howto ask
How to Ask a Programming Question

Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.

What to Post

State your problem clearly and provide all necessary details:

  • the relevant portion of your code, or all of it
  • the expected output
  • the actual output (or the full error)
    :trophy: Gold Standard: Minimal Reproducible Example
Where to Post

Provide the relevant code in the message, and format it nicely with a code block*. If it's too much for one message, you can upload it:

  • Compiler Explorer for most C/C++ snippets
  • OnlineGDB for interaction, debugging
    :no_entry: Do not post screenshots, let alone photos of your screen!
versed star
#

like

std::streampos getFileSize(const std::string& filename) {
    std::ifstream file(filename, std::ios::binary | std::ios::ate);
    if (!file.is_open()) {
        std::cerr << "Unable to open file: " << filename << std::endl;
        return -1; // Indica erro
    }
    return file.tellg(); // Retorna o tamanho do arquivo
}

std::vector<char> readFileData(const std::string& filename) {
    std::ifstream file(filename, std::ios::binary);
    if (!file.is_open()) {
        std::cerr << "Unable to open file: " << filename << std::endl;
        return {}; // Retorna um vetor vazio em caso de erro
    }

    // Obtém o tamanho do arquivo
    std::streampos fileSize = getFileSize(filename);
    if (fileSize == -1) {
        return {};
    }

    // Lê os dados do arquivo para um vetor de char
    std::vector<char> fileData(static_cast<size_t>(fileSize));
    file.read(fileData.data(), fileSize);

    return fileData;
}
#

i found this and i test it and it gave me random bytes. i know its suppose to but i dont know if the function is correct

#

teh second function

#

the first is right

pastel lava
#

if you only want to read file, i like this way```cpp
std::ifstream t("input.txt");
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());

#

add #include <fstream>

versed star
#

ok thanks i ll check it out

cosmic spireBOT
#

@versed star Has your question been resolved? If so, type !solved :)

versed star
#

!solved