Need a simple example of passing FILE pointers


 
Thread Tools Search this Thread
Top Forums Programming Need a simple example of passing FILE pointers
# 1  
Old 02-05-2008
Data Need a simple example of passing FILE pointers

Dear friends,

can anybody pls tell me how to pass FILE pointer in c. I am so confused .. Smilie

suppose I ve two function
1. file_open()
2. read_line()

I want to call these function from main() function and in file_open() function it will open that file and in read_line() function I ve take the file pointer from read_line() function then read line by line ..

I tried with code , but I am sure I am doing some stupid stuffs.. Pls Pls help me to get the code right.
Code:
FILE* fileopen();
void read_line(FILE*);
 
void read_line(FILE **fh){
      char s[50];
      while(fgets(s,49,fh)!= NULL)
              printf("%s", s);
      fclose(fh);
}
 
FILE* fileopen(){
  FILE *file = fopen("abc1.txt", "r");
  return file;
}
 
int main(void) { 
 
   FILE *fh;
   fh = fileopen();
   read_line(&fh);   
  return 0;
}

If Any other example .. Pls
# 2  
Old 02-05-2008
I should open and close the file in the main function but... try this, change the declaration of the pointer and the call of the read_line function:


Code:
FILE* fileopen();
void read_line(FILE*);
 
void read_line(FILE *fh){ ///// change this line
      char s[50];
      while(fgets(s,49,fh)!= NULL)
              printf("%s", s);
      fclose(fh);
}
 
FILE* fileopen(){
  FILE *file = fopen("abc1.txt", "r");
  return file;
}
 
int main(void) { 
 
   FILE *fh;
   fh = fileopen();
   read_line(fh);   //// change this line
  return 0;
}


Regards
# 3  
Old 02-05-2008
Quote:
Originally Posted by Franklin52
I should open and close the file in the main function but... try this, change the declaration of the pointer and the call of the read_line function:


Code:
FILE* fileopen();
void read_line(FILE*);
 
void read_line(FILE *fh){ ///// change this line
      char s[50];
      while(fgets(s,49,fh)!= NULL)
              printf("%s", s);
      fclose(fh);
}
 
FILE* fileopen(){
  FILE *file = fopen("abc1.txt", "r");
  return file;
}
 
int main(void) { 
 
   FILE *fh;
   fh = fileopen();
   read_line(fh);   //// change this line
  return 0;
}


Regards
Really many many Thanks for the help .

I ll neva forget this..
# 4  
Old 02-05-2008
is there any problem if I dont close a file after opening it?

Quote:
Originally Posted by user_prady
Really many many Thanks for the help .

I ll neva forget this..

IS there any problem if I omit the line fclose(fp); if the fp is open .

If yes what ll be the problem..
# 5  
Old 02-05-2008
It's usually to close the file after using it so the pointer can be used to access a different file.
If files are still open when a program exits, the system will close them. However it is usually better to close the files properly.
Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it.


Regards
# 6  
Old 02-05-2008
Bug

Quote:
Originally Posted by Franklin52
It's usually to close the file after using it so the pointer can be used to access a different file.
If files are still open when a program exits, the system will close them. However it is usually better to close the files properly.
Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it.


Regards
Thank you very much .. I got it now..

Your FILE pointer programme helped me a lot for my project..
Thanks again..
GOD bless You..

Regards,
user_prady
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Pointer to pointers

Hi guys, I'm trying to understand pointers in C and made a simple example and I've problems with It. Can someone help? #include <stdio.h> #include <stdlib.h> #include <assert.h> int f1(char **str_); int main(int argc, char **argv) { char *str = NULL; f1(&str); ... (3 Replies)
Discussion started by: pharaoh
3 Replies

2. Programming

Passing Pointers by reference in C++ Problem

Hello All, I am having this issue...where I am actually having hard time understanding the problem: The code is as follows: #include<iostream.h> void fxn(char*** var) { int i =4; *var = (char**)malloc(i*sizeof(char*)); for(int j =0; j<4; j++) { *var = "name"; cout<<*var;... (6 Replies)
Discussion started by: mind@work
6 Replies

3. Homework & Coursework Questions

Passing pointers to struct

Hi, i'm trying to copy a struct into a binary file using the unix instruction write, so i declare and fill the struct "superbloque" in one function "initSB" and then i pass the pointer to another function called bwrite (for block write) which calls write. The problem is that i call the function... (2 Replies)
Discussion started by: ignatius3
2 Replies

4. Programming

Problem With Pointers

Hi guys. What is the difference between these: 1. int *a; 2. int (*a); (2 Replies)
Discussion started by: majid.merkava
2 Replies

5. Programming

Need help with the Pointers in C

I have a special character called . When it is declared as a character variable its showing it can be printed. But when it is declared as a character pointer variable its showing it cannot be printed. I am just wondering why its happening like this.. c1 = '@'; c2 = ''; char *fp; fp="XX"; if... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

6. Programming

pointers

Hi I mash with pointers in C. I solve this problem about 5 hours and I don't know how I should continue. void InsertFirst (tList *L, int val) { tElemPtr new; if((new = malloc(sizeof(tElemPtr))) == NULL) Error(); new->data = val; new->ptr = L->frst; L->frst = new;... (2 Replies)
Discussion started by: Milla
2 Replies

7. Programming

pointers

is this a valid c declaration int (*ptr(int *b)); plz explain... (4 Replies)
Discussion started by: areef4u
4 Replies

8. Programming

Pointers and array

hi all, let say i have a pointer exit, and this exit will store some value. how can i store the value that the pointer points to into an array and then print them out from the array. thanks in advance (2 Replies)
Discussion started by: dianazheng
2 Replies

9. Programming

Regarding Function and Pointers.

HI, Here is some thing that is puzzling me from a long time. Can some body explain me this with example. The question is :- What is the difference between function pointer and pointer to a function. Where do we actually use the function pointers and pointer to functions. Thanks in... (0 Replies)
Discussion started by: S.Vishwanath
0 Replies

10. Programming

Pointers to Arrays

Below is the program i tried to execute...... main() { static int a = {0,1,2,3,4}; static int *p = {a, a+1, a+2, a+3, a+4}; printf (\n %u %u %d, p, *p, *(*p) ); } This works, but i wanted to know why both a and *p are declared as "static". If we dont declare a as static... (2 Replies)
Discussion started by: Jayathirtha
2 Replies
Login or Register to Ask a Question