Storing a list of words from a .txt file into a string array

I know my code is VERY sloppy, but my objective is to read in a .txt file of shuffled words, store it into a string array and alphabetically sort the string array then output the sorted array into a different .txt file. My trouble is that I'm unable actually store the words in the .txt file into the array. I tried a for loop, using "myfile >> intArray[i];" but that didn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

const int SIZE = 5;

int main(){
    string check;
    string intArray[SIZE];

    ifstream myfile("words.txt");
	if(myfile.is_open())
	{
		cout << "File opened!" << endl;

        for(int i = 0; SIZE > i; i++)
		{
            getline(myfile, check, ' ');
            intArray[i] = check;
            cout << intArray[i];
		}
	}

    sort(intArray, intArray + SIZE);

    ofstream output("wordd.txt");
    if (output.is_open())
    {
       	 for (int j = 0; SIZE > j; j++)
       	 {
    		    output << intArray[j];
       	 }
    }
    output.close();
        
    return 0;
}

}
Last edited on
I wasn't exactly sure how line 9, 19, or 20 worked, but I added those in place of removing myfile >> intArray[i]; since that didn't work for me earlier.
Why don't you use a vector?
My class isn't at the point of vectors yet so my guess is that my professor is expecting us to do this without the use of vectors.
OK, lets do it in the hard way.
Here is a little demo on how to read the file. Try to integrate it in your program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <string>
#include "cstdio"

using namespace std;

void error(const string& msg)
{
  cerr << "\a** ";
  perror(msg.c_str());
  system("pause");
  exit(-1);
}

int main()
{
  const string srcName = "names.txt";
  const int MAX_NAMES = 5;

  string names[MAX_NAMES];

  ifstream src(srcName.c_str());
  
  if (!src)
    error("Error opening file " + srcName);
  
  int numRead = 0;
  while (numRead < MAX_NAMES && src >> names[numRead])
    numRead++;

  cout << "Read " << numRead << " names from file." << "\n\n";
  // print the names to check what names were read
  for (string& s: names)
    cout << s << " ";

  system("pause");
  return 0;
}
Hello, @Thomas1945

Please where does the actual reading of the names from the file take place? Is it in line 29?

That is,
29
30
while (numRead < MAX_NAMES && src >> names[numRead])
    numRead++;
@ayoesquire,

yes exactly in this part src >> names[numRead]
@Thomas1965

Thanks bro. I'll try it out.
is there a reason why we include "error reading file" lines or is that just for organized purposes.
When dealing with files there are many things that can go wrong. Printing an error message helps to locate the problem.
Alright thanks! Also I've seem to find the source of my problems... Compiling my program on Visual Studios will run successfully, provided that I change the stack limit in the settings (My original problem required an array size greater than 200k). But I'm using Atom and MinGW which for some reason doesn't allow me to store the text file into a string array. But I could do so with Visual Studios.
Last edited on
Topic archived. No new replies allowed.