G
Gary Wessle
Hi
I am writing a code to open a space delimited data file, return the
number of rows and columns, as well as return the nth column where n
is user define number.
I put all the cells in a loooong vector and loop in an incremental way
to select the column of choice, here is the code which works but gives
a warning
****************************************************************
g++ -Wall -g -c -o useful.o useful.cpp
useful.cpp: In member function 'void file_info::set_col(int)':
useful.cpp:54: warning: comparison between signed and unsigned integer expressions
****************************************************************
**************** useful.h ****************
#ifndef USEFUL_H
#define USEFUL_H
#include <vector>
using std::vector;
class file_info
{
int nRows, nCol;
string file_name;
vector<string> all_cols;
vector<string> col;
void stock_take();
void set_col(int);
public:
file_info(string const& fileName);
~file_info();
/** returns a pair of values, the first is the
* number of rows in a space delimited data file
* and the second is the number of columns.
**/
pair<int,int> get_counts();
/** takes a number n and returns a vector
* containing the nth column of the data file
**/
vector<string> get_col(int);
};
#endif
**************** useful.cpp ****************
#include <utility>
using std:air;
using std::make_pair;
#include <string>
using std::string;
#include <fstream>
using std::ifstream;
#include <sstream>
using std::stringstream;
#include "useful.h"
file_info::file_info( string const& fileName )
: file_name( fileName ){
nCol = 0;
nRows = 1;
stock_take();
}
void file_info::stock_take() {
ifstream in(file_name.c_str());
string line;
getline(in, line);
stringstream input( line.c_str() );
string word;
while(input >> word) {
nCol++; // init'd by constructor
all_cols.push_back(word);
}
while (getline(in, line)){
nRows++; // init'd by constructor
stringstream input( line.c_str() );
string word;
while(input >> word)
all_cols.push_back(word);
}
}
file_info::~file_info() {}
pair<int,int> file_info::get_counts()
{
return make_pair(nRows, nCol);
}
void file_info::set_col(int x){
for(int i = (x-1); i < all_cols.size(); i = (i+nCol) ) //<<LINE 54
col.push_back(all_cols);
}
vector<string> file_info::get_col(int y){
set_col(y);
return col;
}
thanks
I am writing a code to open a space delimited data file, return the
number of rows and columns, as well as return the nth column where n
is user define number.
I put all the cells in a loooong vector and loop in an incremental way
to select the column of choice, here is the code which works but gives
a warning
****************************************************************
g++ -Wall -g -c -o useful.o useful.cpp
useful.cpp: In member function 'void file_info::set_col(int)':
useful.cpp:54: warning: comparison between signed and unsigned integer expressions
****************************************************************
**************** useful.h ****************
#ifndef USEFUL_H
#define USEFUL_H
#include <vector>
using std::vector;
class file_info
{
int nRows, nCol;
string file_name;
vector<string> all_cols;
vector<string> col;
void stock_take();
void set_col(int);
public:
file_info(string const& fileName);
~file_info();
/** returns a pair of values, the first is the
* number of rows in a space delimited data file
* and the second is the number of columns.
**/
pair<int,int> get_counts();
/** takes a number n and returns a vector
* containing the nth column of the data file
**/
vector<string> get_col(int);
};
#endif
**************** useful.cpp ****************
#include <utility>
using std:air;
using std::make_pair;
#include <string>
using std::string;
#include <fstream>
using std::ifstream;
#include <sstream>
using std::stringstream;
#include "useful.h"
file_info::file_info( string const& fileName )
: file_name( fileName ){
nCol = 0;
nRows = 1;
stock_take();
}
void file_info::stock_take() {
ifstream in(file_name.c_str());
string line;
getline(in, line);
stringstream input( line.c_str() );
string word;
while(input >> word) {
nCol++; // init'd by constructor
all_cols.push_back(word);
}
while (getline(in, line)){
nRows++; // init'd by constructor
stringstream input( line.c_str() );
string word;
while(input >> word)
all_cols.push_back(word);
}
}
file_info::~file_info() {}
pair<int,int> file_info::get_counts()
{
return make_pair(nRows, nCol);
}
void file_info::set_col(int x){
for(int i = (x-1); i < all_cols.size(); i = (i+nCol) ) //<<LINE 54
col.push_back(all_cols);
}
vector<string> file_info::get_col(int y){
set_col(y);
return col;
}
thanks