How to copy TXT file to char array in C ++

Im trying to copy the whole .txt file to a char array. My code works, but it does not contain spaces. So, for example, if my .txt file reads “I like pie” and I copy it to myArray, if I cout my array using a for loop, I get “ILikePie”

Here is my code

#include <iostream> #include <fstream> #include <string> using namespace std; int main () { int arraysize = 100000; char myArray[arraysize]; char current_char; int num_characters = 0; int i = 0; ifstream myfile ("FileReadExample.cpp"); if (myfile.is_open()) { while ( !myfile.eof()) { myfile >> myArray[i]; i++; num_characters ++; } for (int i = 0; i <= num_characters; i++) { cout << myArray[i]; } system("pause"); } 

any suggestions?:/

+13
source share
5 answers

WITH

 myfile >> myArray[i]; 

you read the file word by word, which causes missing spaces.

You can read the whole file in a line with

 std::ifstream in("FileReadExample.cpp"); std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); 

And then you can use contents.c_str() to get a char array.

How it works

std::string has a range constructor that copies a sequence of characters in a range [first, last] notes that it will not copy the last in the same order:

 template <class InputIterator> string (InputIterator first, InputIterator last); 

std::istreambuf_iterator iterator introduces an iterator that reads consecutive elements from the stream buffer.

 std::istreambuf_iterator<char>(in) 

will create an iterator for our ifstream in (the beginning of the file), and if you do not pass any parameters to the constructor, it will create an end stream iterator (last position):

The default built std :: istreambuf_iterator is known as an end-of-stream iterator. When a valid std :: istreambuf_iterator reaches the end of the underlying stream, it becomes equal to the end of stream stream iterator. Expressing dereferencing or adding it further causes undefined behavior.

So, this will copy all the characters, starting from the first in the file, until the next character becomes the end of the stream.

+33
source

Use the following code snippet:

 FILE *f = fopen("textfile.txt", "rb"); fseek(f, 0, SEEK_END); long fsize = ftell(f); fseek(f, 0, SEEK_SET); char *string = (char *)malloc(fsize + 1); fread(string, fsize, 1, f); fclose(f); string[fsize] = 0; 
+9
source

A simple solution if you are attached to char arrays and minimal modifications to your code. The snippet below will contain all spaces and line breaks to the end of the file.

  while (!myfile.eof()) { myfile.get(myArray[i]); i++; num_characters ++; } 
+1
source

A simpler approach would be to use the get () member function:

 while(!myfile.eof() && i < arraysize) { myfile.get(array[i]); //reading single character from file to array i++; } 
+1
source

Here is the code snippet you need:

 #include <string> #include <fstream> #include <streambuf> #include <iostream> int main() { std::ifstream file("name.txt"); std::string str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); str.c_str(); for( unsigned int a = 0; a < sizeof(str)/sizeof(str[0]); a = a + 1 ) { std::cout << str[a] << std::endl; } return 0; } 
0
source

Source: https://habr.com/ru/post/952344/


All Articles