944,100 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 760
  • C++ RSS
Nov 4th, 2009
0

cannot open file, no such file or directory

Expand Post »
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();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gem74 is offline Offline
18 posts
since Oct 2009
Nov 4th, 2009
0
Re: cannot open file, no such file or directory
Click to Expand / Collapse  Quote originally posted by Gem74 ...
I saved "input_data.txt" in the header files, see below.

C++ Syntax (Toggle Plain Text)
  1. #include "input_data.txt.h"
Huh?

If you're trying to open the file for input, you use this bit here:
C++ Syntax (Toggle Plain Text)
  1. ifstream inputFile;

Open the file and read it?
Last edited by Dave Sinkula; Nov 4th, 2009 at 9:29 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 4th, 2009
0
Re: cannot open file, no such file or directory
how do I print the file?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gem74 is offline Offline
18 posts
since Oct 2009
Nov 5th, 2009
0
Re: cannot open file, no such file or directory
Click to Expand / Collapse  Quote originally posted by Gem74 ...
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.
Last edited by VernonDozier; Nov 5th, 2009 at 1:18 am.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 5th, 2009
0
Re: cannot open file, no such file or directory
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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gem74 is offline Offline
18 posts
since Oct 2009
Nov 5th, 2009
0
Re: cannot open file, no such file or directory
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.
Last edited by Fbody; Nov 5th, 2009 at 11:44 am.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: (Beginner) how to return char variable?
Next Thread in C++ Forum Timeline: Association





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC