0

I'm trying to open a file and save the information there in an array of chars, however I'm not getting it. To save in a string use this:

int main(){
string line1;
ifstream myfile;
myfile.open("example.txt");

if(!myfile){
   cout<<"Unable to open the file."<<endl;
   exit(0);
}
while(getline(myfile,line1)){
   ReadFile(myfile);
}

}

And It works. When I use an array of chars, I code like this:

int main(){
int size=100;
char line1[size];
ifstream myfile;
myfile.open("example.txt");

if(!myfile){
   cout<<"Unable to open the file."<<endl;
   exit(0);
}
while(myfile.peek()!EOF){
   line1[size]->ReadFile();
}

}

The function ReadFile is this:

void ReadFile(ifstream &is){
   char aux[100];
   is.getline(aux,100);
}
Alice
  • 49
  • 1
  • 9
  • `myfile.peek()!EOF` doesn't seem right. `line1[size]->myfile;` is also weird. – Quimby Nov 10 '18 at 18:09
  • 1
    Don't use an array of chars then - a string is the correct thing to use here. Also, `char line1[size];` is not valid C++ code. –  Nov 10 '18 at 18:10
  • I've used it in other programs and it worked. Do you have any suggestions? @Quimby – Alice Nov 10 '18 at 18:10
  • @NeilButterworth I need to use chars to implement another function that counts the number of chars in the file – Alice Nov 10 '18 at 18:11
  • Use stringstreams to read whole files. https://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring – Quimby Nov 10 '18 at 18:12
  • @Alice No, you don't need to do that. Anything you can do with an array of char can be more easily and more correctly done using a string or a vector. –  Nov 10 '18 at 18:12
  • @Alice the code you posted won't not compile, because `line1[size]->myfile;` doesn't have any member named `myfile`. – Quimby Nov 10 '18 at 18:15
  • @NeilButterworth I have tried with strings (the counting of chars) and I use length() to have the total of chars. For example I want this output: "Hello" ->6 chars (with the \n) and "h":1 time "e":1 time "l":2 time "o":1 time – Alice Nov 10 '18 at 18:16
  • @Quimby so I could creat a new char variable? – Alice Nov 10 '18 at 18:18
  • @Alice What? That expression is not C++, it won't compile, I don't know what you meant to write, but that code is not correct. – Quimby Nov 10 '18 at 18:21
  • @Quimby I meant line1[size]->ReadFile(); – Alice Nov 10 '18 at 18:36
  • @Alice That's still incorrect. – Quimby Nov 10 '18 at 18:40

1 Answers1

0

To read in an array of characters, or text, you can use std::getline and std::string:

std::string text;
std::getline(myfile, text);

To process text lines in a file:

std::string text;
while (std::getline(myfile, text))
{
  Process_Text(text);
}

Don't use arrays of characters, as they can overflow. Also, instead of using == to compare, you'll have to use strcmp. Always verify that your array of characters is terminated by a nul character, '\0', otherwise the string functions will go beyond your array, not stopping until a nul is found.

Edit 1: Space separated
To read in text that is space separated, use:

std::string text;
myfile >> text;

Edit 2: Counting characters in a string
You can count characters in a string by using another array.

unsigned int frequency[128] = {0}; // Let's assume ASCII, one slot for each character.
// ... read in string
const size_t length(text.length());
for (size_t index = 0; index < length; ++index)
{
  const char letter = text[index];
  ++frequency[letter];
}
Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144