TXT file to char array and char array to TXT file [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm new to C++ and wanted to do the following thing: consider a txt file that looks like this for example:
+-+-+-+-+
+-+-o-+-o
+-o-+-+-+
+-+-+-+-+
Now I want to take the content of this file and copy it into an array of the same size and do the other thing around aswell. Thank you for your help, explaining your answer would be extra amazing :)

Using build-in arrays for variable sized things is a bit tricky. The easy way is to use a suitable std::vector<char> instead:
std::ifstream in(from_filename);
std::vector<char> array{std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>()};
// ...
std::ofstream out(to_filename);
std::copy(array.begin(), array.end(),
std::ostreambuf_iterator<char>(out));

Related

How to fill a std::vector<int64_t> with text [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a std::vector<int64_t> myVec in which I want to store a string (text). My understanding is that i cannot reserve space and write to myVec.data(), as this would be undefined behavior. What would be the non-hackish way to do this? I'm assuming the last int64_t will have to be filled with padding zeroes.
I'm using C++14.
If you are constrained to vector the only solution is resize() to required size and strcpy/memcpy into data()
Why would you store text in int?
Is your intention to store digits made from string?
If so, you shall:
std::vector<int64_t> myVec;
myVec.push_back(atoll("100"))

How likely is istream::ungetc() to work with a stringbuf (as used in stringstream)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've tried searching for the answer, but nothing mentions stringstream specifically. I would guess that it would always work and you can always go back as far as the beginning of the underlying string.
Am I right?
How likely is istream::ungetc() to work with a stringbuf (as used in stringstream)?
Well, never.
There's no such thing like istream::ungetc() defined from the standard.
You can use either
int std::ungetc( int ch, std::FILE *stream )
or
std::basic_istream& std::basic_istream::unget()

storing contents in vectors in C++ language [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do you store contents in a vector and then sort them without creating a class?
Doing a class project and it requires to not use a class. The books are not helping.
This page has a good example, assuming you can put all of the elements in the vector at once. If you need to put them in one at a time, use std::vector::push_back.
Something like std::vector<std::string> v; v.push_back("hello"); std::sort(v.begin(), v.end());. Get better books.

How to get variables to my program vrom text file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a file like:
a [able%5:00:00:capable:00] [able] capable
a [abnormal%3:00:00::] [abnormal]
a [absent%3:00:00::] [absent]
a [absolute%3:00:00::] [absolute] perfect or complete
a [abstract%3:00:00::] [abstract] existing only in the mind
a [abundant%3:00:00::] [abundant] plentiful
I want to get first column "avle", "abnormal" etc to my object. How to crop them and how storage them?
Use the string tokenizer, the best/easiest way to split a line into different variables.
char* word = strtok(line," [%:]");
char* word2 = strtok(0," [%:]");
int value = strtoi(strtok(0," [%:]"));
to store there is a vector container, but there can be used any type of arrays, what is more convenient

Fstream contain all the content of ofstream and ifstream? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Why do we use std;:ofstream for output and std::ifstream for input if it is possible to use std::fstream for both?
This is the similar case as with stringstream, istringstream and ostringsstream (link).
Usually you don't both read and write from the same stream, so using ifstream and ofstream is easier as you don't have to bother about the stream state and stream positions (as is the case with fstream).