Thread: I can not write a value into an array of strings

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    1

    Angry I can not write a value into an array of strings

    My program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char *pins[1][1777];
    
    int load(char *name)
    {
       FILE *f;
       f = fopen(name, "r");
       if(f == NULL) return -1;
       char str[8];
       char *tmp;
       int i=0;
       while( fgets(str, 9, f) != NULL)
       {
          if(str[0] == '\n') continue;
          tmp = str;
          pins[0][i] = tmp;
          i++;
    }
       return 0;
    }
    
    int main(int argc, char *argv[])
    {
       if(argc != 3) { printf("Usage: %s [file] [mac]\n", argv[0]); return 1; }
       if(load(argv[1]) == -1) {
          printf("Error load file\n"); return 1;
       }
    
       char cmd[100];
       for(int i=0; i<1777; i++)
       {
          sprintf(cmd, "wpa_cli wps_reg %s %s", argv[2], pins[0][i]);
          printf("%s\n", cmd);
       }
       return 0;
    }
    I ran the program and saw such rubbish:
    Code:
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    wpa_cli wps_reg s @�Ϫ�
    And each launch displays a different nonsense!
    I replaced these lines:
    Code:
    tmp = str;
          pins[0][i] = tmp;
    on:
    Code:
    sprintf(pins[0][i], "%s", str);
    And the segmentation error comes out!I read a book on it, but there it was not taught to me on any page for the whole book, and how to work with strings in C if it is so difficult?
    In general, help me to solve this problem !!

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    On line 4 you declare an array for 1 string hat cold hold 1777 chars - I guess that's not what you actually want.
    On line lines 11 + 14. str has space for 7 chars + terminating '\0', but on line 14 you try to read 9 chars into it.
    Another problem is on line 18. pins[0][i] can hold a single char but you try to store more in it.

    Yes, strings are error prone in C. In C++ you have strings which are much easier to use. If you have a choice switch to C++.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Before you start messing with pointers, start with
    char pins [1777][10];
    Then use strcpy to copy each str you read into successive pins[i]

    Also, don't lie about your buffer sizes
    char star [10];
    fgets (str, sizeof (str), fp )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-13-2017, 01:29 AM
  2. Replies: 3
    Last Post: 02-23-2012, 08:30 AM
  3. fwrite can't write dynamic strings???
    By Devil Panther in forum C Programming
    Replies: 26
    Last Post: 05-19-2009, 02:18 AM
  4. How do u write up an array of strings?
    By hot_ice in forum C Programming
    Replies: 1
    Last Post: 11-19-2001, 08:41 PM

Tags for this Thread