Ironworks Gaming Forum

Ironworks Gaming Forum (http://www.ironworksforum.com/forum/index.php)
-   General Conversation Archives (11/2000 - 01/2005) (http://www.ironworksforum.com/forum/forumdisplay.php?f=28)
-   -   c++ question with files (http://www.ironworksforum.com/forum/showthread.php?t=84032)

Blade 02-02-2003 09:12 PM

ok so i spaced over break [img]tongue.gif[/img] i can't seem to figure out/rember how to read a file into a struct. I have written the struck and opened the files and checked them. Can anyone point me in the right direction? All i need to do is put the info in the file into different parts of the struct. Goes off muttering about homework.

Blade 02-03-2003 02:52 AM

lol i think i scared people away :( good thing i didn't post my calc. or physcics :D

andrewas 02-03-2003 07:42 AM

(variable) &lt&lt (file)?

Or does the file handle class have a read method? Ive never used files much in C++.

Neatest way to do it would be to overload the &lt&lt operator for your struct (which dosent neccesaerily mean turn it to a class, structs can have methods to) and just use the one call. But as long as it works youll get marks for it.

Vaskez 02-03-2003 09:11 AM

Why don't you just set up file objects....the following example is from my old BG2 XP Patcher:

FILE *f = fopen("BGMain.exe","rb+wb");
FILE *f2 = fopen("VKXPPatcher.log","rb+wb");
fread(&d,8,1,f2);

location = (int)d;
fsetpos(f,&location);
fread(&e,4,1,f);

as you can see I open 2 files for binary read and binary write and then read into the address of the variable "d" 8 bits from the file f2. You can set the read position with fsetpos....those are C methods though, which would of course work in C++ but I guess you have to do it with C++ methods?

andrewas 02-03-2003 09:41 AM

My previous post is embarassingly wrong. Use:

(file) &gt&gt (variable); instead.

I need more C++ practice.

What you get depends on what (variable) is. If its a BYTE you get one byte of data and have to write your own parser. Most versatile way of doing it, but hardly elegant.

Blade 02-05-2003 02:36 AM

well thanks for the replies, would have replyed earlier but the homework piles up quickly :( In case you were wondering what the code looks like, hay no snickering so what i'm proud of my work [img]tongue.gif[/img] here it is.
</font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">//This Program was written by Chris Nance
//The class is CS 113
//The date last modified 2 Febuary 2003

//This program reads in from one file the name and ip address of one set of computers
//and then matches them with another ip address read in from a second file, if the ip address
//doesn't match then it says so

#include &lt;iostream&gt;
#include &lt;cstring&gt;
#include &lt;fstream&gt;
using namespace std;

struct NamesWithId
{
char name[20];
int address1, address2, address3, address4;
};

struct CheckId
{
int address1, address2, address3, address4;
};

void checkip(NamesWithId FirstFile[], CheckId checker[]);

void main()
{
char list[30], query[30],ch;
NamesWithId FirstFile[150];
CheckId checker[20];
ifstream check, input;
int i, x;

cout &lt;&lt; &quot;Enter the name of the .list file&quot; &lt;&lt; endl;
cin &gt;&gt; list;

cout &lt;&lt; &quot;Enter the name of the .query file&quot; &lt;&lt; endl;
cin &gt;&gt; query;

input.open (list);
if ( input.fail() )
{
cout &lt;&lt; &quot;error opening .list file\n&quot;;
exit(0);
}
check.open (query);
if ( check.fail() )
{
cout &lt;&lt; &quot;Error reading .query file\n&quot;;
exit(0);
}

while(!input.eof())
{
for ( i = 0; i &lt; 150; i++)
{
input &gt;&gt; FirstFile[i].name;
input &gt;&gt; ch;
input &gt;&gt; FirstFile[i].address1;
input &gt;&gt; ch;
input &gt;&gt; FirstFile[i].address2;
input &gt;&gt; ch;
input &gt;&gt; FirstFile[i].address3;
input &gt;&gt; ch;
input &gt;&gt; FirstFile[i].address4;
}
}
while(!check.eof())
{
for ( x = 0; x &lt; 20; x++)
{
check &gt;&gt; checker[x].address1;
check &gt;&gt; ch;
check &gt;&gt; checker[x].address2;
check &gt;&gt; ch;
check &gt;&gt; checker[x].address3;
check &gt;&gt; ch;
check &gt;&gt; checker[x].address4;
}
}

checkip(FirstFile, checker);

}


//This file compares the two arrays of structs
void checkip(NamesWithId FirstFile[],CheckId checker[])
{
int i, x, output;
for (x = 0; x &lt; 20; x++)
{
for (i=0; i&lt;150; i++)
{
if( FirstFile[i].address1 == checker[x].address1)
{
if( FirstFile[i].address2 == checker[x].address2)
{
if( FirstFile[i].address3 == checker[x].address3)
{
if( FirstFile[i].address4 == checker[x].address4)
{
output = 1;
}
else output = 0;
}

}

}
if (output == 1)
{
cout &lt;&lt; &quot;The computer name is: &quot; &lt;&lt; FirstFile[i].name &lt;&lt; &quot; The ip address is: &quot;
&lt;&lt; FirstFile[i].address1 &lt;&lt; &quot;.&quot; &lt;&lt; FirstFile[i].address2 &lt;&lt; &quot;.&quot; &lt;&lt; FirstFile[i].address3
&lt;&lt; &quot;.&quot; &lt;&lt; FirstFile[i].address4 &lt;&lt; endl;
output = 0;
break;
}

}
}
}</pre>[/QUOTE]probably not elegant and it outputs a couple misalanious lines, and doesn't tell you if the input file is over 150 lines long but it works in the parameters, well except for telling you if the input is over 150 lines i was being lazy and thinking whail having a headach isn't the most conductive thing for programing. Just give me credit if you "borrow" the code or parts of it ;) the input from the file to the struct was monumentally eazy once i rembered how to do it *shakes head in shame* o well. All you do is say input >> variable. where input is the input file stream and variable is what you want to put it into. Told you i wasn't thinking, you were right thanks. For the rest of the odd syntax problems i have i'm thankfull visualstudio.net

[ 02-05-2003, 02:38 AM: Message edited by: Blade ]


All times are GMT -4. The time now is 12:13 AM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
©2024 Ironworks Gaming & ©2024 The Great Escape Studios TM - All Rights Reserved