Click here to Skip to main content
15,887,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have win32 code for read and parsing some csv files. I am trying read files, store in buffers and next parsing and do some calculations.
Code is include Main function and some sub functions. I used Global vector buffers.

C++
#include "stdafx.h"
#include <math.h>
#include <iostream>
#include  <vector>

using namespace std;
typedef vector<char*> charA;
charA line;
vector<charA> data;
double Corr(vector<double>& Set1, vector<double>& Set2);
void Read(char *fileName);
void pars();

int _tmain(int argc, _TCHAR* argv[])
{
	

	Read("a.csv");
	cout<<data[1][1]<<"\n";
        cout<<line[1]<<"\n";
	
	line.clear();
	data.clear();

	
	return 0;
}

void Read(char *fileName){
	FILE * pFile;
		long lSize;
		char * buffer;
		size_t result;

		pFile = fopen ( fileName , "rb" );

		if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

		// obtain file size:
		fseek (pFile , 0 , SEEK_END);
		lSize = ftell (pFile);
		rewind (pFile);

		// allocate memory to contain the whole file:
		buffer = (char*) malloc (sizeof(char)*lSize);
		if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
		
		result = fread (buffer,1,lSize,pFile);
		if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
			char * pch;
			pch = strtok (buffer,"\n");
		int d=0;
		while (pch != NULL) {
			line.push_back(pch);
				pch = strtok (NULL, "\n");
				
		}
		pars();
		free (buffer);
}

void pars(){
	char *pch;
	
	for(int i=0;i<line.size();i++){
		
		charA tmpArray;
	pch = strtok (line[i],",");
		while (pch != NULL) {
			 tmpArray.push_back(pch);
		pch = strtok (NULL, ",");
	}
		data.push_back(tmpArray);
	}


}


Problem is when I try read Line & data vector values in Main function not showing any values, but in Pars() & read() functions showing correct values.
Any bugs in this script?
Regards,
Posted

At the end of the Read function, you free the buffer. After that (and in your main function) any reference to a pointer inside this buffer is not allowed.

You should return the buffer and free it later
C++
int _tmain(int argc, _TCHAR* argv[])
{
	
 
	char* buffer = Read("a.csv");
	cout<<data[1][1]<<"\n";
        cout<<line[1]<<"\n";
	
	line.clear();
	data.clear();
        free(buffer);
 

	
	return 0;
}


And remember, in C/C++ array are indexed starting at 0, that means line[1] is the 2nd item of array line.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Nov-13 16:04pm    
Well spotted, a 5.
—SA
You are storing pointers to temporary memory into line vector while you should instead copy the content.
Why don't you define line this way:
C++
vector <string> line;

?
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 4-Nov-13 16:04pm    
5ed. It's often the good idea to get away from char*, etc...
—SA
CPallini 4-Nov-13 16:21pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900