-1

/* In the text file I have a char followed by a blankspace then a string. I'm trying to read the char and string into seperated arrays. Any help is appreciated */

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
 char arrivOrDepart;
 string licensePlt;
 ifstream inFile;
 inFile.open("Text.txt");
 if (!inFile)
 {
  cout << "Can't open file" << endl;
  return 1;
 }
 for (int i = 0; i < 4; i++)
 {
  getline(cin, arrivOrDepart[i]);
  getline(cin, licensePlt[i]);
 }

 inFile.close();
 cin.get();
 return 0;
}

//text file A QWE123 A ASD123 A ZXC123 A WER123 A SDF123

dk019
  • 1
  • As silly as this is going to sound, it's going to be easier to read the character as a `string`. Then you can `while (cin >> arrivOrDepart[i] >> licensePlt[i]) { i++: }` – user4581301 Oct 06 '18 at 23:46
  • That said, if you want to get posh, aggregate `arrivOrDepart` and `licensePlt` into a `struct` so you only have to deal with one array of that `struct`. As an added bonus, you can then write a `operator>>` overload for that `struct` and simplify to `while (cin >> mystructarray[i]) { i++: }` – user4581301 Oct 06 '18 at 23:48
  • I need both arrays for other tasks. Can't just have one string. I keep getting errors with getline – dk019 Oct 06 '18 at 23:48
  • And then you can go a step further and use a `std::vector` in place of an array. This will eliminate the maximum of 4 inputs in the file. – user4581301 Oct 06 '18 at 23:49
  • the real question I have is how to read both into separate arrays without including the blank space. I have to set these arrays up dynamically but wasn't able to get it statically – dk019 Oct 06 '18 at 23:51
  • You cannot `getline` to a single character. Only to a `string` or (with a different `getline`) to an array of characters. Next, if you are going to separate based on `getline`, use `getline`'s third parameter to set the delimiter. Otherwise it will look for the end of the line, – user4581301 Oct 06 '18 at 23:51
  • how would that statement look? I've tried getline with negative results – dk019 Oct 06 '18 at 23:53
  • Are you required to use `getline` for this task? Normally you wouldn't use `getline` here you would do something like `string temp; cin >> temp >> licensePlt[i]; arrivOrDepart[i] = temp[0];` – user4581301 Oct 06 '18 at 23:59

2 Answers2

1
#include <fstream>
#include <iterator>
#include <vector>

this reads from file into vector

std::ifstream input("d:\\testinput.txt");

std::vector<std::string> bytes(
     (std::istreambuf_iterator<std::string>(input)),
     (std::istreambuf_iterator<std::string>()));

input.close();

then, just put the data into whatever container you want. you should almost always prefer vector over array btw

skeller
  • 1,151
  • 6
  • 6
1

There are a few problems with the code:

  1. getline is the wrong tool of choice for this. if you want to split a stream based on spaces, use >>.
  2. arrivOrDepart and licensePlt are not defined as arrays but are used as arrays.
  3. reading from cin, not from file.

My suggested fixes (excluding using vectors instead of arrays):

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std; // avoid using this
int main()
{
    const int MAXARRAY = 4; // avoid using magic numbers
    char arrivOrDepart[MAXARRAY]; // made an array, but prefer std::vector
    string licensePlt[MAXARRAY]; //made an array
    ifstream inFile;
    inFile.open("Text.txt");
    if (!inFile)
    {
        cout << "Can't open file" << endl;
        return 1;
    }
    string temp;
    int i = 0;
    while (i < MAXARRAY && // not overrunning the arrays 
            inFile >> temp >> licensePlt[i] && // read data from file stream
           temp.length() == 1) // read only one character for arrivOrDepart 
    {
        arrivOrDepart = temp[0];
        i++;
    }

    inFile.close();
    cin.get();
    return 0;
}

Recommended reading:

Why is "using namespace std" considered bad practice?

What is a magic number, and why is it bad?

std::vector documentation (Alternate easier to read but often less accurate documentation)

std::getline documentation. Note the third parameter used to set the parsing delimiter.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • just has to tweek statement to – dk019 Oct 07 '18 at 00:32
  • for (int I = 0; I < MAXARRAY; I++) { inFile >> arriveOrDepart[I] >> licensePlt[I];} – dk019 Oct 07 '18 at 00:33
  • @dk019 That has a mistake in it. Nothing checks `inFile >> arriveOrDepart[I] >> licensePlt[I];` to make sure the read succeeded. You could wind up reading garbage from the file or reading nothing and you'll never know unless the program crashes later over bad data. – user4581301 Oct 07 '18 at 01:04