Archived

This topic is now archived and is closed to further replies.

Tentuxius

I/O Stream - Outputting from a file

Recommended Posts

After looking at various online references about using Input/Output Streams I have learnt a lot, but I do have a problem (and didn't find a solution in searching through previous posts) and my problem is outputting from a stream. int HeaderLength; char * HeaderBuffer; ifstream Xfile; Xfile.open ("File.dat", ios::binary); if(Xfile.is_open()) { printf("Sucess opening file\n"); Xfile.seekg (3, ios::beg); HeaderLength = Xfile.tellg(); Xfile.seekg (0, ios::beg); printf("%i\n", HeaderLength); HeaderBuffer = new char [HeaderLength]; Xfile.read(HeaderBuffer, HeaderLength); cout.write (HeaderBuffer, HeaderLength); } else { //If for some reason it's not, report failure: printf("Failed opening file\n"); } So... cout.write (HeaderBuffer.... That outputs to my screen, great! so its working i can see the first three bytes of a data file. I wish to output not to my screen but put character information extracted from the file and put it into a variable... any ideas? [edited by - tentuxius on March 17, 2004 7:40:46 PM] [edited by - tentuxius on March 17, 2004 7:41:25 PM] [edited by - tentuxius on March 17, 2004 7:41:52 PM] [edited by - tentuxius on March 17, 2004 7:43:13 PM]

Share this post


Link to post
Share on other sites
I''m on my way out, but this should get you started.

Assuming your header looks like this:

struct Header
{
char cHeader[3];
char cVersion;
unsigned int iEntityCount;
unsigned int iTextureCount;
};


You can read it in like so:

// Fill in the header.

Header m_pHeader = new Header;
ifWorld.read(m_pHeader->cHeader, sizeof(char) * 3);
ifWorld.read((char *)&m_pHeader->cVersion, sizeof(char));
ifWorld.read((char *)&m_pHeader->iEntityCount, sizeof(unsigned int));
ifWorld.read((char *)&m_pHeader->iTextureCount, sizeof(unsigned int));
delete m_pHeader;


Where ifWorld is your infile stream (binary format).

You typecast as a character point an address of the variable you want the value to be stored in, than specify the size of the object as the second parameter.

Good luck =)

Share this post


Link to post
Share on other sites
Hi, I could use some clarity on the explaination (line explaination - once somethings explained to me I can usually deduct what to do)!!

...By this I mean, why are you using an array, and how is it placing data into my char type variables?

I just wish to get the first 3 characters out of a file, place them into a char variable... Help much wanted and much appreciated.

[edited by - Tentuxius on March 19, 2004 12:10:41 PM]

Share this post


Link to post
Share on other sites

class FileHeader
{
public:
char Header[3];
char * HeaderID;
};

int FileOpenTest2(void)
{

ifstream Xfile;
Xfile.open ("File.dat", ios::binary);
if(Xfile.is_open())
{
FileHeader HeaderBuffer = new FileHeader;
Xfile.read(HeaderBuffer->HeaderID,sizeof(char) * 3);
}
else
{
//If for some reason it's not, report failure:
printf("Failed opening file\n");
}
Xfile.close();
delete HeaderBuffer;
return 0;
}
[/CODE]
The above doesn't work, mainly coz i dunno what the hell I'm doing.


[edited by - Tentuxius on March 19, 2004 12:25:19 PM]

Share this post


Link to post
Share on other sites
Hey Tentuxius,

Here is a function I just wrote. This is what it does. It reads your file you define, then it writes each line to its own array in char *[]:

#include <stdio.h>
#include <string.h>
#include <fstream.h>

int readFile(const char *filename, char *newLines[]) {
int x, lineCount;
char w, line[255];
FILE *file;
ifstream fin;

// Define file location

file = fopen(filename, "r");

// Record the line data

if( file != NULL ) {
fin.open(filename, ios::in);
lineCount = 0;

// Search for new line

while( !feof(file) ) {
w = fgetc(file);
if( w == '\n' )
lineCount++;
}

// Get line

for( x = 0; x <= lineCount; x++ ) {
fin.getline(line, 255);
// Allocate memory

newLines[x] = new char[strlen(line)+1];
// Copy bytes to importLines

if( newLines[x] != NULL )
strcpy(newLines[x], line);
}
fin.close();
fclose(file);
}else {
return 0;
}

return lineCount;
}

int main() {
int lines;
char *importLines[255];

lines = readFile("c:\\test.txt", importLines);

if( !lines ) {
printf("Could not read file\n");
return 0;
}

// How many lines read

printf("How many lines in your file = %i\n\n", lines);

// Read line if file existed

printf("First line in file is: %s\n", importLines[0]);

return 0;
}


So you know, if your file has 2 lines and one is:

"Hello There"
and the other
"Good Bye"

importLines[0] will now equal "Hello There" and importLines[1] will equal "Good Bye". Also, the function "readFile" returns how many lines were found, starting at 0. So if you have 2 lines in your file, it will return saying you have one in case you want a special loop to read all.

Because importLines[2] is not allocated it will crash if you try reading the last line if I returned (lineCount+1). I tried it before, but I dont like it how you have to always do (lineCount-1) outside of the "readFile" function.

Also, if you only want the first three characters you could do something like:

char myChar[3];

// Hard Way

int i;

for( i = 0; i < 3; i++ ) {
myChar[i] = importLines[0][i];
}

// Easy Way

strncpy(myChar, importLines[0], 3);



I hope this helps,
- [BDS]StackOverflow

[edited by - BlueDev on March 19, 2004 2:44:45 PM]

Share this post


Link to post
Share on other sites
quote:

class FileHeader
{
public:
char Header[3];
char * HeaderID;
};

int FileOpenTest2(void)
{

ifstream Xfile;
Xfile.open ("File.dat", ios::binary);
if(Xfile.is_open())
{
FileHeader HeaderBuffer = new FileHeader;
Xfile.read(HeaderBuffer->HeaderID,sizeof(char) * 3);
}
else
{
//If for some reason it's not, report failure:
printf("Failed opening file\n");
}
Xfile.close();
delete HeaderBuffer;
return 0;
}




If that's the code you are using then it won't work because HeaderID is a pointer and you didn't allocate any space for it prior to reading from the file. Also you can't delete HeaderBuffer outside the if statement because it goes out of scope after you declare it in the next '}'. Did you mean to read using Header instead?

Hope that helps...





[edited by - PatrickD on March 19, 2004 4:10:13 PM]

Share this post


Link to post
Share on other sites