Win32中的缓冲区问题 [英] Buffers Problem in Win32

查看:155
本文介绍了Win32中的缓冲区问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有win32代码用于读取和解析一些csv文件。我正在尝试读取文件,存储在缓冲区中,然后进行解析并进行一些计算。

代码包括主函数和一些子函数。我使用了全局向量缓冲区。



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.

#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);
	}


}





问题是当我尝试阅读Line& Main函数中的数据向量值未显示任何值,但在Pars()& read()函数显示正确的值。

此脚本中的任何错误?

问候,



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,

推荐答案

At在Read函数结束时,释放缓冲区。之后(在你的主函数中)不允许对这个缓冲区内的指针进行任何引用。



你应该返回缓冲区并在以后释放它

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
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;
}





请记住,在C / C ++数组中从0开始索引,这意味着行[1]是数组行的第2项。



And remember, in C/C++ array are indexed starting at 0, that means line[1] is the 2nd item of array line.


您将指向临时内存的指针存储到行向量中,而您应该而是复制内容。

为什么不用这种方式定义行:

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:
vector <string> line;




?


这篇关于Win32中的缓冲区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