• Advertisement
Sign in to follow this  

Best file reading method?

This topic is 4879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

MS Visual C++ 6.0 I have this application, were you can drag'n drop a file on. I managed to receive which file is being dropped, but I have some difficulty reading the file. Can someone help me with this please? I tried a bit with fstreams and FILE, but I didn't get the desired results. The file contains only legal ASCII characters. I would like to read it per line. So I have this 2-D char-array where I want the result to be in. Can someone help me with this please?

Share this post


Link to post
Share on other sites
Advertisement
You could also do this:


#include <iostream>
#include <fstream>

void OpenFileForRead(char* fileName);
void CopyFileToChar(char* charBuffer);

char charsFromFile[255];

std::ifstream iFile; //in File instance

int main()
{
OpenFileForRead("a file.txt");
CopyFileToChar(charsFromFile);

//display the char list
std::cout <<charsFromFile <<std::endl;

return 0;
}

void OpenFileForRead(char* fileName)
{
//open in file stream
iFile.open(fileName);
}

void CopyFileToChar(char* charBuffer)
{
iFile.seekg(0); //go to beginning of file

//get chars from file
short unsigned c = 0; //char counter

while ((iFile.eof() == false) && (c < 255))
{
iFile.get(charsFromFile[c]);
c++;
}
}



Then look for '\n' in the charBuffer in CopyFileToChar to determine whether a line has been read, store the c counter to know where you stopped reading and use seekg(c) next time.

Share this post


Link to post
Share on other sites
I just succeeded partly: I got the entire file in a character-array. From here is just simple programming to reach the desired goals.

A piece of my source if some-else want to know how I managed it.


TCHAR szFileName[_MAX_PATH + 1];
DragQueryFile( hDrop, 0, (LPTSTR)szFileName, _MAX_PATH + 1 );
OFSTRUCT sOFile;
HFILE hFile = OpenFile( (LPTSTR)szFileName, &sOFile, OF_READ );

char lpBuffer[600];
ULONG lpBytesRead;
DWORD BytesToRead;
BytesToRead = GetFileSize( (void*)hFile, NULL );
if( !( ReadFile( (void*)hFile, &lpBuffer , BytesToRead, &lpBytesRead, NULL ) ) )
{
return;
}





But thank you for the give help.

On problem I had with the 'std' class/namespace, that the compiler didn't saw it as a class/namespace, so I can't use it. I don't know why since I got my includes right.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xeile
MS Visual C++ 6.0

I have this application, were you can drag'n drop a file on. I managed to receive which file is being dropped, but I have some difficulty reading the file.

Can someone help me with this please? I tried a bit with fstreams and FILE, but I didn't get the desired results.

The file contains only legal ASCII characters.
I would like to read it per line. So I have this 2-D char-array where I want the result to be in. Can someone help me with this please?


Instead of giving an example using a 2-D char array, which I think is only what you THINK you should be using, or an array of char pointers pointing to char arrays, I'll give the crisp clean C++ version:

*edit: better version made ^_^*

#include <fstream>
#include <iostream>
#include <string>
#include <deque> //like vector, only better
using namespace std;

int main ( int argc , char ** argv )
{
//Variables we use:
string filename = "example.txt";
deque< string > buffer;

//Initialization
if (argc > 1) filename = argv[1];
ifstream myfile( filename.c_str() );

//Is it a valid file?
if (!myfile.is_open())
{
cout << "File \"" << filename << "\" could not be found." << endl;
return 1;
}

//Read into the buffer:
while (myfile.good())
{
string line;
getline( myfile , line );
buffer.push_back(line);
}

cout << "File: " << filename << endl;
cout << "Contains " << buffer.size() << " lines" << endl;
if (!buffer.empty())
{
cout << "First line: " << buffer[0] << endl;
if (buffer[0].size() >= 3)
{
cout << "Third character of first line: " << buffer[0][2] << endl;
}
else
{
cout << "The first line contains less than 3 characters." << endl;
}
}
else
{
cout << "Can't print out the first line, there is none." << endl;
}
}



Here's an example output:

C:\eclipse\workspace\test>main example.txt
File: example.txt
Contains 2 lines
First line: I like to eat pie
Third character of first line: l

C:\eclipse\workspace\test>main example2.txt
File: example2.txt
Contains 1 lines
First line: PIE PIE PIE PIE
Third character of first line: E

C:\eclipse\workspace\test>main empty.txt
File: empty.txt
Contains 1 lines
First line:
The first line contains less than 3 characters.

C:\eclipse\workspace\test>main tc.txt
File "tc.txt" could not be found.

C:\eclipse\workspace\test>main twocharacters.txt
File: twocharacters.txt
Contains 1 lines
First line: tc
The first line contains less than 3 characters.


[Edited by - MaulingMonkey on October 16, 2004 6:54:50 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement