LasherHN
February 24th, 2009, 12:11 AM
I'm trying to understand how reading from a file works, I'm trying the following code:
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
int *m,*n;
m = (int*) malloc (sizeof(int));
n = (int*) malloc (sizeof(int));
FILE *infileptr;
infileptr = fopen(argv[1], "r");
fread(m, sizeof(int), 1, infileptr);
fread(n, sizeof(int), 1, infileptr);
cout<<*m<<" "<<*n<<endl;
}
I'm compiling it with
g++ -o filetest filetest.cpp
then running it with
./filetest a.txt
a.txt has one line:
1 2
The output is:
171057201 0
something is clearly wrong :(
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
int *m,*n;
m = (int*) malloc (sizeof(int));
n = (int*) malloc (sizeof(int));
FILE *infileptr;
infileptr = fopen(argv[1], "r");
fread(m, sizeof(int), 1, infileptr);
fread(n, sizeof(int), 1, infileptr);
cout<<*m<<" "<<*n<<endl;
}
I'm compiling it with
g++ -o filetest filetest.cpp
then running it with
./filetest a.txt
a.txt has one line:
1 2
The output is:
171057201 0
something is clearly wrong :(