From owner-freebsd-questions@FreeBSD.ORG Fri Aug 1 00:00:28 2014 Return-Path: Delivered-To: freebsd-questions@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 6A59EB3C for ; Fri, 1 Aug 2014 00:00:28 +0000 (UTC) Received: from mx02.qsc.de (mx02.qsc.de [213.148.130.14]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 2E5722BA7 for ; Fri, 1 Aug 2014 00:00:27 +0000 (UTC) Received: from r56.edvax.de (port-92-195-69-249.dynamic.qsc.de [92.195.69.249]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx02.qsc.de (Postfix) with ESMTPS id A107C2524E; Fri, 1 Aug 2014 02:00:24 +0200 (CEST) Received: from r56.edvax.de (localhost [127.0.0.1]) by r56.edvax.de (8.14.5/8.14.5) with SMTP id s7100OA3003855; Fri, 1 Aug 2014 02:00:24 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Fri, 1 Aug 2014 02:00:24 +0200 From: Polytropon To: Gary Kline Subject: Re: how to grab text w/ fcanf Message-Id: <20140801020024.bf03b2d3.freebsd@edvax.de> In-Reply-To: <20140731233335.GA24151@ethic.thought.org> References: <20140731233335.GA24151@ethic.thought.org> Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: FreeBSD Mailing List X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Aug 2014 00:00:28 -0000 On Thu, 31 Jul 2014 16:33:35 -0700, Gary Kline wrote: > I've got a *.c file that returns in [let's say] > > /tmp/share/voice > > a bunch of files named: "text.1.txt", "text.2.txt", > "text.3.txt" ... text.N-1.txt", each file containing a few > sentences of plain ol' ASCII or [whatever] text. > > what is the easiest way, in C, *knowing the count=N*, to > grab the *text files and stuff the paragraphs into a global > buffer: char *parabuffer[1024]; ?? I think scandir() is what you're searching for, in combination with alphasort() to get the "natural" ordering of the files. Plain C. Example code: #include #include #include ... struct dirent **namelist; int i,n; n = scandir(".", &namelist, 0, alphasort); if (n < 0) perror("scandir"); else { for (i = 0; i < n; i++) { printf("%s\n", namelist[i]->d_name); free(namelist[i]); } } free(namelist); ... Source for example: http://pubs.opengroup.org/onlinepubs/9699919799/functions/alphasort.html You can iterate over namelist[] and access the d_name structure member for the file name, and then use string operations like strlcmp() or strnstr() to check for desired entries (given a file mask, such as "test.*.txt", as a pattern). See "man scandir" for details. Maybe consider readdir(), see "man readdir" which contains the following example: len = strlen(name); dirp = opendir("."); while ((dp = readdir(dirp)) != NULL) if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { (void)closedir(dirp); return FOUND; } (void)closedir(dirp); return NOT_FOUND; If this is too complicated, you could do the following, but know that it's quick and _very_ dirty: sprintf() the filenames with a counter N in the _known_ (!) pattern and test if the files do exist, with fopen(); if fopen() fails, you know that no further files will follow, you decrease the counter by 1 and have the last valid index. :-) As I initially said, this is quite low level C. Maybe you can see something more elegant in the Gtk 3 documentation? But if it has to be C, the suggested solutions are possible. -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...