delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/02/19:16:33

From: Sean Proctor <sproctor AT ccs DOT neu DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: SIGSEGV error when reading a file into an array
Message-ID: <qsgvls8vimr5vuhao7ra1jh4c3kmrsoukd@4ax.com>
References: <8jm69n$tj0$1 AT nnrp1 DOT deja DOT com>
X-Newsreader: Forte Agent 1.7/32.534
MIME-Version: 1.0
Lines: 149
Date: Sun, 02 Jul 2000 23:05:38 GMT
NNTP-Posting-Host: 207.16.154.218
X-Complaints-To: Abuse Role <abuse AT enter DOT net>, We Care <abuse AT newsread DOT com>
X-Trace: newshog.newsread.com 962579138 207.16.154.218 (Sun, 02 Jul 2000 19:05:38 EDT)
NNTP-Posting-Date: Sun, 02 Jul 2000 19:05:38 EDT
Organization: ENTER.net (enter.net)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sun, 02 Jul 2000 01:35:51 GMT, jason_hsu AT apexmail DOT com wrote:

>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;
initialize these to 0;
>
>	fstream fin1 (file_name.c_str(), ios::in);
would be simpler changed to:
void get_data(char filename[])
{
	...
	fstream fin1(file_name, ios::in);
>	if (!fin1)
>		{
>		cout << "Unable to open file";
		return;		/*don't want to keep going if you
couldn't open the file*/
>		}
>	while (fin1.get(ch))
>		{
>		if (ch==char(9))
is clearer as if(ch == '\t')
I had to go look up what ASCII 9 was...
>			{
>			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;
change above to: col_num = 0;
>			}
>		else
>			{
>			data_length++;
>			if (data_length>data_length_max)
>				{
>				data_length_max=data_length;
>				}
>			}
>		}
simpler as if you use a switch above, in my opinion, easier to add to
as well.
>	fin1.close();
>	row_num_max=row_num-1;
no longer need the above line.
>	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);
why not reuse fin1?
	fin1.open(file_name, ios::in);
I do believe.
then you'd need to change the rest of these...
>
>	if (!fin2)
>		{
>		cout << "Unable to open file";
		return;
>		}
>	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];
Huh?  you never do antying to data_rc, ever... why would you output
one of its elements?
>			}
>		else if (ch=='\n')
>			{
>
>			row_num++;
			col_num = 0;
>			/*
>			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;
			I'm thinking += would work here, but I'm
unfamiliar with C++ strings.
>
>			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);
huh? you didn't list and global variables... and at no point was there
any reference to input_numbers_file_name... unless you're expecting us
to translate that to something like "file.ext" in which case... why
use a string at all?  just use a char[], it's simpler.
>
>  	return 0;
>
>
>
>}

- Raw text -


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