473,385 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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


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. }
Could someone suggest a better way?

Dec 3 '07 #1
3 6688
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
110 100+
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. }
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

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

Similar topics

by: deko | last post by:
In a multi-user environment, I have a table that stores hyperlinks to documents that are stored on the machine that hosts the mdb database. The table entry looks like this: ...
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
by: Jim Showalter | last post by:
I'm trying to write code that gets fixed-length strings from the user and then stores them in a linked list. Here's the definition of my list node: struct node {char str; struct node* next; };...
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
by: Nezhate | last post by:
Hi all, Is there any way to print a matrix's elements using a simple printf ? what I want as result is for example: if mat ={0,1,2,3} result must be: "0123". I tried this code: ...
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.