1

I'm working on a project for school and I need to read in text from a file.

Sounds easy peasy, except my professor put a restriction on the project: NO STRINGS ("No string data types or the string library are allowed.")

I've been getting around this problem by using char arrays; however, I'm not sure how to use char arrays to read in from a file.


This is an example from another website on how to read in a file with strings.

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

The important line here is while ( getline (myfile,line) );

getline accepts an ifstream and a string (not char array).

Any help is appreciated!

Gunner Stone
  • 997
  • 8
  • 26
  • what's the result do you need if meet NO STRINGS, just an empty string "" or nothing but exit? – vinllen Sep 08 '16 at 01:25
  • @vinllen I'll quote the rule here : "No string data types or the string library are allowed." – Gunner Stone Sep 08 '16 at 01:26
  • If your instructor actually put that in all caps and doesn't explicitly state you can't use `std::vector` I'd use `std::vector`. See http://stackoverflow.com/questions/4761529/efficient-way-of-reading-a-file-into-an-stdvectorchar – Captain Obvlious Sep 08 '16 at 01:48
  • what does the text file contain? It is rather important because if it contains numbers, then one does not need strings, if it contains text, then one should use char* – CJCombrink Sep 08 '16 at 05:49

3 Answers3

4

Use cin.getline. Refer to this site for the format: cin.getline.

You can write something like this:

ifstream x("example.txt");
char arr[105];
while (x.getline(arr,100,'\n')){
    cout << arr << '\n';
}
Benson Lin
  • 1,394
  • 13
  • 17
1

ifstream has a method named get() that reads the contents of the file into a char array. get() takes, as parameters, a pointer to the array, and the size of the array; then fills the array up to the given size, if possible.

After get() returns, use the gcount() method to determine how many characters have been read.

You can use then, and a simple logical loop, to repeatedly read the contents of the file, in size-chunks, into an array, and collect all the chunks read into a single array, or a std::vector.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0

You can use the int i = 0; while (scanf("%c", &str[i ++]) != EOF) to judge the end of text input. str is the char array include newline which you wanted, and i is the input size.

You can also use while(cin.getline()) to read per line every loop in C++ style: istream& getline (char* s, streamsize n, char delim ); just like below:

const int SIZE = 100;
const int MSIZE = 100;
int main() {
    freopen("in.txt", "r", stdin);
    char str[SIZE][MSIZE];
    int i = -1;
    while (cin.getline(str[++ i], MSIZE)) {
        printf("input string is [%s]\n", str[i]);
    }    
}
vinllen
  • 1,369
  • 2
  • 18
  • 36