New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

xcode stream problem


Post Reply New Thread Subscribe

 
Thread Tools
mitchell

 
Member Since: Jul 09, 2008
Posts: 1
mitchell is on a distinguished road

mitchell is offline
when I run this I get a bus error.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Forward Declarations

//Post: opens file fname, reads file, returns an dynamic arrary of integers
// arrSz is passed by reference to keep track of size of dynamic array
void readData(string fname,int & arrSz);



int main (int argc, char * const argv[]) {
int filesize = 0;


readData("example.txt", filesize);



return 0;
}


// Reads contents of file into an array
// returns array, arrSz records the array size
void readData(string fname, int & arrSz){

//Declare Variables
int* result = NULL;
arrSz = 0;
int temp;

//Create a file object and open the file
ifstream inStream;
inStream.open(fname.c_str());

//only process file if opening it is successful
if(!inStream.fail()){
//find out how big the file is
while(!inStream.eof()){
arrSz++;
inStream >> temp; //reads line intotemp and moves to the next line
}

//admin functions to refresh file
inStream.close();
inStream.clear();
inStream.open(fname.c_str());

//Read file contents into result, now that size is known
result = new int[arrSz]; //creates results array

//reads contents into array
for(int i; i < arrSz; i++){
inStream >> result[i];
}
inStream.close();
}
cout << result[0];
}

I think the problem is in the argument "example.txt" When I get rid of the string fname parameter and just put the file name directly into inStream.open then it works fine. Any ideas?

Thanks
QUOTE Thanks

Post Reply New Thread Subscribe


« Work with private frameworks | Darwin issues, ive tried most things, any advice greatly appreciated! »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Problem With Keyboard...... mmcltd OS X - Operating System 4 02-02-2010 06:47 PM
Microsoft Messenger Problem millenium_spike OS X - Apps and Games 4 09-16-2008 01:11 AM
ibook G4 ventilation problem doki_2 OS X - Operating System 6 07-10-2008 03:10 AM
Macbook and spotty wireless-n problem (on two routers) yaderhey Internet, Networking, and Wireless 0 09-11-2007 09:06 PM
Huge Graphics Problem With Magic Garage Band mattcooper87 OS X - Apps and Games 0 08-21-2007 08:23 AM

All times are GMT -4. The time now is 12:04 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?