Results 1 to 1 of 1
Thread: xcode stream problem
xcode stream problem
- Member Since
- Jul 09, 2008
- 1
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
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
Xcode problemBy lordlucan in forum Switcher HangoutReplies: 32Last Post: 04-25-2012, 09:49 AM
Photo Stream problemBy DavaR in forum Images, Graphic Design, and Digital PhotographyReplies: 1Last Post: 01-28-2012, 01:52 AM
Macbook Pro Stream to Xbox ProblemBy chumph in forum Apple NotebooksReplies: 1Last Post: 01-25-2010, 12:09 AM
Xcode problemBy mathman in forum OS X - Apps and GamesReplies: 4Last Post: 06-08-2009, 11:09 AM
Save a stream opened via 'Advanced' -> 'Open Stream' in iTunes 7?By cavaleiro in forum iPod Hardware and AccessoriesReplies: 0Last Post: 01-08-2007, 08:56 AM