473,749 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segmentation fault when passing file pointer

1 New Member
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 2528
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
11688
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 exits. I've traced it to the point in the code just before the line following the nested function is to be called, or conversely right after the last line of the nested function. What might be my problem? I'm hoping someone can give me some...
5
9331
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; a.resize(ndata - 1); b.resize(ndata - 1); // fault occurs here
10
2391
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. But, it resulted the Segmentation fault, and so as when running in gdb (saying "Program received signal SIGSEGV, Segmentaion fault at *p++ = *st++"). The platform is Linux kernel 2.4.27, gcc version
2
1241
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
2307
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 but when I execute the binary and enter some string to expect the program to print them for me to screen, segmentation fault occurs. Any ideas anyone? #include <stdio.h> #include <stdlib.h> char *add;
10
2904
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 (_x) {
5
6752
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() const { return _bdl.size(); }
6
2478
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 write them to this second file. However, when the second file is 15360 bytes long, the program dies with a "Segmentation Fault (core dumped)" error! I checked with gdb, and it says the last function to run was memcpy() from libc, which would...
3
4671
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
12514
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. Now, that delete operation is giving me a segmentation fault. If I move the allocation and deletion of the pointer within the method where the pointer is being is used, it works okay ... but then another pointer gives a segmentation fault in...
0
8833
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9389
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.