LinuxQuestions.org
Review your favorite Linux distribution.
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-24-2003, 01:36 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
segmentation fault


I am readding a file that has a format below. All of the first
four fields have fixed length, while The last value
"lan1234567" is a variable field. It could contain anywhere from
10 to 50 characters.

I am reading all of the value using fscanf with w5 being predefined as an array with 50 elements (ex: w5[50]).
I used a for loop to set all array elements to '\0', but it still result in segmentation fault when there is less than 50 characters in
the file that was read.

I never access w5 by individual elements but only used it to write the entire array to a file using
fprintf(file_pointer_1, "%s %s %s %s %s", ip1, ip2, w3, w4, w5);

FILE FORMAT
216.143.22.145 200.168.111.222 : PSK "lan1234567"

=================================

Code:
read_ipsec_secrets1_file(eth_ipsec_secrets_0, eth_ipsec_secrets_1, w3, w4, w5)
  char eth_ipsec_secrets_0[16], eth_ipsec_secrets_1[16], w3[2], w4[4], w5[50];
  {
    FILE *file_pointer, *fopen();
    int i;

    file_pointer = fopen("/etc/ipsec.secrets1", "r");
    if (fopen("/etc/ipsec.secrets1", "r") == NULL)
      {
         perror("Couldn't open file ipsec.secrets1");
         exit(1);
      }

    fscanf(file_pointer, "%s%s%s%s%s", eth_ipsec_secrets_0, eth_ipsec_secrets_1, w3, w4, w5);

    fclose(file_pointer);
  }

/**********************************/

main()
 {
   char eth_ipsec_secrets_0[16], eth_ipsec_secrets_1[16], w3[2], w4[4], w5[50];
   int i;

   for (i = 0; i <= 49; ++i)
     {
       w5[i] = '\0';
       printf ("i = %d, w5 = %c\n", i, w5[i]);
     }

   read_ipsec_secrets1_file(eth_ipsec_secrets_0, eth_ipsec_secrets_1, w3, w4, w5);

Last edited by Linh; 10-24-2003 at 01:38 PM.
 
Old 10-24-2003, 02:16 PM   #2
Majjj
LQ Newbie
 
Registered: Sep 2003
Location: Hyderabad
Distribution: Fedora Core 2
Posts: 10

Rep: Reputation: 0
Hi

Your program looks perfect to me.
You need not run the loop to initialize the array as %s puts a \0 when the string input terminates.

Just check the file permissions of the output file you are writing to....
bye
Majjj
 
Old 10-24-2003, 04:58 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
I changed your code - fscanf can be a problem under some circumstances.... plus you opened the file twice.

Try something like this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define zout(z) memset(&z,0x00,sizeof(z))

/******************************
 test.file = 
 216.143.22.145 200.168.111.222 : PSK "lan1234567"  
 
 *******************************/

void read_ipsec_secrets1_file(char *, char *,char *,char *, char *);

/**********************************/
   
int main(int argc, char *argv[])
 { 
   char eth_ipsec_secrets_0[16], eth_ipsec_secrets_1[16], w3[2], w4[4], w5[50];
   zout(eth_ipsec_secrets_0);
   zout(eth_ipsec_secrets_1);
   zout(w3);
   zout(w4);
   zout(w5);
   read_ipsec_secrets1_file(eth_ipsec_secrets_0, eth_ipsec_secrets_1, w3, w4, w5);           
   printf("%s %s %s %s %s \n",eth_ipsec_secrets_0, eth_ipsec_secrets_1, w3, w4, w5);
   return 0;
}

void read_ipsec_secrets1_file(char *eth_ipsec_secrets_0, 
                              char *eth_ipsec_secrets_1, 
                              char *w3, 
                              char *w4, 
                              char *w5)
  {
    FILE *file_pointer;
    int i=0;
    char array[6][50];  /* one extra element */
    char tmp[256];
    char *ptr;
    
    file_pointer = fopen("test.file", "r");    /* file name for testing */
    if(file_pointer==NULL);  /* do not open file twice */
      {
         perror("Couldn't open file ipsec.secrets1");
         exit(EXIT_FAILURE);
      } 
    zout(tmp);
    if(fgets(tmp,sizeof(tmp),file_pointer)!=NULL) { /* we got data */
        for(i=0;i<5;i++) zout(array[i]);
        ptr=strtok(tmp," ");
        i=0;
        strcpy(array[i++],ptr);  /* first part of string */
        do {
           ptr=strtok('\0'," \n"); /* look for spaces and the end of the line */
           if(*ptr)
              strcpy(array[i++],ptr);
        
        } while (*ptr);
        i=0;
        strcpy(eth_ipsec_secrets_0,array[i++]);
        strcpy(eth_ipsec_secrets_1,array[i++]);
        strcpy(w3,array[i++]);                                
        strcpy(w4,array[i++]);                                
        strcpy(w5,array[i++]);                                                
    }   
    fclose(file_pointer);
  }
 
Old 10-28-2003, 12:18 PM   #4
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
reply

Hi jim mcnamara. Thank you for your help
 
Old 10-29-2003, 05:00 AM   #5
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
Linh,

Although I expect it is not causing your problem, you stated that the last field may contain up to 50 chars, this means that your w5 should be 51 chars long (char w5[51]) to allow for 50 characters + 1 for the null character (terminator)..

Cheers
 
Old 10-29-2003, 05:30 AM   #6
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
Hi....I tried running your program on my machine. (RH 7.1, kernel 2.4.2-4, gcc 2.96) and it's running without giving any segmentation fault.
 
Old 11-03-2003, 08:25 AM   #7
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
reply

Thank you everyone for helping me.
 
  


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
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 04:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 09:34 AM
Help !!! Segmentation fault mola Linux - Software 3 06-23-2005 11:13 AM
Segmentation fault tejas15_10 Programming 9 06-20-2005 09:12 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM


All times are GMT -5. The time now is 07:11 AM.

Main Menu
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