0

So i have this program that supposedly reads any file (e.g images, txt) and get its data and creates a new file with that same data. The problem is that i want the data in an array and not in a vector and when i copy that same data to char array, whenever i try to write those bits into a file it doesnt write the file properly.

So the question is how can i get the data from std::ifstream input( "hello.txt", std::ios::binary ); and save it an char array[] so that i can write that data into a new file?

Program:

#include <stdlib.h> #include <string.h> #include <fstream> #include <iterator> #include <vector> #include <iostream> #include <algorithm> int main() { FILE *newfile; std::ifstream input( "hello.txt", std::ios::binary ); std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {}); char arr[buffer.size()]; std::copy(buffer.begin(), buffer.end(), arr); int sdfd; sdfd = open("newhello.txt",O_WRONLY | O_CREAT); write(sdfd,arr,strlen(arr)*sizeof(char)); close(sdfd); return(0); } 
5
  • 2
    Why does the data need to go in an array at all? What is wrong with using a vector (which has the benefit of working)? Have you tried using the array contents to re-create the vector, which (if successful) you know how to write to a new file? Commented Dec 28, 2021 at 15:20
  • because in a bigger project i need to pass the data into a tcp server that only accepts char arrays Commented Dec 28, 2021 at 15:26
  • 4
    buffer.data() gives you an unsigned char* pointer to the array managed by the vector, that you can pass to whatever function you are now passing arr to. Commented Dec 28, 2021 at 15:30
  • 5
    strlen(arr) doesn't return the same value as buffer.size(). It returns the offset of the first 0 (zero) byte, or exhibits undefined behavior if there is none such. strlen only makes sense when working with nul-terminated text strings; not for binary data. Commented Dec 28, 2021 at 15:33
  • 1
    It looks strange that to read a file, you use the C++ API ifstream, while to write you use the low-level, Unix only, C interface open. Commented Dec 28, 2021 at 19:02

1 Answer 1

1

Try this:
(It basically uses a char*, but it's an array here. You probably can't have an array in the stack in this case)

#include <iostream> #include <fstream> int main() { std::ifstream input("hello.txt", std::ios::binary); char* buffer; size_t len; // if u don't want to delete the buffer if (input) { input.seekg(0, input.end); len = input.tellg(); input.seekg(0, input.beg); buffer = new char[len]; input.read(buffer, len); input.close(); std::ofstream fileOut("newhello.txt"); fileOut.write(buffer, len); fileOut.close(); // delete[] buffer; u may delete the buffer or keep it for further use anywhere else } } 

This should probably solve your problem, remember to always have the length (len here) of the buffer if you don't want to delete it.
More here

Sign up to request clarification or add additional context in comments.

1 Comment

Even though this is what the OP asked for, I would not recommend this way. Managing memory manually is error prone. At worst I'd use std::unique_ptr with its array overload

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.