Hi, I'm getting a segmentation fault on this code when it gets to fopen:
tac.h: - typedef struct
-
{
-
char* ventanaInspeccion;
-
char* ventanaMemoria;
-
FILE* archivo;
-
} TAC;
-
-
int TAC_Abrir(TAC* tac, char* archMensaje);
-
main.c -
TAC tac;
-
TAC_Abrir(&tac, argv[2]);
tac.c: -
int TAC_Abrir(TAC* tac, char* archMensaje){
-
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
7 8404
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
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.
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!
Hi, I'm getting a segmentation fault on this code when it gets to fopen: - 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. - FILE *theFile;
-
theFile = fopen(archMensaje,"rt");
-
tac->archivo = theFile;
Cheers,
donbock
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. - FILE *theFile;
-
theFile = fopen(archMensaje,"rt");
-
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...
For fun (I've nothing better to do...)
I tried taking your code and running it on my Linux box... - #include <stdio.h>
-
-
typedef struct
-
{
-
char* ventanaInspeccion;
-
char* ventanaMemoria;
-
FILE* archivo;
-
} TAC;
-
-
int TAC_Abrir(TAC* tac, char* archMensaje) {
-
tac->archivo=fopen(archMensaje,"rt");
-
if (tac->archivo == NULL) {
-
printf("NULL\n");
-
}
-
else {
-
printf("not NULL\n");
-
}
-
}
-
-
int main(int argc, char** argv) {
-
TAC tac;
-
TAC_Abrir(&tac, argv[2]);
-
-
return 0;
-
}
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 :");...
|
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;
|
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...
|
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...
|
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 =...
|
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 ;
|
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);
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |