PDA

Click to See Complete Forum and Search --> : Reading lines from a file.


Michael_Bray
Dec 9th, 2002, 03:32 AM
Hi,

I wrote a function where I want to read 2 lines from a file. Each line will be soimething like

A + B - C
A - B + A - D - F

etc

I wrote a function as follows:


void GetData(string& Infix, string& Pre)
{
ifstream ins;
ins.open("data.txt");
char buffer[80];

int Count = 0; // Counter used to update position in array where we are saving the floppies contents to.

string data;
string FloppyData[2];

while(!ins.eof()){
getline(ins,data);
FloppyData[Count] = data;
Count++;
}

Infix = FloppyData[0];
Pre = FloppyData[1];

}


I can't see whats wrong with it. I want the FloppyData array to hold two strings, then assign Infix and Pre to the right values in the array.

Thanks in advance for any feedback.

ChrisWise
Dec 11th, 2002, 10:26 AM
All I did to get your code working is specified an absolute path to the file




#include "iostream"
#include "string"
#include "fstream"


using namespace std;

void GetData(string& Infix, string& Pre)
{

ifstream ins("c:\\data.txt");


int Count = 0; // Counter used to update position in array where we are saving the floppies contents to.

string data;
string FloppyData[2];

while(!ins.eof()){
getline(ins,data);
FloppyData[Count] = data;
Count++;
}

Infix = FloppyData[0];
Pre = FloppyData[1];

}


int main()
{
string i,p;

GetData(i,p);

cout << i << endl;
cout << p;

system("Pause");

return 0;

}

Michael_Bray
Dec 11th, 2002, 05:04 PM
:( I can compile that, but when I try to run it comes up with a pop up message saying - btw, I wouldn't have a clue where the created error log is.

Program Error

file.exe has generated errors and will be closed by Windows.

An error log is being created.