473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Segmentation fault when passing file pointer

Howdy,

I was trying pass a file pointer from main to another function, so i can use the same file stream to perform certain task instead of closing stream on main then open a new one on the called function.

the code basically looks like this:

Expand|Select|Wrap|Line Numbers
  1. int func(FILE* pfile) {
  2.    //whatever task i need to perform
  3.  
  4. }
  5.  
  6. int main(){
  7.  
  8.    FILE* pfile;
  9.    pfile = fopen("filename", "f+");
  10.    while(feof(pfile) == 0){
  11.       //Print what's in the file
  12.    }
  13.  
  14.    func(pfile);
  15.  
  16. }
But when func is called, it gives me segmentation fault.
Am i doing something i'm not suppose to?

Thanks!
Oct 8 '11 #1
1 2516
weaknessforcats
9,208 Expert Mod 8TB
THis code:
Expand|Select|Wrap|Line Numbers
  1. while(feof(pfile) == 0){
  2.  //Print what's in the file
  3.  }
  4.  etc...
says to go into the loop only if the file is at end of file.

Maybe you could try:

Expand|Select|Wrap|Line Numbers
  1. while(feof(pfile) != 0){
  2.  //Print what's in the file
  3.  }
  4.  
Oct 9 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: deancoo | last post by:
Hello all, I have a class member, which calls another member from the same class. The problem I'm experiencing is that the nested function executes fine, but causes a segmentation fault as it...
5
by: sandwich_eater | last post by:
I get a segmentation fault in my program when resizing a vector. This seems strange because it is the second resize that causes the error. std::vector<double> a; std::vector<double> b; ...
10
by: Cocy | last post by:
Hi, This might be a sort of FAQ, but I don't see why, so I would someone help me to understand what's wrong? I've just created following code which wold trim white space(s) in a (given) string....
2
by: francescomoi | last post by:
Hi. I've got a C program which calls a function: ---------------- printf("TEST1\n"); int value = my_function(param); printf("TEST2\n"); int my_function(int my_param) {
12
by: nae zot bba la | last post by:
Hi, very short code here. Basically I want to open a file called instruction in a++ mode and then write data of %s type to it, then read from the same file and print to screen. The code compiles...
10
by: Piotr | last post by:
I have a class 'Statistics' which has a private attribute ' vector<int>* _x;' And in the destructor of the Statistics, I have this code to free the memory: Statistics::~Statistics() { if...
5
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size()...
6
by: Wes | last post by:
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++. The idea of the program is to open one file as input, read bytes from it, do some bitwise operations on the bytes, and then...
3
by: krista | last post by:
Hi, I am beginner of C++. When I try to read a file and put it into X and Y variables. I got that segmentation fault. the data file is data.txt: 4.4 6.8 3.2 -5.5 3.3 0.9
10
by: H.S. | last post by:
Hello, I have class in which I am allocating space for a double array in the constructor. I use the double array twice in one of the methods and then delete that array in the class's destructor....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.