LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-16-2006, 07:49 AM   #1
mesh2005
Member
 
Registered: Sep 2005
Location: Ägypten
Distribution: Ubuntu 5.10
Posts: 155

Rep: Reputation: 30
Unhappy strcat segmentation fault


I made a simple c program to create some files on the form sample1, sample2, sample3 ,....
I got segementation fault, I don't know why. Can anyone help me to do it using C (not C++) ?

#include <stdio.h>
#include <stdlib.h>

int main (int argc , char *argv[])
{
int i =0;
FILE *fp;
for (i=0 ; i< 10 ; i++)
{
fp = fopen (strcat("sample",i) , "wb");
fclose(fp);
}
}
 
Old 10-16-2006, 08:13 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Quote:
char *strcat(char *dest, const char *src);

DESCRIPTION
The strcat() function appends the src string to the dest string over-
writing the `\0' character at the end of dest, and then adds a termi-
nating `\0' character. The strings may not overlap, and the dest
string must have enough space for the result.
strcat writes to the buffer pointed to by the first argument. Since you are putting a constant string, it is overwriting past the end, thus the segfault. You need to use a buffer with some space for writing extra letters:
Code:
int main (int argc , char *argv[])
{
int i =0;
FILE *fp;
char filename[16] = "sample";
   for (i=0 ; i< 10 ; i++)
   {
 //strncat doesn't write more characters than the 3rd argument
      fp = fopen (strncat(filename,i, sizeof(filename)-6) , "wb");
      fclose(fp);
   }
}
 
Old 10-16-2006, 08:14 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,120

Rep: Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818
Try something like this instead:
Code:
#include <stdio.h>
#include <stdlib.h>

int     main    (int argc, char *argv [])
{
        char    fname [FILENAME_MAX];
        int     i;
        FILE    *fp;

        for (i = 0; i < 10; i++) {
                (void) sprintf (fname, "sample%02d", i);
                if ((fp = fopen (fname, "w")) == (FILE *) NULL) {
                        (void) fprintf (stderr,
                            "%s:\tcan't open %s\n",
                            argv [0], fname);
                        exit (EXIT_FAILURE);
                }
                /*      close the input file            */
                if (fclose (fp)) {
                        (void) fprintf (stderr,
                            "%s:\tcan't close %s\n",
                            argv [0], fname);
                }
        }
        exit (EXIT_SUCCESS);
}

Last edited by tronayne; 10-16-2006 at 10:50 AM. Reason: Get the spacing right
 
Old 10-16-2006, 08:25 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
And another problem that you will have is that i is an integer. You can not add an integer to a string, unless you use tronayne's solution above (sprintf).
 
Old 10-16-2006, 08:59 AM   #5
mesh2005
Member
 
Registered: Sep 2005
Location: Ägypten
Distribution: Ubuntu 5.10
Posts: 155

Original Poster
Rep: Reputation: 30
Thank you all
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strcat is causing seg fault (c program) mfrick Programming 9 08-20-2014 08:34 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 10:34 AM
Segmentation fault on strcat() Ephracis Programming 8 12-17-2004 02:28 AM
strcat and direntp->d_name segmentation fault [I'm guessing the problem, plz propose] zeppelin Programming 3 04-12-2004 03:52 PM
Segmentation fault.. please help me! rpmadness Linux - Software 4 11-25-2003 07:47 PM


All times are GMT -5. The time now is 10:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration