-
I need your sugestion
Hi,
I have a problem.. I need your sugestion.
I am doing a editor program in c++ where i need to accept huge strings.. Some times 3600 chars long.. the problem is how do i do it... If i use cin or getline etc etc it can accept only limited amount of characters around 126 i guess.... After that it does not accepts any chars from the user and beeps when the keys are pressed... So how do i accept a string this big...
Thanx
Vasanth
-
Read a block of characters with getline, if that block does not contain a newline character then keep reading blocks until it does. Just keep placing the blocks in the same array until you hit the newline and you'll have one very long string.
-Prelude
-
can you please elobrate it.....
-
Here's the basic idea, sorry about the ugly code:
Code:
#include <stdio.h>
#include <string.h>
int main ( void )
{
int i = 0;
char buffer[1000] = {'\0'}, temp[20] = {'\0'}, *p;
while ( ( p = strchr ( buffer, '\n' ) ) == NULL && i < 1000 ) {
fgets ( temp, sizeof temp, stdin );
strcat ( buffer, temp );
i += strlen ( temp );
}
puts ( buffer );
return 0;
}
-Prelude
-
No that did not work... I have attached my c++ source file.. It accepts input from user.. Try making it accept more than 200 characters... (2 lines of data) ....
If you solve it please post it here...
thanx
vasanth
-
Code:
ifstream fin("filename");
char ch;
int index;
const int MAX = 35001;//hold string with up to 35,000 char
char * buffer = new char[MAX];
if(MAX != NULL)
{
for(index = 0; index < MAX - 1; index++)
{
buffer[index] = '\0';
}
while(fin.get(ch) != EOF && index < MAX - 1)
{
buffer[index] = ch;
}
cout << buffer;
}
Buffer will contain every char except EOF in filename if less than
35,001 char in file
You may need to jiggle the while conditional a little. I forget whether EOF is compatible with char type or not.
-
oops, should have been if(buffer != NULL){
sorry
-
also you need to reset index to 0 after using it in the for loop and before using in the while loop.