I need your sugestion

This is a discussion on I need your sugestion within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a problem.. I need your sugestion. I am doing a editor program in c++ where i need ...

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    can you please elobrate it.....

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    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
    Attached Files Attached Files

  6. #6
    Unregistered
    Guest
    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.

  7. #7
    Unregistered
    Guest
    oops, should have been if(buffer != NULL){


    sorry

  8. #8
    Unregistered
    Guest
    also you need to reset index to 0 after using it in the for loop and before using in the while loop.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21