473,385 Members | 1,736 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,385 software developers and data experts.

Segmentation Fault with fopen

9
Hi, I'm getting a segmentation fault on this code when it gets to fopen:

tac.h:

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.     char* ventanaInspeccion;
  4.     char* ventanaMemoria;
  5.     FILE* archivo; 
  6. } TAC;
  7.  
  8. int TAC_Abrir(TAC* tac, char* archMensaje);
  9.  
main.c
Expand|Select|Wrap|Line Numbers
  1. TAC tac;
  2. TAC_Abrir(&tac, argv[2]);
tac.c:
Expand|Select|Wrap|Line Numbers
  1. int TAC_Abrir(TAC* tac, char* archMensaje){
  2. tac->archivo=fopen(archMensaje,"rt");
This not all the code, but the part with the problem..
As I said, when it gets to fopen(), a segmentation fault occurs. archMensaje's content is fine and file exists.

Why could this be happening?

Many Thanks
Sep 8 '08 #1
7 8404
pootle
68
Did you check the tac pointer? When you dereference it to put the result of fopen into archivo is it valid? How is it allocated?

Regards
Sep 8 '08 #2
arnaudk
424 256MB
The only potential error I can see is the mode of fopen. I'm not familiar with "t" perhaps you mean "r+"? If "t" is valid for the library you're using then the error is elsewhere in your code.
Sep 8 '08 #3
macaco
9
Did you check the tac pointer? When you dereference it to put the result of fopen into archivo is it valid? How is it allocated?

Regards
It is allocated when program starts. As I am declaring tac as struct, not as a pointer to struct.

The only potential error I can see is the mode of fopen. I'm not familiar with "t" perhaps you mean "r+"? If "t" is valid for the library you're using then the error is elsewhere in your code.
't' is used to make it clear that it'll be a text file and not a binary 'b'. However, I did try with r, without success.
Debugger breaks on that line, how could the problem be elsewhere?

Many Thanks both!
Sep 8 '08 #4
donbock
2,426 Expert 2GB
Hi, I'm getting a segmentation fault on this code when it gets to fopen:
Expand|Select|Wrap|Line Numbers
  1. tac->archivo=fopen(archMensaje,"rt");
Do you know if the fault occurs during the execution of fopen() or when you write into the structure? You might introduce a temporary variable just while debugging this in order to clarify where the problem is. This also gives you chance to see if fopen() is returning NULL.
Expand|Select|Wrap|Line Numbers
  1. FILE *theFile;
  2. theFile = fopen(archMensaje,"rt");
  3. tac->archivo = theFile;
Cheers,
donbock
Sep 8 '08 #5
pootle
68
Do you know if the fault occurs during the execution of fopen() or when you write into the structure? You might introduce a temporary variable just while debugging this in order to clarify where the problem is. This also gives you chance to see if fopen() is returning NULL.
Expand|Select|Wrap|Line Numbers
  1. FILE *theFile;
  2. theFile = fopen(archMensaje,"rt");
  3. tac->archivo = theFile;
Cheers,
donbock
And also...
You said that archMensaje is correct - but are you sure it is properly alllocated/null terminated? Passing a rubbish filename pointer would cause a segfault... Just for testing, try replacing it with a literal string...
Sep 9 '08 #6
pootle
68
For fun (I've nothing better to do...)
I tried taking your code and running it on my Linux box...

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. typedef struct
  4. {
  5.     char* ventanaInspeccion;
  6.     char* ventanaMemoria;
  7.     FILE* archivo; 
  8. } TAC;
  9.  
  10. int TAC_Abrir(TAC* tac, char* archMensaje) {
  11.     tac->archivo=fopen(archMensaje,"rt");
  12.     if (tac->archivo == NULL) {
  13.        printf("NULL\n");
  14.     }
  15.     else {
  16.        printf("not NULL\n");
  17.     }
  18. }
  19.  
  20. int main(int argc, char** argv) {
  21.    TAC tac;
  22.    TAC_Abrir(&tac, argv[2]);
  23.  
  24.    return 0;
  25. }
This code, when runs without any segmentation fault...
I just passed two arguments on the command line and if the file exists, it prints "not NULL", if it does not, it prints "NULL".

Could you try compiling and running this code to see if you still get a segfault?
Sep 9 '08 #7
bgross
1
I have the same issue. It seems the problem is that MinGW's fopen function does not work properly. I've seen this time and time again in my own code.

I got the same results as pootle, my code would run perfectly fine (or at least fopen ;) ) using any other compiler.
Apr 14 '11 #8

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

Similar topics

3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
16
by: Glen | last post by:
i get segmentation fault when i execute the following code(the platform is gnu/linux) #include<stdio.h> int main() { char fn1,fn2,tn; printf("\nEnter the first file name :");...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
10
by: F?bio Botelho | last post by:
Sorry About the english.... This program copy one file to another , but when i run it it's gives-me an error: Segmentation fault I don't understand ... because it pass my test : opens the...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
6
by: jan247 | last post by:
hi, im new here... um, this code works except that, at the end of the loop, i always get a "Segmentation fault". Can anyone help me on this? using GNU C: ..... FILE *fp; fp =...
19
by: Sameer | last post by:
Hi friends, I am using Mandriva Linux 9.2 and gcc. My source code is, int chunkin ; //no error int i ; for (i=0;i<7225;i++) { chunkin = somedata ;
8
by: Piotr S. | last post by:
What's wrong in following program: #include <stdio.h> #include <stdlib.h> char s; int main() { FILE *fp, *fopen(); if(fp = fopen("plik1","r")== NULL) { printf("blad otwarcia"); exit(1);
8
by: parkskier | last post by:
Ok, so I got a segmentation fault when I tried to run the program I created. I understand what this error means, but I don't know where it is. If someone can quickly look through my code and point...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.