Errrr help please

This is a discussion on Errrr help please within the C++ Programming forums, part of the General Programming Boards category; This displays whats in the text file but how can i make it store what it displays in test. I ...

  1. #1
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36

    Exclamation Errrr help please

    This displays whats in the text file but how can i make it store what it displays in test. I did test=infile.get() but it doesnt work.

    I had it working with something I did but I didn't save and I don't remember how I did it...err very aggervating....its drivin me crazy

    Code:
     cout<<"Enter The Input File(C:/example.txt): ";gets(input_file);fflush(stdin);
    ifstream infile;
       infile.open (input_file, ifstream::in);
    
            while (infile.good())
            cout<<(char) infile.get();
             test=infile.get();
             cout<<test<<endl;
    
    infile.close();
    Last edited by quiksilver9531; 02-17-2002 at 11:15 PM.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    as far as I know, this should work:

    Code:
    char test;
    
    infile >> test;

  3. #3
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36
    That didnt work

  4. #4
    Unregistered
    Guest
    //obtain file name to obtain text;
    cout<<"Enter The Input File(C:/example.txt): ";
    cin >> input_file;

    //variables to use in reading file, displaying input, and saving input
    char test[1000];
    char ch;
    int index = 0
    ifstream infile;

    //open stream and associate file with stream
    infile.open (input_file);

    Code:
    //read in first char
    infile >> ch;
    
    //while stream is "good" and haven't overead buffer
    while (infile.good() && index < 998)
    {
          //display input on screen 
          cout<<(ch);
          //save inpu to buffer
             test[index++] = ch;
           //get next input
            infile >> ch;
    }
    //change char array to string
    test[index] = '\0';
    //output buffer
    cout<<test<<endl;
    
    infile.close();

  5. #5
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36
    Thanks that worked but what can I do if my file is bigger than 1000 characters?

  6. #6
    Unregistered
    Guest
    If you declare too big an array you will overflow the programs static variable stack so you would need to use dynamic memory to declare the array, or use a list to hold the input rather than an array, which would also likely use dynamic memory.

  7. #7
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36
    how would you do that?

  8. #8
    Unregistered
    Guest
    char * buffer = new char[1000000];

    declares a buffer to hold 999,999 char (and a terminal null char) dynamically, assuming there is that much memory available in RAM. Whenever you declare dynamic memory, you also should release it . To do so with an array you would do this:

    delete [] buffer;

    I suggest you read up on dynamic memory in your favorite tutorial or text. It is a topic that is really too big to discuss fully in this type of forum. It is something you should learn how to use, if you wish to do much of anything besides simple programs however.

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