how to push filestream into a char pointer

as practice , my goal
is to read from a file called test.txt and display the length of each word using the a count function.


for some reason i keep getting:

Exception thrown: write access violation.

_Ch was 0x919554.

on line 154: which is the line while (is >> *pp)

i am not sure if i can push from a is stream object into a char pointer using

while (is >> *pp)



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
  
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>

# include <fstream>

using namespace std; 



int count(char* &p )
{

	int total = 0;

	while (*p)
	{

		++total;

		cout << p << endl;

		++p;

	}

	return total; 

}



int main()
{

	 ifstream is("test.txt");

	 if (!is)
	 {

		 cerr << "file could not be found" << endl; 

		 return  EXIT_FAILURE; 

	 }


	 // if file exits
	 else {


		// string word; 

		 char * pp = "abc";

		 char ch; 

		 while (is >> *pp)
	     {

     		 cout << count(pp) << endl; 


	     }



	 }






	system("pause"); 

}


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
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>

using namespace std;

// removes all non-alphabetic characters so that we don't count them
string stripNonAlpha(const string& s)
{
  string retval;

  for (const char ch : s)
  {
    if (isalpha(ch))
      retval += ch;
  }
  return retval;
}

int main()
{
  ifstream is("test.txt");

  if (!is)
  {
    cerr << "file could not be found" << endl;
    return  EXIT_FAILURE;
  }
  string temp;
  while (is >> temp)
  {
    string word = stripNonAlpha(temp);
    cout << word << " => " << word.size() << "\n";
  }
  cout << "\n\n";
  system("pause");
}

text.txt
The agile dog jumps over the lazy fox.
OUTPUT

The => 3
agile => 5
dog => 3
jumps => 5
over => 4
the => 3
lazy => 4
fox => 3
for purpose of the exercise the function is supposed to take in a pointer to C-string
i am not sure if i can push from a is stream object into a char pointer using
while (is >> *pp)

http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt

A working version of your program could be:
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
// as practice , my goal is to read from a file called test.txt and display
// the length of each word using the a count function.
// for some reason i keep getting:
// Exception thrown: write access violation.
// _Ch was 0x919554.
// on line 154: which is the line while (is >> *pp)
// i am not sure if i can push from a is stream object into a char pointer using
// while (is >> *pp)
#include <cstring>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>


int count(char* p);
void waitForEnter();


int main()
{
    std::ifstream is("test.txt");
    if (!is)
    {
        std::cerr << "File could not be found.\n";
        return EXIT_FAILURE;
    }
    // if file exits
    std::string word;
    while (is >> word)
    {
        if(word.empty()) { continue; }
        char* pp = new char(word.size());
        std::strcpy(pp, word.c_str());
        std::cout << count(pp) << '\n';
        delete pp;
        pp = nullptr;
    }

    waitForEnter();
    return 0;
}


int count(char* p)
{
    int total = 0;
    while (*p)
    {
        ++total;
        std::cout << p << '\n';
        ++p;
    }
    return total;
}


void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Otherwise, to get C-strings, you could read the file by C-style functions.
Here is it in gool old plain C99
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
/* Simple prog to read words from a text file
 * and displays their length without punctuation
 *
 * Language: C99
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define BUF_SIZE  20

void stripNonAlpha(char input[], char output[])
{
  size_t out_len = 0;
  size_t len = strlen(input);

  for (size_t i = 0; i < len; ++i)
  {
    if (isalpha(input[i]))
    {
      output[i] = input[i];
      ++out_len;
    }
  }
  output[out_len] = '\0';
}


int main()
{
  FILE* src = fopen("test.txt", "r");
  char input[BUF_SIZE] = {0}, output[BUF_SIZE] = {0};

  if (src == NULL)
  {
    perror("File error");
    return -1;
  }
  while (fscanf(src, "%19s", input) != EOF)
  {
    stripNonAlpha(input, output);
    printf("%s => %u\n", output, strlen(output));
  }

  return 0;
}
Topic archived. No new replies allowed.