VS2017 c++ ifstream error - c++

I've just installed Visual Studio to start writing C++ there, I spent the last year learning in CodeBlocks and everything was going fine. But when I got to trying out the ifstream function, it shoots out an error and I've looked around and can't seem to find an answer anywhere. Could you guys please help?
Here's the very simple code that I wrote:
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n, m[100];
ifstream f1("in.txt");
f1 >> n;
for (int i; i < n; i++)
{
f1 >> m[i];
cout << m[i] << endl;
}
f1.close();
system("pause");
return 0;
}
This is the in.txt path
C:\Users\Administratar\source\repos\ConsApp1\in.txt
And this is what is inside in.txt
4
1
2
3
4
And the error says:
"Error: Unable to open file
C:\Users\Administratar\source\repos\ConsApp1\Debug\ConsApp1.obj
Error code = 0x80070002"

I had a similar issue just now, actually, but the problem wasn't in ifstream or the include at all. It was in the file included previous of it.
Just check stdafx.h file and make sure there's no issues or errors in it and it shall work perfectly fine.

Related

Segmentation Fault with ifstream >> operator [duplicate]

This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Fixing Segmentation faults in C++
(6 answers)
Closed 2 years ago.
I am having trouble with the >> operator of iostream when reading a .txt file. The error is happening at the bottom but here is some code to give some context.
MAIN - I cannot change this file for assignment purposes.
#include <iostream>
#include <fstream>
#include "graphm.h"
using namespace std;
int main() {
ifstream infile1("data31.txt");
if (!infile1) {
cout << "File could not be opened." << endl;
return 1;
}
GraphM G;
G.buildGraph(infile1);
...
}
Below is the method where the error is, Build Graph in graphm.cpp.
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
GraphM::GraphM()
{
size = 0;
for (int i = 1; i <= MAXNODES; i++)
{
for(int j = 1; j <= MAXNODES; j++)
{
C[i][j] = INT_MAX;
T[i][j].dist = INT_MAX;
T[i][j].path = 0;
T[i][j].visited = false;
}
}
}
void GraphM::buildGraph(ifstream& infile)
{
int s;
infile >> s;
size = s;
//I have also tried just infile >> size; and that doesn't work either.
...
}
I get an error that says "EXC_BAD_ACCESS (code=1, address=0x7fffffe7)" on the infile >> s; line.
I tried running on a linux server and it compiled with no error but when running at this point it said "Segmentation Fault".
I am using CLion on a mac. I don't believe it has to do with my code but maybe the IDE... however the error also happened in linux.
The file reads in and does not hit the "Cannot find file" point, but maybe it doesnt read in correctly? In CLion, data31.txt it is inside the debug folder as well as in the same directory as the main.
I also tried the line below just to give it a shot for the first line in main and it lead to the same outcome.
ifstream infile1("/Users/stlp/Documents/CSS343/ass3/assignment3_supportingFiles/data31.txt");
EDIT - showed the entire constructor. Also to be clear, the code runs until the last line listed.

Ifstream is opening the file, but doesn't output the lines inside the file

Im new to c++ and i was trying to open a ".txt" file using ifstream. the file im using is called "ola.txt" which literally just contains two lines of text without punctuation just plain and simple text. The code that i wrote is this
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int x;
string line;
vector<int> vect;
ifstream inFile("C:\\Users\\ruial\\Desktop\\ola.txt");
inFile.open("C:\\Users\\ruial\\Desktop\\ola.txt");
if (inFile.is_open()) {
while (getline(inFile, line))
{
cout << line << '\n';
}
inFile.close();
}
else {
cout << "Unable to open file";
exit(1); // terminate with error
}
return 0;
}
The path to the file that i wrote is correct such that the file opens, but when the program runs it doesn´t cout the lines that i wrote on the txt file to the cmd, i dont know if this is somewhat important but im coding in visual studio 2019.
I can't seem to find the answer to this problem anywhere in the internet and to be honest i think im doing it right, any help would be much appreciated,thanks in advance.
You are trying to open the inFile twice. First time during inFile construction, ifstream inFile("C:\\Users\\ruial\\Desktop\\ola.txt"), second time you try to open it again with inFile.open("C:\\Users\\ruial\\Desktop\\ola.txt"), when it's already open, which is erroneous, and flags the stream as no longer good.
3 possible fixes:
Remove inFile.open("C:\\Users\\ruial\\Desktop\\ola.txt")
Use default constructor, without specifying the file name
inFile.close() before you open it again (obviously, not the nicest fix).

