Hello guys, i have an .txt file like this

Brasil
Portugal
Chile

Espanha

And i have to read this and put it in a array, i've been looking for this all day, but everyone ask me to do this in C++ using getline(), the problem is that i have to do it in C. Can someone help me???

Here is a little demo of using a 'C vector' (Cvec),
(here using ...
a readily expandable dynamic array of dynamic C strings,
to handle a problem of reading in a text file of lines,
using readLine, which is an emulation of C++ getline,
into a dynamic array of dynamic C strings ...
and then showing that vector of dynamic C strings.)

/* read_places.c */

/*

  a little demo of using "CvecOfString.h"
  to read and show file "places.txt"

  http://developers-heaven.net/forum/index.php/topic,2580.msg2865.html#msg2865

*/

/* also uses files "readLine.h" and "Cvec.h"  available at link above */
#include "CvecOfString.h"


const char* FNAME = "places.txt";
/*
Brasil
Portugal
Chile
Espanha
*/

void printCvec( const Cvec* cv )
{
    int i;
    for( i = 0; i < cv->size; ++ i )
    {
        printf( "%s\n", cv->ary[i].str );
    }
}

int main()
{
    FILE* fp = fopen( FNAME, "r" );
    if( fp )
    {
        Cvec cv;
        Rec rc;
        char* line;
        initCvec( &cv );
        while( (line = readLine( fp )) )
        {
            rc.str = line;
            push_backCvec( &cv, &rc );
        }
        fclose( fp );
        printf( "cv.size = %d, cv.cap = %d\n", cv.size, cv.cap );
        printCvec( &cv );

        printf( "\nAfter sorting ... \n" );
        msort( &cv );
        printf( "cv.size = %d, cv.cap = %d\n", cv.size, cv.cap );
        printCvec( &cv );

        printf( "\nAfter clearing ... \n" );
        clearCvec( &cv );

        printf( "cv.size = %d, cv.cap = %d\n", cv.size, cv.cap );
        printCvec( &cv );
    }
    else
        printf( "\nThere was a problem opening file %s\n", FNAME );
    return 0;
}

everyone ask me to do this in C++ using getline(),

fgets() is the C equivalent of c++ getline() (almost anyway).

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.