#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class ReadToLinkedList
{
private:
struct node
{
string data;
node *next;
};
public:
void ReadFromFile(ifstream& inFile, ofstream& outFile)
{
string value;
string number;
node *head;
node *temp1;
node *temp2;
bool first;
head = NULL;
temp1 = NULL;
first = false;
if(inFile.is_open())
{
while(!inFile.eof())
{
getline(inFile,number);
temp1->data = number;
temp1->next = head;
head = temp1;
node *temp1 = new node;
}
}
cout << temp1->data; //CRASHES HERE
};
};
int main()
{
ReadToLinkedList you;
ifstream inFile("INPUT.txt");
ofstream outFile("OUTPUT.txt");
you.ReadFromFile(inFile, outFile);
system("PAUSE");
return 0;
}
I crash when I try to output temp->data. Why?
Planet Telex, on 06 October 2012 - 06:33 PM, said:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class ReadToLinkedList
{
private:
struct node
{
string data;
node *next;
};
public:
void ReadFromFile(ifstream& inFile, ofstream& outFile)
{
string value;
string number;
node *head;
node *temp1;
node *temp2;
bool first;
head = NULL;
temp1 = NULL;
first = false;
if(inFile.is_open())
{
while(!inFile.eof())
{
getline(inFile,number);
temp1->data = number;
temp1->next = head;
head = temp1;
node *temp1 = new node;
}
}
cout << temp1->data; //CRASHES HERE
};
};
int main()
{
ReadToLinkedList you;
ifstream inFile("INPUT.txt");
ofstream outFile("OUTPUT.txt");
you.ReadFromFile(inFile, outFile);
system("PAUSE");
return 0;
}
I crash when I try to output temp->data. Why?
What's the proper way to read from an input file?

New Topic/Question
Reply




MultiQuote




|