davedelong/CHCSVParser

Problem with initForWritingToCSVFile

ijacob opened this issue · 6 comments

Hi Dave,

I was assuming when I do something like this:

CHCSVWriter *writer = [[CHCSVWriter alloc] initForWritingToCSVFile:@"export.csv"];

for (Tbl_ExpenseRecords *noteInfo in xpenseData) {
    // NSLog(@"%@",noteInfo.cardname01);
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.expensedate]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.title]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.value]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.currency]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.cardname01]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.name_]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.address_]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.postalCode_]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.city_]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.country_]];
    [writer finishLine];
}
[writer closeStream];

It would create a file in the Apps DocumentDirectory but there is no file, am I doing something wrong? Need some advise because I'm not very familiar with the stream method.

Thanks in advance
Ingemar

Off the top of my head, I believe the “CSVFile” parameter needs to be a full path, not just the name of a file.

Dave

On Jul 25, 2013, at 10:01 AM, ijacob notifications@github.com wrote:

Hi Dave,

I was assuming when I do something like this:

CHCSVWriter *writer = [[CHCSVWriter alloc] initForWritingToCSVFile:@"export.csv"];

for (Tbl_ExpenseRecords *noteInfo in xpenseData) {
// NSLog(@"%@",noteInfo.cardname01);
[writer writeField:[NSString stringWithFormat:@"%@",noteInfo.expensedate]];
[writer writeField:[NSString stringWithFormat:@"%@",noteInfo.title]];
[writer writeField:[NSString stringWithFormat:@"%@",noteInfo.value]];
[writer writeField:[NSString stringWithFormat:@"%@",noteInfo.currency]];
[writer writeField:[NSString stringWithFormat:@"%@",noteInfo.cardname01]];
[writer writeField:[NSString stringWithFormat:@"%@",noteInfo.name_]];
[writer writeField:[NSString stringWithFormat:@"%@",noteInfo.address_]];
[writer writeField:[NSString stringWithFormat:@"%@",noteInfo.postalCode_]];
[writer writeField:[NSString stringWithFormat:@"%@",noteInfo.city_]];
[writer writeField:[NSString stringWithFormat:@"%@",noteInfo.country_]];
[writer finishLine];
}
[writer closeStream];
It would create a file in the Apps DocumentDirectory but there is no file, am I doing something wrong? Need some advise because I'm not very familiar with the stream method.

Thanks in advance
Ingemar


Reply to this email directly or view it on GitHub.

I tried this:
NSURL *datapath = [[self applicationDocumentDirectory] URLByAppendingPathComponent:@"export.csv"];
NSString *urlString = [datapath absoluteString];

CHCSVWriter *writer = [[CHCSVWriter alloc] initForWritingToCSVFile:urlString];

worked neither

In that case, I think you're running in to Issue #46. I don't have a quick answer to that right now. I'll perhaps have some time to work on this in a couple of days.

Can you tell me how to do the stream thing to create that file? I went through the documentation and also the Apple docs but it honestly it makes no sense to me

Got it, just needed a few minutes fresh air :)

NSURL *datapath = [[self applicationDocumentDirectory] URLByAppendingPathComponent:@"export.csv"];

NSOutputStream *output = [NSOutputStream outputStreamToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:output encoding:NSUTF8StringEncoding delimiter:','];

for (Tbl_ExpenseRecords *noteInfo in xpenseData) {
    // NSLog(@"%@",noteInfo.cardname01);
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.expensedate]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.title]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.value]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.currency]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.cardname01]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.name_]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.address_]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.postalCode_]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.city_]];
    [writer writeField:[NSString stringWithFormat:@"%@",noteInfo.country_]];
    [writer finishLine];
}
[writer closeStream];

NSData *buffer = [output propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
[buffer writeToURL:datapath atomically:NO];

Yep, that will definitely work if you have a small-to-moderate amount of data. If you've got lots and lots and lots of data, you'll notice you're memory usage grow commensurately. Like I said, I'll try and deal with this in a couple of days.