Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
xcode stream problem
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="mitchell" data-source="post: 688597" data-attributes="member: 60668"><p>when I run this I get a bus error.</p><p>#include <iostream></p><p>#include <fstream></p><p>#include <string> </p><p>using namespace std;</p><p></p><p>// Forward Declarations</p><p></p><p>//Post: opens file fname, reads file, returns an dynamic arrary of integers</p><p>// arrSz is passed by reference to keep track of size of dynamic array</p><p>void readData(string fname,int & arrSz); </p><p></p><p></p><p></p><p>int main (int argc, char * const argv[]) {</p><p> int filesize = 0;</p><p> </p><p></p><p> readData("example.txt", filesize); </p><p> </p><p> </p><p> </p><p> return 0;</p><p>}</p><p></p><p></p><p>// Reads contents of file into an array </p><p>// returns array, arrSz records the array size</p><p>void readData(string fname, int & arrSz){</p><p> </p><p> //Declare Variables</p><p> int* result = NULL;</p><p> arrSz = 0;</p><p> int temp;</p><p> </p><p> //Create a file object and open the file </p><p> ifstream inStream;</p><p> inStream.open(fname.c_str());</p><p> </p><p> //only process file if opening it is successful </p><p> if(!inStream.fail()){</p><p> //find out how big the file is </p><p> while(!inStream.eof()){</p><p> arrSz++;</p><p> inStream >> temp; //reads line intotemp and moves to the next line</p><p> }</p><p> </p><p> //admin functions to refresh file </p><p> inStream.close();</p><p> inStream.clear();</p><p> inStream.open(fname.c_str());</p><p> </p><p> //Read file contents into result, now that size is known</p><p> result = new int[arrSz]; //creates results array</p><p> </p><p> //reads contents into array</p><p> for(int i; i < arrSz; i++){</p><p> inStream >> result<em>;</em></p><p><em> }</em></p><p><em> inStream.close(); </em></p><p><em> }</em></p><p><em> cout << result[0];</em></p><p><em>}</em></p><p><em> </em></p><p><em>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?</em></p><p><em></em></p><p><em>Thanks</em></p></blockquote><p></p>
[QUOTE="mitchell, post: 688597, member: 60668"] 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[/i] [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
xcode stream problem
Top