Program runs on one computer but doesn't work on another

I have tried running this code in my school and it works like a charm but when I got home, this piece of code suddenly gets error. Can somebody enlighten me about what happened to my code? Is there something that I should correct or add in my code? edited : There were build errors in the code.
#include <iostream>
#include <fstream>
#include <string>
ofstream fileObject;
using namespace std;
int main() {
string username[5];
cout << "Enter username: ";
for (int i = 0; i < 5; i++) {
getline(cin, username[i]);
}
fileObject.open("open.txt", ios::app);
for (int x = 0; x < 5; x++) {
fileObject << username[x] << endl;
}
fileObject.close();
return 0;
}
ofstream fileObject; is above using namespace std, so it's not recongnized as a type.
Either move it below using namespace std, or use std scope, std::ofstream fileObject.
The second option is a better one, read Why is “using namespace std;” considered bad practice?
I don't see how this can run in any C++ compiler, unless you include headers that already recognize std namespace.

CodeBlocks on Mac are not reading file

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int A;
ifstream file("file.txt"); // there is a single "8" in the file
file >> A;
cout << A; // I get 0 always
return 0;
}
While I'm not new to CodeBlocks, I'm new to CodeBlocks on Mac. I have changed the "Execution working directory" and it still does not work, please help.
Don't change the execution working directory.. When you're reading from file, try writing the full directory where is that file, for example:
// this is your file.txt location
ifstream file("C:\\Desktop\\file.txt"); // this is for Windows
and then run a program.
If it still doesn't work, try watching this tutorial: https://www.youtube.com/watch?v=De6trY8FRYY

C++ code runs on Mac with multithreading, file will not open in Ubuntu

Running C++ code using Repast HPC 2.1 on an Ubuntu 14.04 VirtualBox VM. This code has been proven to work when run in XCode on a Mac laptop. I'm currently trying to make the existing code run in Ubuntu. Make runs successfully on the code in Ubuntu, but when trying to call mpirun -n 4, I receive the following error, indicating that the file will not open properly:
file open fail /home/repasthpc/Desktop/hpcmodel/angiogenesis-osteogenesis-simulator/concFiles/c100forC.txt
file open fail /home/repasthpc/Desktop/hpcmodel/angiogenesis-osteogenesis-simulator/concFiles/c100forC.txt
file open fail /home/repasthpc/Desktop/hpcmodel/angiogenesis-osteogenesis-simulator/concFiles/c100forC.txt
file open fail /home/repasthpc/Desktop/hpcmodel/angiogenesis-osteogenesis-simulator/concFiles/c100forC.txt
This is using multiple threads (4), which I've heard can be an issue when opening files in Ubuntu, but I'm not sure how to fix it if this is the problem.
The error is generated by the file.fail() call in this method:
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
#include "repast_hpc/Random.h"
#include "SolubleMap.h"
#include "BoneModel.h"
using namespace std;
void SolubleMap::releaseVEGF(std::string filename){
ifstream file;
file.open(filename.c_str());
int row=time; //time
int column=ydim; // location
if(file.fail()){
cerr << "file open fail" << endl;
}
else{
this->VEGFConcentration = new double*[row]; // memory allocated for elements of rows
for(int j = 0; j < row; j++){
this->VEGFConcentration[j] = new double[column]; // memory allocated for elements of columns
for(int i = 0; i < column; i++){
if (!(file >> this->VEGFConcentration[j][i])){
std::cerr << "error while reading file";
break;
}
}
if (!file) break;
}
}
file.close();
}
When the full filepath is passed as a string to file.open() in line 2 of the method, still produces the same error.
Is there some reference or library I am missing that is provided by XCode?
Thanks!