Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,332 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,102 people online right now. Registration is fast and FREE... Join Now!




Can ifstream::read variable be static?

 
Reply to this topicStart new topic

Can ifstream::read variable be static?

prads
post 14 Mar, 2008 - 07:11 PM
Post #1


D.I.C Head

**
Joined: 22 Oct, 2007
Posts: 111


My Contributions


Hello,
In the example below: can buffer be a static variable? i.e can ifstream::read read data from a static variable?
For example can the declaration of buffer be as signed char buffer[100]?
CODE

// read a file into memory
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  int length;
  char * buffer;

  ifstream is;
  is.open ("test.txt", ios::binary );

  // get length of file:
  is.seekg (0, ios::end);
  length = is.tellg();
  is.seekg (0, ios::beg);

  // allocate memory:
  buffer = new char [length];

  // read data as a block:
  is.read (buffer,length);
  is.close();

  cout.write (buffer,length);

  delete[] buffer;
  return 0;
}


Thanks,
prads
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 14 Mar, 2008 - 07:30 PM
Post #2


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


Yes, it can be an array of char, although not recommended to use. If you define the array length to be 100 , a maximum of 99 characters can be stored in that array, the last character being '\0'.

Way to read: is.getline(buffer, 100, '\n');

Hope this helps.
User is offlineProfile CardPM

Go to the top of the page

prads
post 14 Mar, 2008 - 07:54 PM
Post #3


D.I.C Head

**
Joined: 22 Oct, 2007
Posts: 111


My Contributions


QUOTE(letthecolorsrumble @ 14 Mar, 2008 - 08:30 PM) *

Yes, it can be an array of char, although not recommended to use. If you define the array length to be 100 , a maximum of 99 characters can be stored in that array, the last character being '\0'.

Way to read: is.getline(buffer, 100, '\n');

Hope this helps.


In my actual code, the is.read loops several times, say 3*500 times, each time with fresh values stored in buffer. So would declaring buffer[100] outside result in an exceptional error violating the max size?...I also thought of a possible solution for it being declared as static buffer[100]={0}; , wherein it overwrites previous values to 0(zero). Is this required or does the function is.read itself overwrite its previous values?
Also as you mentioned can it hold only 99 characters?.....isn't it 0-99 which is 100 characters?
To be more clear i have the variable as follows:
CODE

for(int i=0;i<3;i++)
{
for(int j=0;j<500;j++)
{
// static signed char buffer[100]={0}; // is this required??
is.read((char*)buffer,100);
}
}

Thanks,
prads

This post has been edited by prads: 14 Mar, 2008 - 08:03 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 03:14AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month