Having pro with String & array!!

This is a discussion on Having pro with String & array!! within the C Programming forums, part of the General Programming Boards category; I try to make a string array with 5 element to access data file, but I'm so mess up!! So ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    21

    Having pro with String & array!!

    I try to make a string array with 5 element to access data file, but I'm so mess up!! So can somebody take a look at my code and help me out? thanx for any help.

    sample data file:a.dat
    Code:
    aa bb cc dd ee
    my code:
    Code:
     
    typedef char TYPE_PLANE[3];
    
    /*funtion to access data
    File opened already*/
    
    int get_type(TYPE_PLANE type[5], int *number, FILE *infile)
    {
    	int i;
    	TYPE_PLANE word[5];
    
    	fgets(word, 5, infile);
    	strcpy(type, word);
    
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    >>typedef char TYPE_PLANE[3];

    You have declared a string of 3 character long allowing you 2 chars (3rd will be a '\0')

    >>TYPE_PLANE type[5]
    Then an array of 5 of these eg Type[5][3].
    (I'm not sure this works with char strings the same way it does with say int's.)

    >>fgets(word, 5, infile);
    and then try to read 4/5 characters into the array that will only hold 2.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    21
    Thanx a lot novacain!!
    I'm kinda get that
    but i still confuse about what you said:

    >>fgets(word, 5, infile);
    and then try to read 4/5 characters into the array that will only hold 2.

    should i write like this:
    Code:
    for(i=0; i<5; i++)
        for(j=0; j<3; j++)
           fscanf("%s", word[i][j]);
    or can you give me a sample?

    Thanx in advance!!

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    When you declare an array of type char, a string variable, the last element will be a terminator '\0'. If you need 10 characters for say "1234567890" will have to declare

    char sString[11]={"1234567890"};
    NOT
    char sString[10]={"1234567890"};//this will crash

    >>fgets(word, 5, infile);
    This reads from 'infile' to 'word' for 5 chars.
    Word however has only been allocated 3 chars, one will be the terminator '\0' leaving two for your read of 5, so you will be using memory that is not yours eg crash.
    If your file has only
    aa
    bb
    cc
    dd
    ee
    then change fgets(word, 2, infile);

    For bigger strings try
    typedef char TYPE_PLANE[64];

    and

    TYPE_PLANE Type[5];

    for an array of five strings each 64 in length. As chars are only 1byte this is a tiny piece of mem to allocate (approx 1/3Kb out of a ave systems 128Mb).
    Best to have more mem than you need. I always allocate an extra 'bit' for safety.

    [EDIT]
    Your loop code looks good.

    fscanf( stream, "%c", &c );
    is the line I think you are looking for. Note the %c to read a single char not %s for a array of chars. (and the file pointer as first paramater)
    [/EDIT]
    Last edited by novacain; 02-06-2002 at 09:45 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21