I have written a code that reads from a file named network.dat
The code I wrote is
f = fopen("network.dat", "r");
if(f == NULL)
exit(1);
int read, N;
printf("%p\n", f);//output file pointer, included this just to check if file is opened properly
fscanf(f, "%d%d", &N, &read);//error here
cout<<N;
The file is being opened correctly and am getting the file pointer (49897488) as output but the line following it is where program stops working and I don’t get N as output.
Please tell if other detail is required.
Contents of network.dat are
10 1
1 6 1.28646
1 7 1.2585
2 9 1.33856
and so on. Am just focusing on first 2 numbers from the file i.e. 10 and 1.
As I stated in my comment, the problem is that your format specifier is incorrect. Try
Since you’re using
coutI’m fathoming a guess that this is actually C++ code… honestly, you should really not be doing this the canonical C way. Instead, useifstream.