So, the binary file literally stores just one struct array
#include <string>
#include <fstream>
using namespace std;
struct Card{
char symbol;
int color;
int id;
};
int main(){
int numcards = 12;
Card *cardArray[numcards];
ifstream arch;
arch.open("casoPruebaParte1.dat", ios::binary);
if(!arch.is_open()){
cout<<"Error al abrir el archivo\n";
exit(1);
}
numcards = 12;
arch.read((char*)&cardArray, numcards*sizeof(Card));
for(int i=0; i<numcards; i++ ){
cout << cardArray[i]->id << endl;
}
arch.close();
return 0;
}
the error is: segmentation fault (core dumped) and debugging shows this:
error in line 30 ( cout << cardArray[i]->id << endl;)
Related
I am doing an assignment (.csv-parser) for uni. While trying to run the code after compilation it returns a SegFault 11.
This is my code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
int main (int argc, char** argv) {
string line;
string buffer;
vector<vector<string> > database;
vector<string> dataset;
//bool start = true;
ifstream denkmaeler;
denkmaeler.open(argv[1]);
ifstream denkmal;
denkmal.open(argv[1]);
int semic = 1;
//int semic2 = 1;
int zaehler = 0;
if (denkmal.is_open()){
(getline(denkmal, buffer));
for (int i = 0; i < buffer.length(); i++ ){
if(buffer[i] == ';'){
semic++;}
}
}
denkmal.close();
if(denkmaeler.is_open()) {
//if (counter < 1) {
while (getline(denkmaeler, buffer));
if (line.back() == *argv[2]) {
line += argv[2];
stringstream ss(line);
while (getline(ss, line, *argv[2])) {
dataset.push_back(line);
database.push_back(dataset);
dataset.clear();
}
}
}
for (int x=0, y=semic; x < semic; y=database.size(), x++, y++){
if (x > semic){
x=0;
cout << '\n' << "-------------------------------" << '\n' << endl;
}
if (database[y][0].length() == 0){
database[y][0] = "not available";
}
cout << database[x][0] << " : " << database[y][0] << endl;
}
}
If someone would be able to point out my mistake I would be very thankful. I read some posts pointing out that the problem could be an array but I am not sure how that could be.
For segmentation error, it is best to use a debugging tool which can exactly show you where the error is (which line)!
I am using gdb, i recommend you to google it
I want to "quick sort" the array that I have read from a .txt file; but the code is generating an error. It says "Expected primary expression before "input" ".
Any help is appreciated. This is the code. Thank you.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
void quickSort(int tabele[], int majtas, int djathtas){
if(majtas<djathtas){
int vendPivot = ndarja(tabele, majtas, djathtas);
/* rendit sipas rradhes elementet para dhe pas vendPivot */
quickSort(tabele, majtas,vendPivot-1);
quickSort(tabele,vendPivot+1, djathtas);
}
}
vector<int> lexoTabele(){
vector<int> data;
ifstream inFile;
inFile.open("detyraekursit.txt");
if (!inFile.is_open()) {
cout << "Unable to open file";
exit(1); // terminate with error
}
int x;
cout<<"Vektori INPUT para renditjes ";
while (inFile >> x) {
data.push_back(x);
}
inFile.close();
return data;
}
int main() {
vector<int> input = lexoTabele();
int x,y;
for(int i = 0;i < input.size();i++) {
cout<< i << ", " << input[i];
}
quickSort(vector<int>input,x,y);
return 0;
}
This is my first attempt at getting a bunch of values from a separate text document. I have a separate text file named P.txt with the following values 13 49
16 2
4 0
90 60
40 20
60 -100
7 1
5 9
3 3
15 -22
11 7
12 3
I have never tried to get data from an outside file to put into an array and while working I keep getting errors on line 15 which is:
read_data(int nums[], int size);
Visual studio says that it is expecting a ")" after int and that the read_data function does not take 0 arguments. Can anyone explain these errors to me and help guide me in the right direction? Thanks!
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void read_data(int nums[], int size);
int main()
{
const int size = 24;
ifstream dataIn;
double lab[size];
cout << "The numbers on file are:\n ";
read_data(int nums[], int size);
system("PAUSE");
return 0;
}
void read_data(int nums[], int size)
{
ifstream dataIn;
dataIn.open("P.txt");
if( dataIn.fail() )
{
cout << "File does not exist." << endl;
exit(1);
}
int count;
for ( count = 0; count < size; count++ )
{
dataIn >> nums[count];
}
dataIn.close(); }
The problem is in the line read_data(int nums[],int size);
You cannot declare a variable in a function call.
Also, you cannot use system("pause") without stdlib.h or cstdlib
Use this instead :
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
void read_data(int nums[], int size);
int main()
{
const int size = 24;
ifstream dataIn;
double lab[size];
int nums[size];
cout << "The numbers on file are:\n ";
read_data(nums, size);
system("PAUSE");
return 0;
}
void read_data(int nums[], int size)
{
ifstream dataIn;
dataIn.open("P.txt");
if( dataIn.fail() )
{
cout << "File does not exist." << endl;
exit(1);
}
int count;
for ( count = 0; count < size; count++ )
{
dataIn >> nums[count];
}
dataIn.close(); }
I'd like to know how to read a file using streams but also using them inside a function.
So far my code is;:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void burbuja(int[]);
void imprimeArreglo (int[],int);
void leeArchivo(string&);
int arreglo[10];
int i;
void burbuja (int a[])
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(a[i]>a[j])
{
int temp=a[i]; //swap
a[i]=a[j];
a[j]=temp;
}
}
}
}
void imprimeArreglo(int a[],int tam)
{
for(int i=0;i<tam;i++)
cout << a[i] << " ";
}
void leeArchivo(string& nombre)
{
string filename = nombre;
ifstream myfile(filename);
string line;
if (myfile.is_open()) {
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
}
int main()
{
string nombre = "arr.txt";
leeArchivo(nombre);
cin >> i ;
return 0;
}
I'd like to be able to call leeArchivo("arr.txt") from the main method.
With this I get the errors:
Error: bubble.cpp(37,14):'ifstream' is not a member of 'std'
Error: bubble.cpp(37,19):Statement missing ;
Error: bubble.cpp(39,20):Undefined symbol 'file'
Error: bubble.cpp(39,25):Could not find a match for 'std::getline(undefined,std::basic_string<char,std::string_char_traits<char>,std::allocator<char>>)'
What am I missing here? (I'm new to C++)
The file I'm trying to read has the following structure:
<number>
<number> <number> <number> ...
E.G.:
5
19 28 33 0 1
=========================================
EDIT:
I'm using Borland C++ 5.02
EDIT 2:
Updated code, using Geany Now error is: BUBBLE.cpp:38:25: error: no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::string&)'
That's particularly odd behavior with ifstream. Try this edit:
void leeArchivo(const string&);
void leeArchivo(const string& nombre)
{
ifstream file(nombre.c_str());
string line;
while(getline(file,line)) {
cout << line << endl;
}
}
int main()
{
leeArchivo("arr.txt");
return 0;
}
Also, use:
#include <cstdlib>
Instead of:
#include <stdlib.h>
I tried to create a program that uses an input file 'cardList.txt' that contains:
Schmidt, Helga
Alvarez, Ruben
Zowkowski, Aaron
Huang, Sun Lee
Einstein, Beverly
and I wanted to sort this alphabetically by last name.
main:
#include <iostream>
#include <fstream>
#include <string>
#include "insertsortFunct.h"
using namespace std;
int main(void){
ifstream inData;
ofstream outData;
const int listSize = 5;
char cardList[listSize][25];
instruct();
openFile(inData, outData);
buildList(cardList, inData);
inData.close();
sortList(cardList, listSize);
cout << endl << "Your list is sorted" << endl;
writeFile(cardList, outData);
outData.close();
return 0;
}
I defined these functions in a separate file:
#include "insertsortFunct.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void instruct(void){
cout << "The program insertSort.cpp reads the file, cardList.txt " <<
"into an array and" << endl;
cout << "sorts the array using the selection sort algorithm." << endl;
cout << "The sorted array is written to a file named cardList.srt." <<
endl;
}
void openFile(ifstream& inputFile, ofstream& outputFile){
inputFile.open("cardsList.txt");
if(!inputFile.is_open())
exit(1);
outputFile.open("cardsList.srt");
if(!outputFile.is_open())
exit(1);
}
void buildList(char (*array)[25], ifstream& inputFile){
for (int i = 0; i < 5; i++)
inputFile >> array[i];
}
void sortList(char (*list)[25], int length){
int firstOutOfOrder, location;
char temp[25];
for (firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder++){
location = firstOutOfOrder;
while ( location > 0 && list[location - 1] > list[location]){
temp[25] = list[location][25];
list[location][25] = list[location - 1][25];
list[location - 1][25] = temp[25];
location--;
}
}
}
void writeFile(char (*array)[25], ofstream& outputFile){
for (int i = 0; i < 5; i++)
outputFile << array[i];
}
However my program only prints the instruct(); statements and nothing else appears to happen. This program is supposed to a create a file cardList.srt with the sorted list and that does not appear in my directory after compiling.
Either inputFile.is_open() or outputFile.is_open() return false and hence exit() is called.