0

So this seems like such a simple problem but i am struggling with it. I have a big file that i want to just go through and put every char in the file into a char array. My problem is i dont know how to deal with the newlines and any whitespace. is there a way to just go through the file and grab all of the chars, skip over newlines, and put them in an array?

Yes, i have searched around on google but with no luck.

ageoff
  • 2,798
  • 2
  • 24
  • 39

4 Answers4

1

Here is how it would be trivially done in C. I am sure it can be done using the C++ interface as well, but the C library is part of C++ too.

#include <stdio.h>
#include <ctype.h>

FILE *f = fopen ("filename", "r");
if (!f)
   error ...

char array [BIGENOUGH];
int index = 0;
while (!feof (f))
{
    char c = fgetc (f);
    if (!isspace (c))
        array [index++] = c;
}
fclose (f):
wallyk
  • 56,922
  • 16
  • 83
  • 148
1

The preferred method would be to use the standard library string. Example of removing whitespace from a string here. How to read from a file line-by-line here.

Example code:

fstream file;
file.open("test.txt",ios::in);
while ( !file.eof() ) {
  string str;
  file >> str;

  remove_if(str.begin(), str.end(), isspace);
  str.erase(remove_if(str.begin(), str.end(), isspace), str.end());
  //save str here
}

file.close();

remove_if sample implementation:

template<typename T, typename P>
T remove_if(T beg, T end, P pred)
{
    T dest = beg;
    for (T itr = beg;itr != end; ++itr)
        if (!pred(*itr))
            *(dest++) = *itr;
    return dest;
}

This code is untested.

Community
  • 1
  • 1
N_A
  • 19,799
  • 4
  • 52
  • 98
1

The key to doing what you want in C++ is to take advantage of the formatted input operations. You want to ignore whitespace; the formatted input methods do exactly that.

Here is one way, using the canonical C++ input loop:

#include <fstream>
#include <iostream>

int main () {
  std::ifstream inFile("input.txt");

  char c;
  std::string result;
  while(inFile >> c)
    result.push_back(c);

  std::cout << result;
}

I prefer standard algorithms to hand-crafted loops. Here is one way to do it in C++, using std::copy. Note that this way and the first way are nearly identical.

#include <vector>
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>

int main () {
  std::ifstream inFile("input.txt");
  std::string result;
  std::copy(std::istream_iterator<char>(inFile),
            std::istream_iterator<char>(),
            std::back_inserter(result));
  std::cout << result;
}

Another way, this time with std::accumulate. std::accumulate uses operator+ instead of push_back, so we can read the file in a string at a time.

#include <vector>
#include <fstream>
#include <numeric>
#include <iostream>
#include <iterator>
#include <algorithm>

int main () {
  std::ifstream inFile("input.txt");
  std::string result =
    std::accumulate(
      std::istream_iterator<std::string>(inFile),
      std::istream_iterator<std::string>(),
      std::string());
  std::cout << result;
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

fgets allows you to read until a newline (notice that the newline still exists in the destination buffer, so make sure to overwrite it with '\0').
Read the file line by line, and each time concatenate your output with the previous output.

Eran Zimmerman Gonen
  • 4,375
  • 1
  • 19
  • 31