#how can i get the fdata from a file with the file name?
14 messages · Page 1 of 1 (latest)
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.
Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.
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
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!
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
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>
ok thanks i ll check it out
@versed star Has your question been resolved? If so, type !solved :)
!solved