Skip to main content
gamedev.net
🔒 Locked

Best file reading method?

Started by Xeile Oct 16, 2004 at 3:15 AM 4 replies 1.6k views
Original Post
Xeile
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?
nife
nife
fgets reads a line at a time.
Killers don't end up in jailThey end up on a high-score!
xtrmntr
xtrmntr
What were the desired results you were looking for in fstreams?
Dim_Yimma_H
Dim_Yimma_H
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 instanceint 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.
Xeile
Xeile
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.
MaulingMonkey
MaulingMonkey
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 betterusing 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.txtFile: example.txtContains 2 linesFirst line: I like to eat pieThird character of first line: lC:\eclipse\workspace\test>main example2.txtFile: example2.txtContains 1 linesFirst line: PIE PIE PIE PIEThird character of first line: EC:\eclipse\workspace\test>main empty.txtFile: empty.txtContains 1 linesFirst line:The first line contains less than 3 characters.C:\eclipse\workspace\test>main tc.txtFile "tc.txt" could not be found.C:\eclipse\workspace\test>main twocharacters.txtFile: twocharacters.txtContains 1 linesFirst line: tcThe first line contains less than 3 characters.


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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.