|
-
Nov 7th, 2000, 02:59 PM
#1
Thread Starter
Hyperactive Member
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.
-
Nov 7th, 2000, 05:27 PM
#2
Frenzied Member
Try something like:
Code:
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
Harry.
"From one thing, know ten thousand things."
-
Nov 7th, 2000, 06:31 PM
#3
Monday Morning Lunatic
Have a class on me...
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.
Code:
#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:
Code:
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|