POINTERS - Ok, I feel stupid but...

...I need a little help.

I'm trying to copy a file into an array for analysis.

The error I'm getting is on line 43.

Also, my function at the end...All I did was convert my pseudo-code. If someone could look and tell me if that is going to do what I want it to do (count the characters, words and lines in the array). I've yet to see if it will work because I can't get it to compile past the line 43 error....

Any help or guidance would be greatly appreciated! :D

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
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
#include <ctype.h>

using namespace std;

void TextFileInfo(int& charcount, int& wordcount, int& linecount, char* currentchar);

int main (int argc, char* argv[])
{
    int charcount=0;
    int wordcount=0;
    int linecount=0;
    
    
    char inputFileName[256];
    if (argc <= 1)
    {
       cout << "Enter name of file:" << endl;
       cin.getline(inputFileName, sizeof(inputFileName));
       }
    else
    {
       *inputFileName = *argv[1];
       }    
    
   ifstream fin( inputFileName, ifstream::binary);
   if ( !fin ) {
      cerr << "File " << inputFileName << " not found!";
      getch();
      return 1;
      
      
   }//endif

  
      char currentchar[sizeof(fin)];
      while (!fin.eof())
      {
            *currentchar = fin.get();
            currentchar++; //this is where my error is... 
                           //"43 C:\School   \Main2.cpp ISO C++ forbids cast 
                           //to non-reference type used as lvalue 
      }
                 
           
           TextFileInfo(charcount, wordcount, linecount, currentchar);
           
           cout << "The file statistics are:\n\n"
           << "-------------------------------\n"
           << "Character Count: " << charcount << endl
           << "Line Count: " << linecount << endl
           << "Word Count: " << wordcount << endl
           << "-------------------------------\n" << endl;
           
           cin.get();
           return 0;
      }
      
      
void TextFileInfo(int& charcount, int& wordcount, int& linecount, char* currentchar)
{
     while(currentchar)
     {
          
          if(currentchar == " ")
          {
             charcount++;
          }
          else if(currentchar == "\n")
          {
                  linecount++;
                  charcount++;
          }
          
          else if ((*currentchar >= 48 && *currentchar <= 57) || (*currentchar >= 65 && *currentchar <= 90) || (*currentchar >= 97 && *currentchar <= 122))
          {
                  if (currentchar-- == " ")
                  {
                     wordcount++;
                     currentchar++;
                  }
               
          }
          else{
               charcount++;
          }
          currentchar++;
     }
}
Last edited on
1
2
3
4
5
6
7
8
      char currentchar[2048];        // this is a buffer (its name can be confusing)
      char* next_char = currentchar; // this is a pointer to the next character
      while (!fin.eof())
      {
            *(next_char++) = fin.get();
      }
      *next_char = '\0';
Firstly, sizeof(fin) does not return the size of your file, only the size of ifstream class.
see here how to get the length of the file http://www.cplusplus.com/reference/iostream/istream/tellg/

But you main problem is that you do maths with currentchar, which is not a pointer but an array.
You could either writechar* currentchar = new char[length];, which I don't suggest because you will then find it difficult to free the array, or char my_array[length]; char* currentchar = my_array;, though of course if you took my advice and decided to calculate the length of your file (and length would not be constant) you wold have to write char* my_array = new char[length]; char* currentchat = my_array;.
Good luck
Confused...

I thought currentchar was an address to the first thing (ie currentchar[0]) in the array.

and char* is a character address... why can't I do math between character addresses? Incrementing the address by 1 would be looking at the next thing in that array (ie currentcount[1], etc)
what are you trying to do with the line 43 of code? currentchar is a pointer and your telling it to be 1 number higher? just use an iterator. create int i and add one to it each time the while loop loops through and access the c stlye string using square brackets just like a normal array.

char currentchar[sizeof(fin)];
int i = 0;
while(!fin.eof)
{
currentchar[i] = fin.get();
i++;
}

i think this should solve that problem if i am correct on what you are trying to do. also i think i you should learn more about c style strings.

sorry if i completely misunderstood you or ur code as i am still a beginner but i do think i am correct

also may i ask how line 39 is compiling. you do not know the size of fin before run time so how can you create an array like that?
a pointer is an address in memory. And currentchar is pointing to the first address in the array. If I increment the address by 1 its pointing to the next item in that array.
When I added
1
2
3
4
5
6
7
char currentchar[2048];        
      char* next_char = currentchar; 
      while (!fin.eof())
      {
            *(next_char++) = fin.get();
      }
      *next_char = '\0';


it copied correctly...

But now I need to get TextFileInfo to work correctly...
aha! i figured it out. should have been using ascii dec codes.

Thanks everyone!
Topic archived. No new replies allowed.