Can't figure why it won't work.

I have been given some code which reads some values from a .txt document. It supposedly was tested on a PC compiler and runs perfectly. However, when I try to run it through Xcode's C++ I receive a:
'Thread 1: EXC_BAD_ACCESS code=1, address =0x68' message.

Here is the code, if you guys could please point out what I'm missing...that would be brilliant. I am new to C++ so hopefully it isn't too stupid of a mistake.
Thank You.

/* fscan example */
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

/* char str [80];
int i;

printf ("Enter your family name: ");
scanf ("%79s",str);
printf ("Enter your age: ");
scanf ("%d",&i);
printf ("Mr. %s , %d years old.\n",str,i); */
int main()
{

float overpotential[1000], current_density[1000];
int i=0, ipairs=0;
char top_line[80];
FILE *pFile;

pFile = fopen("Al.txt","r");
fscanf(pFile, "%79s", top_line);
fscanf(pFile, "%i", &ipairs);
cout<<top_line<<"\n";
cout<<ipairs<<"\n";
while (i<ipairs)
{
fscanf(pFile, "%f %f", &overpotential[i], &current_density[i]);
cout<<overpotential[i]<<" " <<current_density[i] <<"\n";
// cout<<i<<"\n";
i++;

}
//now find OCP by searching current_density for near zero value, interpolating and calculating OCP


//cout<<i;



fclose(pFile);


}
Last edited on
Those are some pretty big arrays that you're creating as local variables. Maybe you're exceeding the stack size. Try making them static to see if that helps:
static float overpotential[1000], current_density[1000];

Dave
I made the change, but to no avail. This is the line of the error, if it helps:
fscanf(pFile, "%79s", top_line);
Thank you for replying.
That code is crap. Throw it away.

(It won't compile on any compiler. You are better off trying to figure out how to do it yourself anyway.)

Everything you do must be in a function. When the user starts your program, the main() function runs first. Hence, a basic program always starts with it:

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main()
{
  cout << "Hello world!\n";
}

For anything with strings, include <string>.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string users_name;

  cout << "What is your name? ";
  getline( cin, users_name );

  cout << "Hello " << users_name << "!\n";
}

Remember that whenever you ask the user for something, he will press ENTER after that input. So if you plan to ask for both strings and (things like numbers), you must pay attention to getting rid of that ENTER key. The getline() function gets rid of it. All the stuff like cin>>foo does not, so you have to make sure to fix that at the right places.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
{
  unsigned age;
  double favorite_number;
  string name;

  cout << "What is your age? ";
  cin >> age;  // does not read the ENTER key, but that's okay, our next input is:

  cout << "What is your favorite number? ";
  cin >> double;  // skips the previous ENTER key, does not read the one after the user's favorite number.

  cout << "What is your name? ";

  // Except next we want to read a string, so we have to get rid of that ENTER key that's sitting there in the input waiting to be read.
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );

  // Okay, now we can read a string, ending with (and throwing away) the next ENTER key press
  getline( cin, name );

  cout << "Okay, " << name << ", your favorite number ";
  if (age == favorite_number) cout << "is";
  else                        cout << "is not";
  cout << " your favorite number. What's up with that?\n";
}

(By the way, you should be compiling and running these to see what is happening. This is the fastest crash-course you will ever get.)

Those "cin" and "cout" things are each a "stream". So is a file. To handle a file, include <fstream> and use it the same. The only thing is you have to specifically open a file before you can use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include <limits>
#include <iostream>
#include <string>
using namespace std;

int main()
{
  unsigned age;
  string name;

  ifstream fin( "a_file.txt" );  // ifstream for reading, ofstream for writing

  // The file is presumably two lines long (at least): 
  // the first line has a whole number; the second line has a name.
  fin >> age; 
  fin.ignore( numeric_limits<streamsize>::max(), '\n' );
  getline( fin, name );

  cout << "Hello " << name << ", you are " << age << " years old.\n";

  // You don't have to call fin.close() here -- when the "fin" object is 
  // destroyed, the file will be closed for you.
}

Make sure to refer to the tutorial for more information, like arrays and "if" and "while" control constructs and the like.

http://www.cplusplus.com/doc/tutorial/

Hope this helps.
@Duoas: if you are referring to the OP's code, the name/age part is commented out.
So apart from the use of 'stdio.h' instead of 'cstdio' there is nothing syntactically wrong with it.


@OP: Check out that you are actually opening the file. (pFile not_eq NULL)
Topic archived. No new replies allowed.