Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with inputting multidimensional string arrays....
#61
Yay, I figured it out
and there was much rejoicing Big Grin

anyways, the problem was that I read one of your examples wrong and it messed everything up. Here is the function that works:

Code:
char *handle_input(char *s, char *printwhat, char *errmsg) {
    char buff[80];
    cout << endl << printwhat;
    fgets(buff, 80, stdin);
    strcpy(s, buff);
    if (strlen(ltrim(rtrim(s))))
        return s;
    else {
        setcolor(8);
        cout << endl << errmsg << endl;
        setcolor(7);
        return s;
    }
}

Another question. How do you make an infinite loop?
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#62
Many ways
Code:
for (;;){ }
Code:
while (true) { }
Code:
do { }

and others I don't want to think of right now.
am an asshole. Get used to it.
Reply
#63
how could I create a list of all the available .txt files in a directory?
I know I would need an array of strings, but I don't know of a command for something like that.
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#64
I've never addressed that problem in C... you may want to do a google search. Or, post your question on http://www.tek-tips.com
am an asshole. Get used to it.
Reply
#65
Quote:how could I create a list of all the available .txt files in a directory?
I know I would need an array of strings, but I don't know of a command for something like that.

Do you need help on getting the DIR contents, or making the array of strings?

Array of strings is just a pointers array:

Code:
char **array_of_strings;

array_of_strings = (char **) malloc (num_elements * sizeof(char*));

for (i=0; i<num_elements; i++)
   array_of_strings[i] = (char *) malloc (num_chars * sizeof(char));

Getting the DIR contents is platform-dependent, but I could help you with ANSI sollutions.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#66
Thanks. I need help with DIR commands.
Also, what is ANSI?

I have another question:
which one is better to use? They do exactly the same thing.

Code:
FILE *f;
    f = fopen(filename, "r");
    if (!f) return 1;
    fgets(buff, 80, f);
    strcpy(word->theword, ltrim(rtrim(buff)));
    fgets(buff, 80, f);
    strcpy(word->definition, ltrim(rtrim(buff)));
    word->clues = atoi(fgets(buff, 80, f));
    for (i = 0; i < word->clues; i++) {
        fgets(buff, 80, f);
        strcpy(clue[i], ltrim(rtrim(buff)));
    }
    fclose(f);
Code:
ifstream infile(filename);
    if (!infile.is_open()) return 1;
        infile.getline(buff, 80);
        strcpy(word->theword, buff);
        infile.getline(buff, 80);
        strcpy(word->definition, buff);
        infile.getline(buff, 80);
        word->clues = atoi(buff);
        for (i = 0; i < word->clues; i++) {
            infile.getline(buff, 80);
            strcpy(clue[i], ltrim(rtrim(buff)));
        }    
    infile.close();
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#67
ANSI is the standard institute. ANSI C refers to the standarized C.

You can read the directory contents using the "ffblk" structure and "findfirst" and "findnext" commands. The ffblk structure is used to store stuff about a filesystem item (a file, or a directory):

Code:
struct ffblk {
       char lfn_magic[6];           /* LFN: the magic "LFN32" signature */
       short lfn_handle;            /* LFN: the handle used by findfirst/fin
       unsigned short lfn_ctime; /* LFN: file creation time */
       unsigned short lfn_cdate; /* LFN: file creation date */
       unsigned short lfn_atime; /* LFN: file last access time (usually 0) */
       unsigned short lfn_adate; /* LFN: file last access date */
       char ff_reserved[5];      /* used to hold the state of the search */
       unsigned char ff_attrib;  /* actual attributes of the file found */
       unsigned short ff_ftime;  /* hours:5, minutes:6, (seconds/2):5 */
       unsigned short ff_fdate;  /* (year-1980):7, month:4, day:5 */
       unsigned long ff_fsize;   /* size of file */
       char ff_name[260];        /* name of file as ASCIIZ string */
     }

findfirst and findnext just find the first file, and next file, given a ffblk structure. They return nonzero if more files are available, so they are perfect to run accross a directory:

Code:
// Prints a list of *.exe files in the current directory
     struct ffblk f;
     int done = findfirst("*.exe", &f, FA_HIDDEN | FA_SYSTEM);
     while (!done)
     {
       printf("%10u %2u:%02u:%02u %2u/%02u/%4u %s\n",
         f.ff_fsize,
         (f.ff_ftime >> 11) & 0x1f,
         (f.ff_ftime >>  5) & 0x3f,
         (f.ff_ftime & 0x1f) * 2,
         (f.ff_fdate >>  5) & 0x0f,
         (f.ff_fdate & 0x1f),
         ((f.ff_fdate >> 9) & 0x7f) + 1980,
         f.ff_name);
       done = findnext(&f);
     }

Constants used:

Code:
`FA_RDONLY'
     Include read-only files in the search (Ignored.)

`FA_HIDDEN'
     Include hidden files in the search

`FA_SYSTEM'
     Include system files in the search

`FA_LABEL'
     Include the volume label in the search

`FA_DIREC'
     Include subdirectories in the search

`FA_ARCH'
     Include modified files in the search (Ignored.)

function prototypes:

Code:
#include <dir.h>

int findfirst(const char *pathname, struct ffblk *ffblk, int attrib);
int findnext(struct ffblk *ffblk);

I've used this in Linux and gcc ports (mingw, djgpp...), so maybe it won't work for you if you use MSVC (I've not tested it). I'll take a glance on better alternatives.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#68
<dir.h> doesn't exist niether does <dir>
also, look at my previous post, I edited it right before you replied (it's a different question about which method is better to load files)
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#69
I found something that works for MSVC

Code:
#include <stdio.h>
#include <iostream.h>
#include <io.h>

void main( void ) {
struct _finddata_t file;
long hFile;

if( (hFile = _findfirst( "*.*", &file )) == -1L )
printf( "No files in current directory!\n" );
else {
do {
cout << file.name << endl;
} while( _findnext( hFile, &file ) == 0 );

_findclose( hFile );
}
}

yay!
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#70
LOL it looks really similar Big Grin

About your first question, I'd use the FILE sollution. C-style, more portable, just uses stdio.h... well, i like it more Smile But it, as always, depends on what you prefer. Use which looks easier for you.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)