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}

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 6688
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

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

Similar topics

6
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: ...
7
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...
7
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...
11
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;
6
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; };...
9
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...
3
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;
12
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: ...
1
KevinADC
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...
0
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...
0
isladogs
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...
0
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...
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
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...
0
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.