| | |
cannot open file, no such file or directory
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
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();
}
#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();
}
0
#2 23 Days Ago
•
•
•
•
I saved "input_data.txt" in the header files, see below.
C++ Syntax (Toggle Plain Text)
#include "input_data.txt.h"
If you're trying to open the file for input, you use this bit here:
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
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;
}
#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;
}
•
•
Join Date: Oct 2009
Posts: 44
Reputation:
Solved Threads: 5
0
#6 22 Days Ago
You use
To read from a file into your program, you need an INPUT file stream, so you want to use
Change
Also, you are attempting to create a string object called "name".
You will need
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.
![]() |
Similar Threads
- ImportError: ld.so.1: python: fatal: libclntsh.so.9.0: open failed: No such file or d (Python)
- "failed to open stream: No such file or directory" on local file (PHP)
- fatal error C1083: Cannot open include file: (C++)
- open file from root directory (C#)
- Unable to open input file with fstream (C++)
- Open or Save file dialogues? (Python)
- "Cannot Open Include File" MS Visual C++ error (C++)
- Put file in protected directory? (Networking Hardware Configuration)
- Error message when trying to open an external file in C++ program (C++)
Other Threads in the C++ Forum
- Previous Thread: (Beginner) how to return char variable?
- Next Thread: Association
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






