Reading an in-memory file

I am trying to write a program that will re-organize the data of a file that has been loaded into memory.

I already have the part of the program that will do the re-organizing, and I have already loaded the file that needs to be re-organized into memory. However, I can't get one part of the program to talk to the other part of the program.

How do I tell my program to read the in-memory file one char at a time and pass that info to the program that will do the re-organizing?
I'd probably do it with a function call.
As Moschops said, you should call a function with the address of the memory location containing the file as an argument.
Thanks, that is a great idea. . . except I don't know how to do it. . . I know functions,
but I don't know the code to place into the function. I've been trying to use ifstream infile >> ch,
but that gives:
1
2
3
4
5
6
7
8
9
10
11
12
13
1>c:\tmci\reorganize_4\reorganize_4\reorganize_4.cpp(69):
 error C2440: 'initializing' : cannot convert from
 'char' to 'std::basic_ifstream<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          No constructor could take the source type, or constructor
 overload resolution was ambiguous
1>
1>Build FAILED.
 


I don't know how to accomplish what I need to do. I'd give you my code, but it is 118 lines long.
You have to create the ifstream first, and then you can read a character from it.

1
2
3
char ch;
ifstream infile("filename");
infile >> ch;


http://www.cplusplus.com/doc/tutorial/files/
Sorry, I am confused. Wasn't the ifstream already created when the file was loaded into memory?
If you want the file "in memory" then you will have to load the file associated with the fstream to some chunk of contiguous memory, won't you?
Yes, I agree. However, The file in question is already in memory. I just want to update it by deleting what I don't want from what is already in memory. I thought that the process of loading it in memory also created the ifstream for that file?
Once you've read the file into memory, you just access that memory. You have no more use for the ifstream.
Yes I agree. My problem is that I want the program that I'm writing to access the memory,
and to delete what I don't want from memory.
I do not know how to tell my program to access what is in memory.
The other part of the program will delete what I don't want to keep.
I am writing a function that will load the program into memory, and another group of functions that will delete isalpha, ispunct, ect.

I need to learn how to tell my program exactly how to read what's in memory.
all you have to do is to open the source file, find the size of the file using fseek and ftell, and read the file into a properly allocated buffer, than manipulate/remove the undesired characters in the buffer, and finally write the buffer into another file. the type of buffer can be char* or a vector of type char, so you can easily manipulate it, i.e removing, adding, replacing and so on...
I assume the file contents were buffered into some memory. That memory has an address (hopefully contiguous) and can be read from. Depending on how that buffer was created and filled you may or may not be able to alter it as-is, but may only be able to read its contents. If this is the case you can just create another buffer to mirror the changes you want.

Allocate some memory big enough:

1
2
char* someMemory = new char(theSizeOfTheFile);
char* locationInMemory = someMemory;


Read into it (in a very inefficient way, but this is just to illustrate)

1
2
3
4
5
for (int i=0; int i<numberOfBytesInFile; i++)
{
  inputFile >> someMemory;
  someMemory++;
}



Create new output file:
1
2
ofstream myfile;
myfile.open ("outPutFile");


Write bytes to output:

1
2
3
4
5
6
7
8
9
10
11
locationInMemory = someMemory;

for (int i=0; int i<numberOfBytesInFile; i++)
{
  if (wantToKeepThisByte)
  {
      myfile << *locationInMemory;
  }
  locationInMemory++;
}
  myfile.close();
Topic archived. No new replies allowed.