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.
Related
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
fstream file, save_file;
vector<string> words;
for (int i = 0; i < 1000; i++)
file >> words[i];
cout << words[0];
return 0;
}
I want to save these words in a vector, but I can't. I have a message: zad1.exe is already runing! Please close it first to compile successfully! I don;t know why.
Here is my solution:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main()
{
std::fstream file("test.txt"), save_file;
std::vector<std::string> words;
std::string line;
for (int i = 0; i < 1000; i++){
file >> line;
words.push_back(line);
}
std::cout << words[0];
return 0;
}
Instead of test.txt write your file path
On some operating systems you cannot write to a file if that file is a running program. That is what the error message is telling you. You are creating a program called zad1.exe and it is currently running so you cannot create a new version of zad1.exe until you stop the version that is already running.
Are you working on Windows? If so then use the task manager to kill any versions of zad1.exe that you can see.
Plus you have many problems with the code as pointed out in the comments above. But the first task is to kill those running programs.
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.
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.
This program is supposed to read a .txt file into an array, I do not know if it has something to do with the file path or the code in general. The X's are just a placeholder for my username. I know I can put the files in the project file, but I need to pull it from the computer. Any help would be appreciated!
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int main()
{
ifstream StuAns;
char stuAns[20];
StuAns.open("C:\\Users\\XXXXXXX\\Desktop\\StudentAnswers.txt");
if (!StuAns)
cout << "ERORR: File did not open\n";
while (int count = 0 < 20 && StuAns >> stuAns[20])
count++;
StuAns.close();
for (int i = 0; i < 20; i++)
cout << stuAns[i];
return 0;
}
If StuAns.open() fails, you are not stopping your program, you are continuing on and trying to read from an unopened file and output unread data.
Also, open() doesn't tell you WHY it failed. If you need that info, you will have to use the Win32 API CreateFile() function directly, then you can query GetLastError() if CreateFile() fails.
That being said, there are several bugs in your code:
StuAns[] contains uninitialized data, which is what you end up seeing output in the final for loop.
reading StuAns >> stuAns[20] goes out of bounds of the array. Valid indexes are 0..19 only. You are trashing memory (if the file were opened successfully).
your while loop is coded wrong.
you need to use count instead of 20 in the final for loop.
Try this:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ifstream StuAns;
char stuAns[20] = {};
int count = 0;
StuAns.open("C:\\Users\\XXXXXXX\\Desktop\\StudentAnswers.txt");
if (!StuAns.is_open())
{
cerr << "ERROR: File did not open\n";
return -1;
}
while (count < 20 && StuAns >> stuAns[count])
count++;
StuAns.close();
for (int i = 0; i < count; ++i)
cout << stuAns[i] << "\n";
return 0;
}
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!