Output Error from trying to read in words from a file. What's wrong?

So my text file says this:
1
2
3
4
5
I love you
all day and
all night
forever and
ever.


In my code listed below, everything is compiling correctly, however when I go to run the program, the last output displays everything correctly except for the words. Instead of giving me a list of words, it gives me "Words: 0xffbfeb00".
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

// Function Prototypes
void readFilename(ifstream&, string&);
void countCharsLines(ifstream&, int&, int&, char&);
void populateArray(ifstream&, string[]);
void outputResults(string&, int&, int&, string[]);

int main()
{
  // Variables
  ifstream inFile;
  string filename;
  int countLines;
  int countChars;
  char ch;
  string words[1000];

  // Function Calls
  readFilename(inFile, filename);
  countCharsLines(inFile, countLines, countChars, ch);
  populateArray(inFile, words);
  outputResults(filename, countLines, countChars, words);
  return 0;
}
// Function: readFilename
void readFilename(ifstream &inFile, string &filename)
{
  cout << "Please enter the file name you wish to open." << endl; // Reads in file name
  getline(cin, filename);
  inFile.open(filename.c_str()); // Opens file

  if (!inFile) // Displays error message
    {
      cout << "This file did not open properly and the program will now terminate.\nPlease make s\
ure the spelling of the file is correct." << endl;
    }
}
// Function: countCharsLines
void countCharsLines(ifstream &inFile, int &countLines, int &countChars, char &ch)
{
  string line;
  countLines = 0;
  countChars = 0;

  while(!inFile.eof())
    {
      getline(inFile,line); // counts lines
      countLines++;
    }

  while(!inFile.eof())
    {
      ch = inFile.get(); // counts characters
      countChars++;
    }

  inFile.close();
}


// Function: populateArray APPEARS TO BE THE CULPRIT
void populateArray(ifstream &inFile, string words[])
{
  for(int i=0;i<=1000;i++)
    {
      inFile >> words[i]; // Stores the words from the file into the array
    }
  inFile.close();
}

// Function: outputResults
void outputResults(string &filename, int &countLines, int &countChars, string words[])
{
  cout << "Filename: " << filename << endl;
  cout << "Number of lines: " << countLines << endl;
  cout << "Number of characters: " << countChars << endl;
  cout << "Words: " << words << endl;
}
Last edited on
Line 81 - 'words[]' is an array of strings right ? To print out elements of the words[] array you have to select specific elements like words[10].
What you are accessing when you write cout << words; is the address in memory of the array (like a pointer).

Just loop line 81 with cout << words[i] << endl; to print out each element.

You should also be doing some check to make sure that you're not trying to read beyond the file in that populateArray function.

Always use:
while(inFile) instead of while(!inFile.eof()) to confirm that file is reading. eof() is only 1 check (out of many) for why a read failed (not if it failed).

Line 36 - if (!inFile.is_open())

XD
Last edited on
Always use:
while(inFile) instead of while(!inFile.eof()) to confirm that file is reading. eof() is only 1 check (out of many) for why a read failed (not if it failed).


I would say it's pretty rare you'd want to use either.

There's a bigger problem with countCharsLines. After the lines are counted you attempt to count the characters... while the get pointer from the input file is still at the end of the file and the file stream is still in an eof state.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// it would make more sense to just supply the file name to the function
// and let the function take care of creating the file stream.
void countCharsLines(ifstream &inFile, int &countLines, int &countChars, char &ch)
{
    string line ;

    unsigned lineCount = 0 ;
    unsigned charCount = 0 ;

    while ( getline(inFile, line) )
    {
        ++lineCount ;
        charCount += line.size() ;
    }

    // Should we really be closing a file we didn't open?
    inFile.close();

    countLines = lineCount ;
    countChars = charCount ;
}


Note that whitespace characters are included in the character count (except for newlines.)
Last edited on
I am still getting issues, I tried to loop it, but now it doesn't print out anything, it's just blank.

Here is my updated code.
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
int main()
{
  // Variables
  ifstream inFile;
  string filename;
  int countLines;
  int countChars;
  char ch;
  string words[1000];

  // Function Calls
  readFilename(inFile, filename);
  countCharsLines(inFile, countLines, countChars, ch);
  populateArray(inFile, words);
  outputResults(inFile, filename, countLines, countChars, words);
  return 0;
}
// Function: outputResults
void outputResults(ifstream &inFile, string &filename, int &countLines, int &countChars, string words[])
{
  // Variables
  int i = 0;

  // Outputs
  cout << "Filename: " << filename << endl;
  cout << "Number of lines: " << countLines << endl;
  cout << "Number of characters: " << countChars << endl;
  cout << "Words: " << endl;

  while(!inFile.eof())
    {
      cout << words[i] << endl;
      i++;
    }
}
You close the file in countCharsLines.

It is still closed when you get to populateArray and outputResults. Perhaps now might be a good time to pay attention to the comments in the code snippet I posted above. It can be applied to these other functions as well.

@cire
I would say it's pretty rare you'd want to use either.

I don't mean any disrespect but I'm sure that everyone (myself included) would appreciate seeing an opinion like this expanded further to include why it is wrong and at least some sort of alternative. We can all learn from good explanations but opinions are about as useful as the C++ vs Java debate in another thread on this forum.


As for my part:
while(inFile >> words[i]) is what I should have said as an alternative to that 'for' loop on line 68 of OP's program.
Yes, as cire pointed out:while(inFile) is not reliable because it checks the state of the stream before processing it.
Last edited on
Soranz wrote:
I don't mean any disrespect but I'm sure that everyone (myself included) would appreciate seeing an opinion like this expanded further to include why it is wrong and at least some sort of alternative


An alternative was presented. Did you not take a gander at the code?

Soranz wrote:
Yes, as cire pointed out:while(inFile) is not reliable because it checks the state of the stream before processing it.


...
Did you not take a gander at the code?

There was solid code which I agreed with but no explanation why this while ( getline(inFile, line) ) is better. Which is what, "I would say it's pretty rare you'd want to use either." was about (I guess..).

...

Another implicit comment. We don't all know what you know.

Anyway. I'm not playing this game anymore so I'm apologizing for the awkwardness and obvious confusion. Yes, I started it - I stated my piece and now I'm done with it.

No hard feelings and certainly no disrespect.
I'd be happy to explain anything I post that you don't understand, however it would be counter productive to explain every little thing in every post. As it happens, I thought it is fairly obvious when you look at the code and trace through the execution why that particular snippet of code is better in most cases. Not to mention that particular bit gets explained quite frequently, both for extraction from ifstreams and istreams such as std::cin.


For the record, input of the form:

1
2
3
while ( filestream is good )
    get input
    process input


does the wrong thing, because it doesn't check to see whether getting input was successful before processing the input.
Last edited on
Topic archived. No new replies allowed.