Reputation:
I'm a beginner programmer working on a project that requires my code to read characters from an input file, manipulate them without changing the original file, and then print out the modified version to an output file. To do this, I need to use the fopen()
function at some point in the program, but I'm curious: what is actually happening here? Are the contents of the input file copied into the variable input1
?
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *input1;
input1 = fopen(argv[1], "r");
return 0;
}
Upvotes: 1
Views: 815
Reputation: 25
Read file handling APIs here.
http://www.thegeekstuff.com/2012/07/c-file-handling/
Upvotes: -1
Reputation: 4454
The fopen function opens a stream for I/O to the file filename, and returns a pointer to the stream. so,in your statement :
input1 = fopen(argv[1], "r");
fopen() opens the file argv[1] for reading and the address returned by the function is assigned to input1.This does not involve copying of the contents of the file.
you can then use the file pointer to read data from the file.for example :
char c = getc(input1);
Upvotes: 1
Reputation: 992707
The return value of fopen()
is a file handle, which is like a token that you can use later to interact with the file. You can pass your input1
to a function such as fgets()
or fread()
, depending on what you want to do with the file next.
The contents of the file are not copied anywhere by fopen()
.
Upvotes: 4