read a file

May 11, 2013 at 7:32pm
why the inputfile is NULL in here?

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
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
  FILE *outputfile;
  outputfile=fopen("dna1.dat","r+");
  if(outputfile!=NULL)
  {
                      
                      cout<<"DNA";
                              
                      fclose(outputfile);
  }
  else
  {
      cout<<"can't";
  }
  FILE *inputfile;
  inputfile=fopen("dna2.dat","r+");
  if(inputfile!=NULL)
  {
                      
                      cout<<"sequence";
                              
  fclose(inputfile);
  }
  else
  {
      cout<<"t";
      }
 
  system("pause");
  return 0;
}
May 11, 2013 at 7:43pm
For option r+ the file must already exist.
If you believe it already does exist, then check it is in the right location, usually the same folder as the executable program.
http://www.cplusplus.com/reference/cstdio/fopen/
May 11, 2013 at 8:12pm
Isn't the type "FILE" for use with "stdio.h"?
May 11, 2013 at 8:45pm
Yes, it is part of C rather than C++. But compilers often include the other headers, probably because one header file may include another header. C is a subset of C++ and the use of C code is permitted.
Topic archived. No new replies allowed.