468,497 Members | 1,899 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,497 developers. It's quick & easy.

C++ {how to count characters in a unknown size of a string}

1
Hello,

I am very new to C++ programming. I am trying to read a file with very large strings back to back, no commas or any other seperations between characters in a string and the size of the string is not known as well. I need to count how many characters totoal in a unknown size of a string.

this is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. #define NEWLINE '\n'
  6. int main () {
  7.    string line;
  8.    char* p;
  9.    int i = 0;
  10.    ifstream in ("test.v");
  11.    while (getline (in, line))
  12.    {
  13.    p = &line[0];
  14.    while (endl == false)  // This does not work!
  15.    {
  16.      i = i + 1;
  17.    }
  18.      cout << "Total characters in this line is :  " << i;
  19.   for (int n = 0; n < i; n++)
  20.   {
  21.     cout << "seq is : " << p[n] << endl;
  22.   }
  23.    }
  24. }
  25.  
Could someone suggest a better way?

thanks
Dec 3 '07 #1
3 6269
epots9
1,351 Expert 1GB
Moved to the C++ Forums where the resident experts can better assist you, and please use code tags.

**Moved from Programming Challenges
Dec 3 '07 #2
xoinki
110 100+
hi,
just have a
Expand|Select|Wrap|Line Numbers
  1. while(1)
  2. {
  3.     if(foef(fp))
  4.         break;
  5.     count += getline(); // should loop through here and fetch char by char until u find
  6.                                 // '\n' when u find '\n' return the count you have been 
  7.                                 // incrementing.   
  8. }
  9.  
Regards
Xoinki
Dec 3 '07 #3
try this
#include <iostream>

#include <fstream>

#include <string>

using namespace std;

#define NEWLINE '\n'

int main () {

string line;

char* p;

int i = 0;

ifstream inFile ("test.v"); //i don't remember if in was a serverd word so I changed the variable name;
.
while (!(inFile.eof()) //this is while is not end of file

{
char Line[512]; // this is a buffer i think
string cad;
int val = 256; //this is the maximum chars it's going to get
inFile.getline((char*)&Line, val, '\n'); //it stops reading when it reaches val or when it finds the '\n' wildcar
cad = line;
}
/* while (endl == false) // This does not work!

{

i = i + 1;

} I didn't understand this piece of code */

cout << "Total characters in this line is : " << cad.length(); // the method length() of strings returns the string size ;)

/* for (int n = 0; n < i; n++)

{

cout << "seq is : " << p[n] << endl;

}*/

}

}




tell me if there's something you didn't understand
Dec 3 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Jim Showalter | last post: by
9 posts views Thread by Adi | last post: by
3 posts views Thread by waynejr25 | last post: by
12 posts views Thread by Nezhate | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.