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

Segmentation error

Hii,
when I run this program using GCC there is no compilation error but I get segmentation error. Please help me

#include<stdio.h>
#include<string.h>

int main()
{
FILE *fp;
char *filename();

if ((fp = fopen (filename(),"r")) == NULL)

{
printf ("File could not be opened\n");
// error_handler();
}
else
{
printf ("file opened");
}
}

char *filename() /* return filename */

{ static char *filenm = "........................";

do
{
printf ("Enter filename :");
scanf ("%24s",&filenm);
// skipgarb();
}
while (strlen(filenm) == 0);
return (filenm);
}
Sep 18 '07 #1
11 1714
Savage
1,764 Expert 1GB
Well,here is your problem:

Expand|Select|Wrap|Line Numbers
  1. char *filename()                   /* return  filename */
  2.  
  3. { static char *filenm = "........................";
  4.  
  5. do
  6.    {
  7.    printf ("Enter filename :");
  8.    scanf ("%24s",&filenm);
  9.  //  skipgarb();
  10.    }
  11. while (strlen(filenm) == 0);
  12. return (filenm);
You have assigned to filenm a literal string,so it's now pointing to it,then you decide to change that string to other which is probably longer,and there you go,my favorite segmentation error.

A single question,why is filenm static?

PS:please use code tags(#button)
Savage
Sep 18 '07 #2
Thanks for the reply. But i am still not getting the output. I added the skipgarb function also. And I am using only a file name of 6 character "myfile". The program is as follows.

#include<stdio.h>
#include<string.h>

int main()
{
FILE *fp;
char *filename();

if ((fp = fopen (filename(),"r")) == NULL)

{
printf ("File could not be opened\n");
// error_handler();
}
else
{
printf ("file opened");
}
}

char *filename() /* return filename */

{ static char *filenm = "........................";

do
{
printf ("Enter filename :");
scanf ("%24s",&filenm);
skipgarb();
}
while (strlen(filenm) == 0);
return (filenm);
}

skipgarb() /* skip garbage corrupting scanf */

{
while (getchar() != '\n')
{
}
}

The error which I get is as follows. Help.

[sagar@localhost ~]$ gcc fileopp4.c
[sagar@localhost ~]$ ./a.out
Enter filename :myfile
Segmentation fault
[sagar@localhost ~]$
Sep 18 '07 #3
Savage
1,764 Expert 1GB
Try using gets instead of scanf.

Savage
Sep 18 '07 #4
No Better Luck. Same Error.
Sep 18 '07 #5
Savage
1,764 Expert 1GB
No Better Luck. Same Error.
It looks like that this happens because filename is a pointer,change it to stack array and it will work.

Savage
Sep 18 '07 #6
Savage
1,764 Expert 1GB
It looks like that this happens because filename is a pointer,change it to stack array and it will work.

Savage
You can get it to work with pointer also,but you must first initialize pointer to a valid address.

Savage
Sep 18 '07 #7
Hii thanks for your reply. I modified the program for better debugging and is as follows.
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#include<string.h>

int main()
{
#include<stdio.h>
#include<string.h>

char *filename() /* return filename */

{
static char *filenm = "........................";
printf("Entered the *file function\n");
do
{
printf ("Enter filename :");
scanf ("%24s",filenm);
printf("Took file name\n");
skipgarb();
}
while (strlen(filenm) == 0);
return (filenm);
}

skipgarb() /* Skip input garbage corrupting scanf */
{
while (getchar() != '\n');
printf("Finished skipgarb func");
}

int main()
{
FILE *fp;
char *filename();

if ((fp = fopen (filename(),"r")) == NULL)

{
printf ("File could not be opened\n");
}
else
{
printf ("File opened\n");
fclose (fp);
printf("File closed sucessfully");
}

}

Then when I ran the program the result is as follows.So the error is in the function where we enter the filename as it doent print the line "took file name". Give your suggestions. Tried gets also.

[sagar@localhost cprogfiles]$ ./a.out
Entered the *file function
Enter filename :myfile
Segmentation fault
Sep 18 '07 #8
Savage
1,764 Expert 1GB
Read my previous two posts.

Savage
Sep 18 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
static char *filenm = "........................";
Have you considered that filenm is a literal and that makes it const??? You cannot change the value of a literal.

Just create an array:
Expand|Select|Wrap|Line Numbers
  1. static char filenm[] = "........................";
  2.  
and things will start working.
Sep 24 '07 #10
Have you considered that filenm is a literal and that makes it const??? You cannot change the value of a literal.

Just create an array:
Expand|Select|Wrap|Line Numbers
  1. static char filenm[] = "........................";
  2.  
and things will start working.
(or)

just allocate memmory to get the input file name and point the poiter to the memmory, as

static char *filenm;
filenm = (char *)malloc(24);
scanf ("%24s",filenm);

it works...
Sep 15 '08 #11
JosAH
11,448 Expert 8TB
(or)

just allocate memmory to get the input file name and point the poiter to the memmory, as

static char *filenm;
filenm = (char *)malloc(24);
scanf ("%24s",filenm);

it works...
*ahem* this thread has been dead for about a year. No need to resurrect it.

kind regards,

Jos
Sep 15 '08 #12

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

Similar topics

1
by: Justin Tang | last post by:
Hi I am wondering about the segmentation fault in PHP. Namely, I'm running PHP version 4.6.9 on my server right now and when I try to process a large piece of text via textarea(3k+), the resulting...
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...
21
by: user | last post by:
I just finish writing LAB2 with no errors when compiling, but once i run it, i get "segmentation fault".. i don't know what is wrong, can anyoen tell me what it means and how i can fix it? thx!
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;
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...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
2
by: zl2k | last post by:
hi, all I am using a 2 dimensioanl array implemented by vector<vector<long> >. When the row number grows to 8 and I am trying to insert a new row, I got the segmentation error. However, if I...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
3
by: Dhieraj | last post by:
While compiling a C++ code I am getting the following error : CC -c -I/opt/iona/artix/2.0/include -I/opt/iona/asp/6.0/include -I/opt/ar/api63/include -I//var/tmp/vidya/aotscommon/include ...
0
by: ollii | last post by:
Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.