Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I can not get Visual Studio to read in a text file. Below is the code I have. The file opens perfectly in a Unix Environment, but it does not work when copied and pasted into Visual Studio. I am using fstream to open the file. Why the file is not being read?
When I run the program, it builds but I get no output. I have an output statement cout << "inf\n". So the loop is not even being reached, which is why I believe the file is not being read. Again, when I run the same code in a Unix environment the output statement does display and the values from the file are displayed ( via tree.insert(), tree.remove() ).
I tried the solution in this link. As it suggested, I changed my working directory to $(ProjectDir)\Debug and $(ProjectDir)\Release. Also, I moved my text file from the Resources folder to my Source Folder in the Solution Explorer. However, the file still was not being read.
I also updated my code to include cerr << "Error: " << strerror(errno); directly after fstream inf ("BTREE5_1.txt"). With this line of code the output I get is
Error: No such file or directory
Can someone please explain why? My text files are in the same folder as my code as explained above.
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "BTree.h"
using namespace std;
int main()
{
bool first = true;
BTree tree(6, 2);
int value;
char s[80], command;
ifstream inf("BTree5_1.txt");
cerr << "Error: " << strerror(errno);
inf.getline(s, 80);
while (inf >> command >> value)
{
cout << "inf\n";
if (command == 'i')
tree.insert(value);
else
{
if (first)
{
cout << "After all insertions.\n";
tree.print();
first = false;
} // if first
cout << "Deleting " << value << ". \n";
tree.remove(value);
tree.print();
// fgets(s, 80, stdin);
} // else deletion
} // while
system("PAUSE");
return 0;
} // main
The problem was that I copied and pasted my text files from the Unix Environment. To fix this I just placed the text files into my Directory from my C Drive.
ie>) C:\Users\s.proctor\Documents\Visual Studio 2015\Projects\ecs60\p2\p2\p2
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I didn't got any errors, but my C++ code is still not working. It's really simple:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string a;
ofstream fout("char.out");
ifstream fin("char.in");
fin >> a;
fout << a;
return 0;
}
char.in after running:
uiui
char.out after running:
Did I missed anything simple in my code?
P. S. : I got Norton Antivirus and my project folder is missed from AutoCheck.
in fact for reading and writing you should open and close file but you didn't close.
Also you have two files where you have done writing from one file and reading from another file, I wonder how you expect to get the correct output.
this is how it should be :
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string a;
ofstream fout("char.out");
// check if file is created
if(fout.is_open()){
// do writing in file
}
else
cout << "can not open file\n";
fout.close();
//-----------reading the file----------
// use the same file
ifstream fin("char.out");
if(fun.is_open()){
// do reading from file
std::cout << a << std::endl;
}
else
cout << "can not open file\n";
fin.close();
return 0;
}
And if you want to add a line of text to the end of the file, you must add:
ofstream fout("filename" , ios::app);
This question already has an answer here:
Can't open txt files in c++ program with Visual Studio 2019
(1 answer)
Closed 1 year ago.
I am new to C++ and I am trying to open and read from a file into a simple program that I have Made. Here it is:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream myFile("Otto.txt");
myFile.open("Otto.txt");
if (!myFile.is_open())
{
cout << "Sorry, an error has occured";
}
string read;
while (myFile >> read)
{
cout << read;
}
myFile.close();
return 0;
}
Here's my Otto.txt file:
Hello
I
Am
Otto!
I am using Visual Studio 2019. Any help would be appreciated.
Attempting to open a file on an ifstream object that is already associated with an open file will cause the (second) open operation to fail, which will cause the stream to enter a failed state.
In your code, you are opening the file twice, once in the constructor of myFile and once in the open function call.
The solution is to only open the file once, either in the constructor, or in an open function call, but not both.
This question already has answers here:
Can't open file for input C++
(1 answer)
Cannot open file with relative path? (C++ ifstream)
(1 answer)
Closed 2 years ago.
I have a .txt file containing a list of about 200 numbers with one on each line and I'm trying to parse it with C++ but I'm facing some issues -- here's my code:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
ifstream file;
file.open("input.txt");
// check for error
if(file.good())
cout << "opened!";
else
{
cerr << "issue" << endl;
exit(1);
}
while (file >> n)
{
cout << n;
}
file.close();
}
Basically I'm trying to input these elements into an integer array (separate from this) but I have trouble reading the .txt file for some reason as it always results in "issue". Also, when I try for example to output multiple numbers from this file
int a,b,c;
file >> a >> b >> c;
cout << a << b << c ;
it always results in (seemingly) random numbers being printed. I thought it was a problem with the location of my input file but after recreating a project and changing the locations of my files I still can't seem to fix this. How can I solve this issue?
Output from debug:
File opened...
File contents:
Output from .exe (run via double click from /project/debug):
File opened...
File contents:
line1
line2
etc. . .
Source code:
#include <iostream>
#include <fstream>
#include <regex>
#include <string>
#include <list>
using namespace std;
using namespace tr1;
int main()
{
string line;
list<string> dataList;
ifstream myFile("test_data.txt");
if (! myFile)
{
cout << "Error opening file. \n";
return 0;
}
else
{
cout << "File opened... \n";
while( getline(myFile, line) ) {
dataList.push_back(line);
}
}
cout << "\n\n File contents:";
list<string>::iterator Iterator;
for(Iterator = dataList.begin();
Iterator != dataList.end();
Iterator++)
{
cout << "\t" + *Iterator + "\n";
}
getchar();
return 1;
}
thank you for your help!
i now understand the problem, thank you. obviously, this also shows that this method of error handling for files is worthless. I have corrected that as well. Thanks again.
The way you've coded this line:
ifstream myFile("test_data.txt");
means that the code is looking for the file in the current working directory.
When you run outside the debugger that will be /project/debug (in your case), which is where the file presumably is.
When you run inside the debugger that will (probably) be \project, which won't contain the file.
You'll need to either have two copies of the file, hard code the full path to the file, or have some way of specifying the file at runtime.
You can also specify the working directory (where it will look for test_data.txt) in the Debug property page for your project in VC.
Your .exe is normally run from Debug/../ when started from Visual Studio. When you double-click on it, it runs in 'Debug/'.
Either move your test_data.txt, or do as most developers and create an output directory where your binaries and data are exported before run.
Here is a simple program to output to a text file:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
double myNumber = 42.5;
fstream outfile("test.txt", fstream::out);
outfile << "The answer is almost " << myNumber << endl;
outfile.close();
}
All that ends up being wrote to my text file is, "The answer is almost " and the data is not displayed at all. What am I doing wrong? or could it be a problem with Xcode since I am using that as an IDE.
I'm not sure what the problem is. Is it that it's never executed or that it's writing to the wrong path. To shed light on this try include unistd.h and insert this snippet.
char* s = getcwd(NULL, 256);
printf("im running and pwd is: %s\n", s);
Inside xcode hit CMD-SHIFT-R to open the console and see if it prints anything.
There is no problem with your code. It could be a problem with Xcode.