This question already has answers here:
Closed 6 years ago.
I have a csv file which contains many rows of data. my function passes lineNum as an argument. So when a user enters 4 as lineNum I want to read the 4th line in the csv file.
I thought a good way to go through with this would be to look for \n 's and count them,stop when the count is lineNum-1, and then proceed to read the next line.
I think this is decent way of doing this, but I am thoroughly confused with the implementation. would love some help
Here is my code
void ReadCsv( int lineNum){
ifstream inFile ("/media/LOGGING/darsv1.csv");
string line;
string dataArray[226900];
int i = 0;
int endofline =0;
int a, b, c, d, e;
while (getline (inFile, line)) {
//printf(line.c_str());
istringstream linestream(line);
string item ="";
int itemnum = 0;
if (lineNum==1) {
printf(" line number is 1. ");
while (getline (linestream, item, ',')) {
itemnum++;
dataArray[i]=item;
i++;
}
}
else {
while (getline (linestream, item,'\n')) {
endofline=endofline+1;
cout<<" went through line number "<<endofline<<" ";
printf(" inside inner while, looking for line. ");
if (endofline == lineNum-1) {
printf(" found the correct line. ");
while (getline (linestream, item, ',')) {
itemnum++;
dataArray[i]=item;
i++;
printf(" found the correct data in the line. ");
}
}
}printf(" out of inner while. ");
}printf(" out of outer if. ");
}
printf(" out of all while loops. ");
}
If you just need to read a certain line in a CSV and then from that line read the comma separated items then this might help. I agree with @sanjaya-r that you should keep it simple.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main (int argc, const char * argv[]) {
string line, csvItem;
ifstream myfile ("/tmp/file.csv");
int lineNumber = 0;
int lineNumberSought = 3; // you may get it as argument
if (myfile.is_open()) {
while (getline(myfile,line)) {
lineNumber++;
if(lineNumber == lineNumberSought) {
cout << line << endl; ;
istringstream myline(line);
while(getline(myline, csvItem, ',')) {
cout << csvItem << endl;
}
}
}
myfile.close();
}
return 0;
}
This is not compilable as it stands.
Is this your bug?
if (endofline = lineNum-1)
You are assigning lineNum - 1
to endofline
. Use ==
for comparisons.
Just keep it simple. Let one getline loop do all the work. No need to special case for first line.
// ...
linesToGo = linesNum - 1;
string line;
while(getline(infile,line) && (linesToGo > 0)) {
linesToGo--;
}
if (linesToGo == 0) {
cout << "found line:," << line << endl;
// process here.
} else {
count << "not enough lines in file." << endl;
}
Also, dont mix cout and printf.
There's a SO question and answer on a way to go a specific line in a text file where you can find Xeo's answer as my favourite one. Xeo uses istream::ignore
, the adequate function so it is clean and fast solution.
Here's a complete example based on the answer mentioned above (with some decoration):
#include <fstream>
#include <limits>
#include <string>
#include <iostream>
using namespace std;
fstream& Go2Line(fstream& file, unsigned int num)
{
file.seekg(ios::beg);
for(unsigned int i=0; i < num - 1; ++i)
file.ignore(numeric_limits<streamsize>::max(),'\n');
return file;
}
int main()
{
fstream file("/media/LOGGING/darsv1.csv",ios_base::in);
if (!file)
cout << "Unable to open file /media/LOGGING/darsv1.csv\n";
else
{
int Number2Go = 4;
Go2Line(file, Number2Go);
if (!file)
cout << "Unable to reach line " << Number2Go << ".\n";
else
{
string line;
getline(file,line);
cout << "Line " << Number2Go << "reached successfully. It is:\n" << line;
}
}
return 0;
}
I created the following program because I am practicing with C++ algorithms. While the solution seems effective, please take it with a grain of salt (i.e., some people may consider it too complicated). On the other hand, it may help you understand certain aspects of iterators, streams, and algorithms.
#include<iostream>
#include<fstream>
#include<algorithm>
#include<iterator>
/**
@brief A trick proposed by Jerry Coffin / Manuel (SO users) to
create a line-by-line iterator. This makes an `std::istream`
yield lines as opposed to chars.
*/
struct Line
: std::string {
friend std::istream & operator>>(std::istream& is, Line& line) {
return std::getline(is, line);
}
};
/**
@brief This predicate contains an internal count of the lines read
so far. Its `operator()(std::string)` will evaluate to `true` when
the current line read equals the target line (provided during
construction).
*/
struct LineNumberMatcher {
unsigned int target_line;
unsigned int current_line;
LineNumberMatcher(unsigned int target_line)
: target_line(target_line),
current_line(0) { }
bool operator()(const std::string& line) {
return ++current_line == target_line;
}
};
int main(int argc, char* argv[]) {
if(argc != 3) {
std::cout<<"usage: "<<argv[0]<<" filename line"<<std::endl;
return 0;
}
std::string filename(argv[1]);
unsigned int target = std::stoi(argv[2]);
// Build the LineNumberMatcher
LineNumberMatcher match(target);
// Provide a scope after which the file will be automatically closed
{
// Open the file
std::ifstream fp(filename);
// Copy the line to standard output if the LineNumberMatcher
// evaluates to true (that is, when the line read equals the target
// line)
std::copy_if(std::istream_iterator<Line>(fp),
std::istream_iterator<Line>(),
std::ostream_iterator<std::string>(std::cout, "\n"),
match);
}
return 0;
}
Compile with C++11 support (in my case, g++ spitline.cpp -std=c++111
, using GCC 4.7.2). Sample output:
$ /a.out spitline.cpp 7
@brief A trick proposed by Jerry Coffin / Manuel (SO users) to
which, indeed, is line number 7 in my source code.