C++ :: Open File And Read In Rows To String Vector And Return Vector
Jun 7, 2012
I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,
N,N'-dimethylethylenediamine.mol
This is the function that opens the file :
Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );
[Code] ....
The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.
View 9 Replies
ADVERTISEMENT
Jul 10, 2013
The error is unclear but suggests Its received a bad pointer from another heap. It references dbgheap.c line 1322 and assertion failure
I have two string vector functions the first is called from the main function, the second is called from the first.
Their purpose is to receive a string of text and numbers in a semi-specific format, which the main body of the code reads from a text file, and delaminates the data as to return the first variable in the string as the variable name and the second as the variable value. Along the way it filters out a lot of the unwanted whitespace and punctuation.
E.g "{ VariableNameA 123 }" would be returned as "VariableNameA" And "123"
The code works perfectly for most of the lines in the text file but fails on one particular line where the first variable is 25 characters long. Basically it works for anything 22 characters or less. There are never more than 4 elements in the vector and each element is never intended to be longer than 25 characters.
It fails trying to return from the second split function to the first split function.
Is there a limit to the size of each vector element? I'm struggling to find a way round this without having to rewrite the whole thing.
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
[Code] ....
View 4 Replies
View Related
Feb 20, 2015
I'm trying to move a dictionary into a group of bite-sized files based off of the length of the strings in each file (I'm ignoring strings of length 1 for obvious reasons). Since I don't know what the longest word is (and I'm not going to look for it), and I don't want redundant files, I decided to use a vector of output file streams that I would put all the words into, but I can't get the file to open.
I took a look at it and found the failbit is 1, but the badbit is 0, so apparently it's a logic error on opening the stream [URL] .... I looked online and decided to try using pointers, but that didn't work either, so now I'm asking what the problem might be, because I can't think of any reason why it isn't working.
void createDictionary() {
//declare variables
string dictionaryName;//name of dictionary file
ifstream dictionaryIn;//file of original dictionary
vector<shared_ptr<ofstream>> dictionaryOut;//files where dictionary will be put based off of word length
string word;//input from the dictionary file
vector<int> lengths;//vector containing available lengths and their locations
unsigned long long counter = 0;
[Code] ....
View 2 Replies
View Related
Mar 24, 2012
Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>
[Code]....
Why is it reading in nothing for the arrays, and also making the size of the total thing the total number of numbers? It should have a size of 2, not 5.
View 14 Replies
View Related
Nov 7, 2014
void Equipa::alocaDirector (istream &isV){
string _name;
string _age;
string _country;
while (!isV.eof()){
getline(isV, _name);
[Code] .....
I want this function to "copy" the first 3 txt lines to one position in the vector. Example
Directors.txt:
John
45
England
Mark
53
Belgium
With a print function this would be something like:
Name: John
Age: 45
Country: England
and the same for the second guy.
But something really weird is happening. I have this alloc function and then I have in the main() something to print it:
Equipa me1;
ifstream myFile;
string output;
myFile.open("/home/luiscosta/git/AEDA/AEDAP1/DB/MemEqui/Diretores.txt");
//if (myFile.fail())
while (!myFile.eof()) {
getline(myFile, output);
[Code] ....
which calls a new print function:
string MembroEquipa::imprime() const {
stringstream ss;
//ss << nome << ", " << idade << ", " << pais;
[Code] ...
And then I run it, this happens:
Name: 45
Age: 0
Country: Angel Lopez
Name: 50
Age: 0
Country: Armindo Pinto
Name: 43
Age: 0
Country:
Somehow my print function is skipping one line and I can't print anything.
View 6 Replies
View Related
Jul 7, 2014
Input file:
Course Of Sales.csv
Time,Price ($),Volume,Value ($),Condition
10/10/2013 04:57:27 PM,5.81,5000,29050.00,LT XT
10/10/2013 04:48:05 PM,5.81,62728,364449.68,SX XT
10/10/2013 04:10:33 PM,.00,0,.00,
How to read the csv file with my own vector class. I already have date.h,date.cpp,time.h,time.cpp
How to do the maintest and how to create the vector class
I need to show Date, highest price and Start time(s) of the highest share price
View 3 Replies
View Related
Nov 3, 2013
My project's goal is to read some numbers from input file,then create a grid.my input file is:
2 3
3 4 5
1 2 4
first line is rows and colums. second and third line the grid location values.My code is
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include<vector>
using namespace std;
[code].....
View 7 Replies
View Related
Aug 12, 2012
I'm trying to get my program to read a series of comma delimited values from a file into a vector. However, I am unsure how to actually go about doing this. I've posted my best guess below but it's really just a stab in the dark and I always receive a compiler error.
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
vector <string> v_input;
int main() {
ofstream fout ("Insert File Path Here.txt");
fout << "4, 8, 15, 16, 23, 42";
[Code] ....
View 3 Replies
View Related
Dec 10, 2013
I need a large string vector for file content manipulation.
v1.max_size() gives me 153 391 689.
But if I make a test by looping a vector pushing back strings it crashes around 16 - 26 000 000 though I still have a lot of ram (1GB left).
How come and why isn't vector size limited by ram instead?
View 11 Replies
View Related
Jul 30, 2014
Ok here is the code
//assuming that all the directives have been included here
void ptrPointer(vector<string>* const p, int n);
int main() {
vector<string> inventory;
vector.push_back("sword");
vector.push_back("knife");
[Code] .....
Well...my question is how come the last statement or cout statement to be accurate won't work and display? Since we can do something like this below?
string text = "some text";
string *p = &text;
cout << p;
I passed the address of the vector to the function. And then display the address. It is a pretty common operation. But it just won't work. But then we do something simple similar to that of the vector. But it works.
View 6 Replies
View Related
Sep 6, 2014
I am trying to pop the element on a vector off and return its value at the same time
vector.push_back();
Unfortunately that code only removes that element from the vector it does not return it
Is the only way to get the element and destroy it is to do this?
vector.back();
vector.pop_back();
View 1 Replies
View Related
Oct 6, 2014
My function has the following prototype:
std::vector<double> calculate_mag_response(double start_freq, double end_freq,
int N, std::vector<double> num, std::vector<double> den
The function should return a vector size N of magnitude responses in decibels of the transfer function defined by numerator and denominator vectors num and den. To adequately test, you should drive this with more transfer functions.
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ifstream>
using namespace std;
int main(int argc,char *argv[]) {
vector<double> myNum, myDen, results;
[Code] ....
View 2 Replies
View Related
Oct 19, 2014
So I have a Binary Tree and I need to return a vector of integers (the nodes) of the heaviest path in the tree.
First, is it possible to do in C? Because I think a vector is an ADT in C++.
I've started writing something recursive, which worked for a balanced tree of height 1, and failed for longer height.
This is what I've written - [URL] ....
View 6 Replies
View Related
Oct 12, 2014
Okay, so for an assignment I need to write a function called find() that returns a reference to a vector. So I have vector <int> & find(string & key); If I do this, I get the obvious warning warning: reference to local variable 'lineNum' returned [enabled by default].
If I do vector<int> & find(string & key) const; I get a huge error that starts out like
In member function 'std::vector<int>& index_table::find(std::string&) const':
indextable.cpp:74:30: error: no match for 'operator='
Am I using the const identifier incorrectly?
View 5 Replies
View Related
Sep 17, 2014
How to pass my array to the function and return it as a sorted vector. I'm kind of lost with the functions part.
Problem: Write a program that performs a sorting routine as discussed in class. Create an array of 10 integers (e.g., 63, 42, 27, 111, 55, 14, 66, 9, 75, 87). Pass this array to a separate function that sorts the integers and returns a vector containing the sorted integers. Use either the SELECTIONSORT algorithm or the INSERTIONSORT.
Code so far:
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
[Code]...
View 2 Replies
View Related
Oct 29, 2014
Code:
cout<<"Enter Filename for input e.g(inp1.txt .... inp10.txt):"<<flush;
cin>>filename;
ifstream inpfile;
inpfile.open(filename,ios::in);
if(inpfile.is_open())
[Code] .....
View 8 Replies
View Related
Oct 5, 2013
I need to read a text file which has various lines containing integers. I need to write those integers separately in a vector. Example, the first line of the text file contains 3 9 8 7 6 so vector[4]=3, vector[3]=9, vector[2]=8 and so on. Next read the second line 4 1 2 3 4 5 and write to another vector vector[5]=4, vector[4]=1...
I tried the code below but it will write from the second line, the whole line in one vector index.
int str; // Temp string to
cout << "Read from a file!" << endl;
ifstream fin("functions.txt"); // Open it up!
string line;
// read line count from file; assuming it's the first line
getline( fin, line );
[Code]...
View 1 Replies
View Related
Feb 9, 2015
How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.
here is my code:
#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)
[Code].....
View 3 Replies
View Related
May 13, 2013
I need to create a class vector as a template and define operations on vectors.
And this is what I made.
#include<iostream>
using namespace std;
template<class T>
[Code].....
View 2 Replies
View Related
Mar 26, 2013
Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>
How do I make a function that creates vector of vector of every different integers?
<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>
and so on...
View 2 Replies
View Related
Mar 6, 2014
Consider I have a vector of strings and then I use an istringstream to read each word of each element in the vector, why do I nescessarily use an istringstream?
This is the code that does what I just described (I think)..
Code:
for(auto &elem : svec) {
istringstream strm(elem);
while (strm >> word)
//Magic
}
What would be the equivalent of using something else than an istringstream in this scenario and how does the istringstream work?
View 7 Replies
View Related
Jul 5, 2013
I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structures according to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.
For instance, here is a test program that successfully finds the maximum according to just one member of the structure:
Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
[Code] ....
And the output was this, as expected:
Maximum element S.a of vector<S> vec is at: 9
[I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6 vec[6].a = 6
[I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7 vec[7].a = 7
[I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8 vec[8].a = 8
[I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9 vec[9].a = 9
However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.
This was the description of the std::find function that I found: find - C++ Reference
Code:
template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);
I could also find another function called std::find_if, but this only accepts a unary predicate like this: find_if - C++ Reference
Code:
template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:
Code:
int x=7;
std::vector<S>::iterator result;
result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;
Is there another std function that I can use to make search and find an element according to just one member of myStruct?
Or perhaps there is a clever way to pass two arguments to the unary predicate.
View 4 Replies
View Related
May 29, 2014
I wrote a program that reads a list from a file and stores it in a string type vector. Now, I want the user to input a word so that the program can search the vector to see if that word already exists. I have used every possible way of reading input from the console and storing it in order to compare with the vector but it never results in a match. When I print the input string and the vector string they are exactly the same thing (or at least print to the console as if they were). I've tried using getline; using cin direct to a string var; using cin to a char array and then casting to string using string str(arr); I even added a newline at the end just in case and STILL I cannot get a match.
vector <string> currentSet; //read a list in from a file and has 9 items in it
cin.ignore();
string line;
getline(cin, line);
if(line == vector[0]){//if printed to console line is HEAT and vector[0] is HEAT
cout<<"match"<<endl;
}
View 3 Replies
View Related
Nov 8, 2013
Reading a .dat file, i'm unable to open the file. This program is for a Air Quality index detector, the AQI machine records the particle concentration every minute and saves it into new data file for each day. So I need to make this program run every minute and convert the concentration in to the AQI readings.
The filename is of the format "ES642_2013-11-09.dat", where ES642 is the software name of the machine, and rest is the year, month and day. The code here is just for the file reading section:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
[Code] .....
View 4 Replies
View Related
Jan 30, 2014
So I've been turning my programs into classes and I run into errors.
So this program is supposed to allow the user to open the file and either read or write to it.
I've omitted the read part from it as I want to attempt that on my own .
I get these compile errors:
fileclass.cpp:13: error: ISO C++ forbids declaration of ‘choice’ with no type
fileclass.cpp:21: error: ISO C++ forbids declaration of ‘choice’ with no type
fileclass.cpp: In member function ‘int file::choice()’:
[Code] .....
View 10 Replies
View Related
Oct 15, 2013
So the program reads contents from a text file into a vector and then the user enters in a string that they want to search for. The program iterates through the vector to find the string and then saves that line to another vector to display later(incase there is more then 1 instance of the string found).
Here is what I have atm:
void doSearch(vector<string> &phonelist, string searcher, vector<string> &holdNumbers)
{
int i = 0;
string value;
[Code].....
I just get an R6010 error -abort() has been called.
View 2 Replies
View Related