read preexisting file

hi everyone
could u plz help me in doing this work
. read first 32 charcters of a preexisting text file and then find out ascii value of each character then take next 32 characters and do the same put all d values in output file
Here is the code which can read your file using 32 bytes width window. If the length of the file is divided by 32 with the rest last window will be shorter. What is left to recognize the characters ASCII codes. You can use snprintf( .... ) with params to format them and write in to the new buffer or you can check their values using decimal numbers or hex because in the buffer you have chars.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  int maxSize;
  unsigned int _32bytesWindowsNumber = 0;
  unsigned int theRest = 0;
  char buffer = new char[32];
  ifstream is;
  
  is.open ("test.txt", ios::binary );  
  is.seekg (0, ios::end);
  maxSize = is.tellg();
  
  is.seekg (0, ios::beg);
  _32bytesWindowsNumber = (int)(maxSize/32);
  theRest = maxSize%32;
    
  for (unsigned int = 0; i<_32bytesWindowsNumber; ++i){
  	memset( buffer, 0x00, 32);
  	is.read(buffer, 32);
  	... do something with the chars in buffer
  }
  if (theRest){
  	memset( buffer, 0x00, 32);
  	is.read(buffer, theRest);
  	... do something with the chars in buffer
  }
  is.close(); 
  delete[] buffer;
  return 0;
}
adzajac: The idea is not to do peoples homework for them. In the end it does help you and it doesn't help them.
Bjarne Stroustrup wrote:
Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient.

Also, ¿why are you setting the buffer to 0 just before reading?
The buffer is set to ) rather for educational purposes. Specially in the situation when theRest exists. In this case it could be easier to create null terminated string. But I agree that it's not necessary.

By the way it's still unfinished homework because there is still a task with ASCII detecting ;).
Topic archived. No new replies allowed.