// 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 = newchar(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.