CSV File C++
Dec 12, 2009 at 2:39am UTC
This is actually based off an example helios gave me a fair while back. I needed to do this in C back then though. But now I wanted to throw it into C++.
Anyway it works fine but I want to know If I can remove the
char buffer[1024]
somehow inside the
LoadDataBase()
function. And just make it a string for the getline from the file - as I'm curious if I can do this without using chars. I'm sure it's something really stupid I'm missing or typed wrong previously. I just can't get getline to work how I want. If there is anything else you can see I could neaten up - please do point it out.
Heres an example file
1 2 3 4 5 6
NAME,DIRECTORY,NUMBER
bob1,Data\Bob.png,1
bob2,Data\Bob.png,2
bob3,Data\Bob.png,3
bob4,Data\Bob.png,4
bob5,Data\Bob.png,5
Heres the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
struct DBitem
{
std::string name;
std::string directory;
int number;
};
void LoadDataBase( std::vector<DBitem>& db, std::string fileName )
{
std::ifstream file;
file.open( fileName.c_str(), std::ifstream::in );
if ( file.is_open() )
{
// Get the first trash line.
char buffer[1024];
file.getline(buffer, 1024);
while ( file.getline(buffer, 1024) )
{
DBitem temp;
std::string line = buffer;
size_t first = line.find(',' );
size_t second = line.find(',' , first + 1);
temp.name = line.substr(0, first);
temp.directory = line.substr(first + 1, second - (first + 1) );
{
std::stringstream stream(line.substr(second+1));
stream >> temp.number;
}
db.push_back(temp);
}
}
else
{
std::cout << "Error loading file [" << fileName << "]" << std::endl;
}
}
void DisplayDataBase( const std::vector<DBitem>& db )
{
for ( unsigned int i = 0; i < db.size(); i++ )
{
std::cout << db[i].name << " " << db[i].directory << " " << db[i].number << std::endl;
}
}
int main()
{
std::vector<DBitem> database;
LoadDataBase(database, "DB.txt" );
DisplayDataBase(database);
return 0;
}
Dec 12, 2009 at 4:11am UTC
Just clicked then. I changed it too:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
void LoadDataBase( std::vector<DBitem>& db, std::string fileName )
{
std::ifstream file;
file.open( fileName.c_str(), std::ifstream::in );
if ( file != NULL )
{
std::string line;
std::getline(file, line);
while ( std::getline(file, line) )
{
DBitem temp;
size_t first = line.find(',' );
size_t second = line.find(',' , first + 1);
temp.name = line.substr(0, first);
temp.directory = line.substr(first + 1, second - (first + 1) );
std::stringstream stream(line.substr(second+1));
stream >> temp.number;
db.push_back(temp);
}
file.close();
}
else
{
std::cout << "Error loading file [" << fileName << "]" << std::endl;
}
}
Still if anyone has any ideas how to neaten this up let me know :)
Last edited on Dec 12, 2009 at 4:13am UTC
Topic archived. No new replies allowed.