www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/01/22:01:37

From: jason_hsu AT apexmail DOT com
Newsgroups: comp.os.msdos.djgpp
Subject: SIGSEGV error when reading a file into an array
Date: Sun, 02 Jul 2000 01:35:51 GMT
Organization: Deja.com - Before you buy.
Lines: 127
Message-ID: <8jm69n$tj0$1@nnrp1.deja.com>
NNTP-Posting-Host: 24.18.171.165
X-Article-Creation-Date: Sun Jul 02 01:35:51 2000 GMT
X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)
X-Http-Proxy: 1.1 x59.deja.com:80 (Squid/1.1.22) for client 24.18.171.165
X-MyDeja-Info: XMYDJUIDjason_hsu
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Here is the situation:

I have a spreadsheet saved as a tab-delimited text data file.  I am
trying to save the data to a string array.  However, I keep getting a
SIGSEGV stack fault error.  How do I avoid getting this error?  I'm
sure that someone here has been here before, and I would like to know
what it took for you to solve such a problem.

Some notes:
The text data file is about 2K, and the spreadsheet consists of 10
columns and 26 rows.

Here is the code.  (You'll want to create a file
for "input_numbers_file_name".)

#include <iostream.h>
#include <string>
#include <fstream.h>

void get_data(string file_name)
{
	char ch;
	int row_num=1, row_num_max=1, col_num=1, col_num_max=1,
data_length=1, data_length_max=1;

	fstream fin1 (file_name.c_str(), ios::in);
	if (!fin1)
		{
		cout << "Unable to open file";
		}
	while (fin1.get(ch))
		{
		if (ch==char(9))
			{
			data_length=1;
			col_num++;
			if (col_num>col_num_max)
				{
				col_num_max=col_num;
				}
			}
		else if (ch=='\n')
			{
			row_num++;
			col_num=1;
			}
		else
			{
			data_length++;
			if (data_length>data_length_max)
				{
				data_length_max=data_length;
				}
			}
		}
	fin1.close();
	row_num_max=row_num-1;
	cout <<"\n"<< row_num_max<<" rows\t"<<col_num_max<< "columns";
	cout <<"\nMaximum length of data: "<<data_length_max;

	row_num=1;
	col_num=1;
	string data_rc[row_num_max][col_num_max];
	string data_rc_temp;

	fstream fin2 (file_name.c_str(), ios::in);

	if (!fin2)
		{
		cout << "Unable to open file";
		}
	while (fin2.get(ch))
		{
		if (ch==char(9))
			{

			col_num++;
			data_rc_temp="";
			cout
<< "\nC"<<col_num<<"R"<<row_num<<"\t"<<data_rc[row_num][col_num];
			}
		else if (ch=='\n')
			{

			row_num++;
			/*
			col_num=1;

			cout
<< "\nC"<<col_num<<"R"<<row_num<<"\t"<<data_rc[row_num][col_num];
			data_rc_temp="";
			*/
			}
		else
			{
			/*
			data_rc_temp=data_rc_temp+ch;

			data_rc[row_num][col_num]=data_rc_temp;
			*/
			}
		int x=0;
		}
	cout<<"JUST BEFORE CLOSING fin2";
	fin2.close();

	}


int main ()
{
	get_data(input_numbers_file_name);

  	return 0;



}

--
Jason Hsu, KB9PNZ
jason_hsu AT apexmail DOT com
http://www.bigfoot.com/~jason_hsu/


Sent via Deja.com http://www.deja.com/
Before you buy.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019