Thread: reading files into arrays

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    48

    reading files into arrays

    how do i read a text file's content into an array?

    i want it to work like this:

    the file contains the word "hello"

    the code would read the file and put the text in an array called text. text[0] would be 'h' text[1] would be 'e' and so on....

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    You'll need to get the file size. Once you have it, then you can create a dynamic array with the capacity of the file size in bytes, then fread() the file into an array.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by The Dog
    You'll need to get the file size. Once you have it, then you can create a dynamic array with the capacity of the file size in bytes, then fread() the file into an array.
    So you wouldn't just use fgets() then?

    In that FAQ example, stdin is a FILE* to the default input stream (normally the keyboard), but you can use a stream you've fopen'ed yourself.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    48
    can you give an example please?

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    There's is way to get the file size by seeking from the beginning of file to end of the file, then calling ftell() to get the file pointers position, and voila.

    Look up on these functions : fseek(), ftell()

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @SMB3Master: Do you want to complete file in memory at once? If you, do you understand how to process it once you have it? I'm kinda guessing you just want to read line by line?

    @The Dog: You forgot to mention that using fseek/ftell for determining a file size is not suitable to files opened in text mode.
    http://www.eskimo.com/~scs/C-faq/q19.12.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> You forgot to mention that using fseek/ftell for determining a file size is not suitable to files opened in text mode.

    I didn't even realize it. I usually write win32 progs so i use the api functions provided to get file info.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >can you give an example please?

    If you choose a fixed-size array with fread (not doing the line-by-line with fgets or using dynamic allocation) you might try something like this.
    Code:
    #include <stdio.h> 
    #include <ctype.h> 
    
    int main(void) 
    {
       char text[8];
       size_t i, count = 0;
       FILE *file = fopen(__FILE__, "rb");
       if ( file != NULL )
       {
          count = fread(text, 1, sizeof text, file);
          fclose(file);
       }
       for ( i = 0; i < count; ++i )
       {
          printf("text[%lu]", (long unsigned)i);
          printf(isprint(text[i]) ? " = '%c'\n" : " = 0x%02X\n", text[i]);
       }
       return(0); 
    }
    
    /* my output
    text[0] = '#'
    text[1] = 'i'
    text[2] = 'n'
    text[3] = 'c'
    text[4] = 'l'
    text[5] = 'u'
    text[6] = 'd'
    text[7] = 'e'
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    48
    i want to get the file's contents into an array like i said in my first post.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by SMB3Master
    i want to get the file's contents into an array like i said in my first post.
    Your question is still ambiguous, imo.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      FILE *fp;
      char text[6];
      fp = fopen("myfile.txt", "r");
      if (fp)
      {
        if (fgets(text, sizeof(text), fp))
        {
          puts(text);
        }
        
      }
      return(0);
    }
    If you know what's in the file already, what's the point in reading it in the first place? This program produces the same output (near enough) as the previous one:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      puts("hello");
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    48
    the purpose is reading a config file, this works perfectly.

    thanks

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how do i read a text file's content into an array?<<
    >>the file contains the word "hello"<<
    ....
    >>the purpose is reading a config file<<

    Config files consist of more than the word "hello", I'm sure. The Dogs and Daves answers probably won't help you, they were unfortunatley mislead by your rather vague question. Be more specific next time, and you'll be more likely to get a correct answer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    48
    using hello was only an example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 11:42 AM
  2. Issues reading text files
    By ozzy34 in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2004, 08:15 AM
  3. reading structured arrays, changing their structure
    By _kevin007 in forum C Programming
    Replies: 6
    Last Post: 05-15-2003, 03:49 PM
  4. Having some trouble with reading from files
    By bluebob in forum C Programming
    Replies: 19
    Last Post: 04-18-2002, 05:29 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM