Please help, I don't know why it is not reading the file. I am working in VS C++ 2008, I saved "input_data.txt" in the header files, see below. What is wrong?

#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include "input_data.txt.h"
using namespace std;

class Node {
public:
    int x;
    Node *next;
    Node *prev;
};

class periodicElement
{   Node *head, *current, *prev, *tail;
public:
    periodicElement(){
        head = current = prev = tail = NULL;
    }

//***** builds linked list ******//
    void add(int value) {
        current = new Node;
        current -> x = value;
        current -> next = NULL;
        current -> prev = NULL;

//***** check if head equal to NULL *****//
        if (head==NULL){
head = current;
tail = current;}
        else {
prev -> next = current;
current -> prev = prev;
    }
tail = prev = current;
    }

//****** prints the linked list *****//
    void printRight() {
        current = head;
        while (current!=NULL) {
            cout << current -> x << endl;
            current = current -> next;

        }
    }

    void printLeft() {
        current = tail;
        while (current != NULL) {
            cout << current -> x << endl;
            current = current -> prev;
        }
    }
    };

//* an entry point for execution *//

int main() {
    ofstream outputFile("input_data.txt");
        ifstream inputFile;
        periodicElement one;
        one.add(23);
        one.insertNode(35);
        one.add(90);
        cout << "This prints from right to left:" << endl;
        one.printRight();
        cout << endl;
        cout << "This prints from left to right:" << endl;
        one.printLeft();
}

Recommended Answers

All 5 Replies

I saved "input_data.txt" in the header files, see below.

#include "input_data.txt.h"

Huh?

If you're trying to open the file for input, you use this bit here:

ifstream inputFile;

Open the file and read it?

how do I print the file?

how do I print the file?

  1. Open the file.
  2. Read all or part of the file into variable(s).
  3. Display the variable(s).
  4. Repeat steps 2 and 3 till end of file.

ok, this is what i have, i created just a different program just to open and read and print the header file i have to work only on this. What am I doing wrong? I want to print my file that i have, the file includes a column of numbers, column of two characters, and column of names.

#include <iostream>
#include <fstream>
using namespace std;

int main () {
ofstream myfile;
string name;
myfile.open ("Elements.h", ios::in); //opens file for input
getline(myfile, name); //reads file and prints
cout << name << endl;
myfile.close();
return 0;
}

You use ofstream to create an OUTPUT file stream. You use this if you want to write to a file.

To read from a file into your program, you need an INPUT file stream, so you want to use ifstream .

Change ofstream myfile; to ifstream myfile; Also, you are attempting to create a string object called "name".
You will need #include <string> if you want to create a string object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.