cannot open file, no such file or directory

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 11
Reputation: Gem74 is an unknown quantity at this point 
Solved Threads: 0
Gem74 Gem74 is offline Offline
Newbie Poster

cannot open file, no such file or directory

 
0
  #1
23 Days Ago
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();
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #2
23 Days Ago
Originally Posted by Gem74 View Post
I saved "input_data.txt" in the header files, see below.

  1. #include "input_data.txt.h"
Huh?

If you're trying to open the file for input, you use this bit here:
  1. ifstream inputFile;

Open the file and read it?
Last edited by Dave Sinkula; 23 Days Ago at 9:29 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 11
Reputation: Gem74 is an unknown quantity at this point 
Solved Threads: 0
Gem74 Gem74 is offline Offline
Newbie Poster
 
0
  #3
23 Days Ago
how do I print the file?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #4
23 Days Ago
Originally Posted by Gem74 View Post
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; 23 Days Ago at 1:18 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 11
Reputation: Gem74 is an unknown quantity at this point 
Solved Threads: 0
Gem74 Gem74 is offline Offline
Newbie Poster
 
0
  #5
22 Days Ago
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;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 44
Reputation: Fbody is an unknown quantity at this point 
Solved Threads: 5
Fbody Fbody is offline Offline
Light Poster
 
0
  #6
22 Days Ago
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; 22 Days Ago at 11:44 am.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC