Thread: Question about File Input

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    38

    Question about File Input

    In the program I'm writing I'm having a bit of a problem. The program is reading in a number of strings from an input file and placing them into a character array, malloc-ing the memory, then analyzing them in a number of different ways. However, I can't read in the strings before I declare the character array, and that's where I encounter my problem. Now I know each string will be less than one 'line' long i.e. less than 80 characters or so. The question is, how would I setup a while loop to determine the number of strings? Here's the input file, but obviously it's going to need to work with any number of input files.

    Code:
    palindromes
    String manipulation and recursion
    Otto
    Command-line arguments
    Anna
    file handling and dynamic memory allocation
    Able was I ere I saw Elba
    ctype.h
    Madam, I'm Adam.
    Use of menus: Integer vs. Character input
    Array of strings?
    Looking for EOF?
    Was it a cat I saw?
    A man, a plan, a cat, a canal: Panama?
    It's a palindrome project, hence some of the odd strings. Basically, I'm trying to figure out how to setup a loop that would determine I need an array of size 14 after reading that data file? Any tips appreciated!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The best way would be to maintain some dynamic data structure that grows accordingly. The "simplest" of these is a dynamic array. When you begin to reach the maximum size, realloc the array. Though I would rather use a linked list as I find them less finicky than realloc.

    As an alternative, you could always read the file twice. Once to find out how many strings are there and again to save them to a new dynamically allocated array.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Right, that was my plan. I was going to read it and then just rewind. I just wasn't sure what to setup as the condition of my while loop to have it stop at the end of the input file.

    Edit: anyone have any suggestions on how I can do that?
    Last edited by ChristianTool; 04-22-2004 at 06:06 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The two loops would be identical except the first wouldn't save the line and the second would:
    Code:
    int n;
    char buf[81];
    char (*lines)[81];
    
    /* fopen fin and such */
    n = 0;
    while ( fgets ( buf, sizeof buf, fin ) != NULL ) {
      n++;
    }
    lines = malloc ( n * sizeof *lines );
    rewind ( fin );
    n = 0;
    while ( fgets ( buf, sizeof buf, fin ) != NULL ) {
      strcpy ( lines[n++], buf );
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Beautiful. Thanks a bunch!

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Okay, here's my problem now. In my function FillArray, the array is filled with the strings from the input file. Then, just for testing purposes, I have it print those strings out. That function works perfectly. However, when I open the MenuOne function and attempt to display the strings to the user, as I'm supposed to do for the program, I get some strange results.

    Here's the code, anyone have any ideas?

    Code:
    void FillArray ( FILE *data, char *strArray )
    {
       char buf[81];
       int counter = 0;
    
       rewind ( data );
    
       while ( fgets ( buf, 81, data ) != NULL )
       {
          strcpy ( &strArray[counter], buf );
          printf( "\n%s", &strArray[counter] );
          counter++;
       }
    
       MenuOne ( counter, strArray );
    }
    Then:

    Code:
    /* Controls the first level menu                 */
    /* name: MenuOne                                 */
    /* inputs: Number of strings, array of strings   */
    /* returned values: none                         */
    void MenuOne ( int numStrings, char *strArray )
    {
       int i;
    
       for ( i = 0; i < numStrings; i++ )
       {
          printf( "%d: %s\n", i + 1, &strArray[i] );
       }
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is strArray a single string, or an array of character pointers? If the latter, then use it correctly, instead of the botched way you're doing it:
    Code:
    void MenuOne( int numStrings, char *sArray[] )
    {
        ...
            printf(" %d: %s\n", i+1, sArray[i] );
    ...
    Your code has a single character array. Not an array of character pointers.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    That's what I'm having trouble with. How would I do that? In the FillArray function I'm printing it in basically the same exact way as I am trying in the MenuOne function. Also, I understand that arrays of strings are two dimensional functions, but when printing the whole string, what would I have to do with the second number. I.e. strArray[2] would obviously be the third string in the array, but wouldn't strArray[2][5] just be the 6th character in that string? So are you saying I'd have to print it one character at a time?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char *strArray
    Not an array of strings unless you're doing something creative. Perhaps
    Code:
    char **strArray
    would work better for you. But it depends on how strArray was originally declared.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    strArray was declared as :

    Code:
    char **strArray = NULL;
    It is supposed to be an array of strings. I probably don't have to tell you this is my first time messing with those. I've used two dimensional arrays, but always of a constant size. I.e. I knew the size when writing the program.

    Basically, I'm having problems with the array going between functions. I.e. in the FillArray function it's working properly. I'm filling each element of the array one by one in the for loop, and prints it out afterwards. However, as soon as I leave that loop, it's not acting normally. I.e. initially it prints as such :

    Code:
    palindromes
    String manipulation and recursion
    Otto
    Command-line arguments
    Anna
    file handling and dynamic memory allocation
    Able was I ere I saw Elba
    ctype.h
    Madam, I'm Adam.
    Use of menus: Integer vs. Character input
    Array of strings?
    Looking for EOF?
    Was it a cat I saw?
    A man, a plan, a cat, a canal: Panama?
    But when I print it in the exact same manner in the next function, MenuOne it prints as such:
    Code:
     pSOCAfAcMUALWA man, a plan, a cat, a canal: Panama?
    SOCAfAcMUALWA man, a plan, a cat, a canal: Panama?
    OCAfAcMUALWA man, a plan, a cat, a canal: Panama?
    CAfAcMUALWA man, a plan, a cat, a canal: Panama?
    AfAcMUALWA man, a plan, a cat, a canal: Panama?
    fAcMUALWA man, a plan, a cat, a canal: Panama?
    AcMUALWA man, a plan, a cat, a canal: Panama?
    cMUALWA man, a plan, a cat, a canal: Panama?
    MUALWA man, a plan, a cat, a canal: Panama?
    UALWA man, a plan, a cat, a canal: Panama?
    ALWA man, a plan, a cat, a canal: Panama?
    LWA man, a plan, a cat, a canal: Panama?
    WA man, a plan, a cat, a canal: Panama?
    A man, a plan, a cat, a canal: Panama?
    My function call for MenuOne is :
    Code:
    MenuOne ( counter, strArray );
    And its prototype is as follows, and it's printed out as such :
    Code:
    void MenuOne ( int numStrings, char *strArray )
    
     for ( i = 0; i < numStrings; i++ )
          {
             printf( " %s\n", &strArray[i] );
          }
    Last edited by ChristianTool; 04-24-2004 at 08:15 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void MenuOne ( int numStrings, char *strArray )
    See if you can spot the problem. If you can't, reread the above posts.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    I'm sorry, I'm not trying to sound redundant. I'm just new to trying to work with an array of strings of a dynamic size. I can't seem to find an online resource, anything in my text book, etc. to explain it. It's the syntax I'm having a problem with, I pretty much understand the logic. I just need to know how to declare it, pass it to functions, and a way of being able to index it for analysis.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I just need to know how to declare it, pass it to functions, and a way of being able to index it for analysis.
    Did you even read my last post? If you had, and followed it's instructions, you would have reread my first post and Prelude's last post. Either way works.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Beginner Text File Input Question
    By Ruggles in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2006, 02:18 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM