PDA

Click to See Complete Forum and Search --> : Opening and reading files


reeset
Nov 7th, 2000, 01:59 PM
Hi folks:

I'm very new to C++, and I am trying to figure out how to open and read a file. I do not want to the the MFC, but rather, would like to use something like the fread function so that I can read chuncks of the file. What this is going to be used for, is I have files that are basically one string, and about 5 MB each. I want to be able to read 255 character chunks out of the file, and search these chuncks for an particular ASCII character. If the character isn't in the chunk, then I need to place it into a variable and then re-read the file. Once the ASCII character is found, I will then take the contents of any previous chuncks, as well as what has just been read, and run it through a processor. If anyone can give me any advice on any of this, I would greatly appreciate it.

HarryW
Nov 7th, 2000, 04:27 PM
Try something like:


FILE *fpFile;
char buffer;

// open file for reading, check the syntax because it's been a while.
fpFile = fopen(FILE_PATH_AND_NAME, "r");

// Loop through and send characters to the processor until STOP_CHARACTER is found.
// Again, check the syntax
while ((buffer = getchar(fpFile)) != STOP_CHARACTER)
{
//Send character to processor
};

fclose(fpFile);


That code's off the top of my head and it's been a while since I wrote C code, so I expect there'll be lots of bugs :)

parksie
Nov 7th, 2000, 05:31 PM
Here's something to make your life easier - it's a tiny interface onto the low-level file i/o so you don't need to bring in too much excess :). You don't have to use it, but it helps if you want to keep your .exe file size down.


#ifndef __DIRECTFILE_H__
#define __DIRECTFILE_H__

#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>

class DirectFile {
public:
DirectFile() {
m_iFD = -1;
}

virtual ~DirectFile() {
//Close();
}

bool OpenRead(char *pcFilename, bool bBinary) {
if(m_iFD != -1) {
return false;
}
m_iFD = _open(pcFilename, (bBinary ? _O_BINARY : _O_TEXT) | _O_RANDOM | _O_RDONLY);
if(m_iFD == -1)
return false;
m_bMode = false;
return true;
}

bool OpenWrite(const char *pcFilename, bool bCreate, bool bBinary, bool bAppend) {
if(m_iFD != -1) {
return false;
}
m_iFD = _open(pcFilename, (bBinary ? _O_BINARY : _O_TEXT) | (bCreate ? _O_CREAT : 0) | _O_RANDOM | _O_RDWR | (bAppend ? _O_APPEND : _O_TRUNC), _S_IREAD | _S_IWRITE);
if(m_iFD == -1)
return false;
m_bMode = true;
return true;
}

bool Close() {
if(m_iFD != -1) {
int iResult = _close(m_iFD);
m_iFD = -1;
if(iResult == -1)
return false;
if(iResult == 0)
return true;
}
return false;
}

int Write(void *pData, int nSize, int nCount) {
if((m_iFD == -1) || (m_bMode == false))
return -1;

int iCount = _write(m_iFD, pData, nCount * nSize);
if(iCount == -1)
return -1;

return (iCount / nSize);
}

int Read(void *pBuffer, int nSize, int nCount) {
if((m_iFD == -1) || (m_bMode == true))
return 0;

int iCount = _read(m_iFD, pBuffer, nCount * nSize);
if(iCount == -1)
return 0;
return (iCount / nSize);
}

long GetPos() {
if(m_iFD == -1)
return -1;
return _tell(m_iFD);
}

long Seek(long lOffset, long lOrigin) {
if(m_iFD == -1)
return -1;
return _lseek(m_iFD, lOffset, lOrigin);
}

int m_iFD;
bool m_bMode; // true=write, false=read
};

#endif // __DIRECTFILE_H__

To use, something like:

DirectFile dfFile;
char pcBuf[255];
int iCount;

dfFile.OpenRead("filename.ext", false); // false = text mode

while(iCount = dfFile.Read(pcBuf, sizeof(char), 255)) {
// Search pcBuf for the character...
if(iCount < 255) break;
}

dfFile.Close();

...or similar. Harry's code will work just as well with a quick change to read 255 bytes at a time.

Wow...choices.