Hello,
I google and search through about the 2 dimension array and I still not really understand how to do it..

for example:

I got a file input called namelist.txt with following thing
John
Jason
Leonard
Kelly
Kate
Ash

and I got the following code:

#include <stdio.h>

int main ()
{
    FILE* spopen;
    char name[20];
    int count = 0;

    spopen = fopen ("list.txt", "r");


    while ( (fscanf (spopen, "%s", &name)) != EOF)
    {
        printf ("%s\n", name);
    }

    return 0;
}

I got list of names print out, but how do I assign those names into array so that after that while loop, I can do the following thing.

for example,
let say I want to print the 2nd and 3rd name in the list (which is Jason and Leonard).
so I use,

printf ("%s \n %s \n", name[what to type?], name[What to type?]);

how do I assign all the names into array called nameAry? like:

nameAry[0][what to type?] = John
nameAry[1][what to type?]=Jason
nameAry[2][what to type?]=Leonard
nameAry[3][what to type?]=Kelly
nameAry[4][what to type?]=Kate
nameAry[5][what to type?]=Ash

Thank you in advance. and it is my 1st post here ^^.
Good day.

Tom Gunn commented: Good first post. :) +8

Recommended Answers

All 4 Replies

You know how to use 1D arrays, right? 2D arrays are 1D arrays where every item is an array. C has arrays of arrays instead of true multidimensional arrays:

int a[5] = {1,2,3,4,5};

[0] -> [1][2][3][4][5]

int b[3][2] = 
{
    {1, 2},
    {3, 4},
    {5, 6}
};

[0] -> [1][2]
[1] -> [3][4]
[2] -> [5][6]
#include <stdio.h>

#define ASIZE 5
#define BXSIZE 3
#define BYSIZE 2

int main()
{
    int a[ASIZE] = {1,2,3,4,5};
    int b[BXSIZE][BYSIZE] = 
    {
        {1, 2},
        {3, 4},
        {5, 6}
    };
    int x, y;

    for (x = 0; x < ASIZE; ++x)
    {
        printf("%d%s", a[x], x < ASIZE - 1 ? " " : "\n");
    }

    for (x = 0; x < BXSIZE; ++x)
    {
        for (y = 0; y < BYSIZE; ++y)
        {
            printf("%d%s", b[x][y], y < BYSIZE - 1 ? " " : "\n");
        }
    }

    return 0;
}

A string variable is a char array, right? So an array of strings is an array of char arrays, and instead of using name to get the string variable, you use name[x] . Everything else is the same:

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

#define NAME_CNT 5
#define NAME_LEN 20

int main()
{
    /* 5 names up to 19 chars each */
    char name[NAME_CNT][NAME_LEN];
    size_t x, y, n;

    printf("Enter %d names\n", NAME_CNT);

    for (x = 0; x < NAME_CNT; ++x)
    {
        printf("Name #%d: ", x + 1);
        fflush(stdout);

        if (!fgets(name[x], NAME_LEN, stdin)) break;

        /* trim any trailing line break */
        n = strlen(name[x]);
        if (name[x][n-1] == '\n') name[x][n-1] = '\0';
    }

    for (y = 0; y < x; ++y) puts(name[y]);

    return 0;
}
spopen = fopen ("list.txt", "r");
while ( (fscanf (spopen, "%s", &name)) != EOF)

Do not forget to check that fopen() succeeded. If it did not, it returns a null pointer and your program will probably crash without any warning or hint as to what happened:

spopen = fopen("list.txt", "r");

if (!spopen)
{
    perror("opening list.txt");
    /* exit or recover */
}

And do not forget to close the file when you are done with it. ;)

while ( (fscanf (spopen, "%s", &name)) != EOF)

fscanf() does return EOF at the right time for this loop, so the code is right. But I think it is a better habit to use the number of conversions for the test instead of EOF directly. If you expect 1 conversion and fscanf() returns EOF, the loop will stop as it should. But if the stream gets corrupted somehow and instead of EOF, no conversions are made and fscanf() returns 0, the loop will not stop as it should. Using the expected conversions for the test works in both cases.

Another small nitpick is that name is already a char pointer and that is what fscanf expects. Used as a value, array names decay into a pointer to the first item. When you say &name , it adds a level of indirection and the type changes to a pointer to a char array. Mismatching types in fscanf() is undefined behavior:

while (fscanf(spopen, "%s", name) == 1)
{
    printf ("%s\n", name);
}

if (ferror(spopen))
{
    perror("spopen stream state");
    /* exit or recover */
}

fclose(spopen);
commented: An excellent and informative reply. +4
commented: seconded +24

You may want to look at std classes.

vector<string> myStringArray

is way better than the clasical array

You may want to look at std classes.

vector<string> myStringArray

is way better than the clasical array

In C? ;)

In C? ;)

my mistake ! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.