Bad ptr when reading from text file

Hello,

I am trying to read integers from a text file as an exercise. I have included the code from my program. Watching the values of variables in the program in the debugger, i notice that after "f=fopen", f always has the value {_ptr=0x00000000000 <Bad Ptr>. I attempted to resolve this with the fseek function but it doesn't make any difference. When the fscanf statements are called, an Access Violation occurs, terminating the program.

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
31
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <ctype.h>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
	int i = 0;
	int j = 0;
	int k = 0;
	string m;
	FILE *f;
	f = fopen("nums.txt","r");
	fseek(f,0,SEEK_SET);
	while(!feof(f))
	{
	fscanf(f,"%s",m);
	cout<<m;
	fscanf(f,"%i",i);
	cout<<i;
	fscanf(f,"%i",j);
	cout<<j;
	fscanf(f,"%i",k);
	cout<<k;
	fclose(f);
	}
}



I guess I didn't state my post in the form of a question. What is causing the issue with the pointer? Is it was is causing the program to crash? How do I resolve this issue?
fscanf like scanf needs the ADDRESS of the variable.
e.g.
1
2
int i = 0;
fscanf(f,"%i",&i); //**address** of i 
.

Note: you should not be using the c++ string classstring though.So you will find that this one won't work:
1
2
string m;
fscanf(f,"%s", &m); //I can't see this one working 


There is also another error.. with that loop. One for you to find out.
Topic archived. No new replies allowed.