save data in array

This is a discussion on save data in array within the C Programming forums, part of the General Programming Boards category; if i was reading from a text file, and would want to save the word in to array. how should ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    23

    save data in array

    if i was reading from a text file, and would want to save the word in to array. how should i do this?

    Code:
    char *buffer[]={"aa", "bb", "cc", "dd", "ee", "ff"};
    this is hard coded in the program, but if all the data from aa to ff was initially in another file, and i will need a file pointer to read them out from the text file, how can i save them in array?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Well if you knew how many words and how long the biggest word was, you could have

    char buffer[6][3];

    If you know how many words, but not their lengths
    char *buffer[6];

    If you don't know either, then
    char **buffer;

    Anything with a * in it will require use of malloc at some point.

    It's pretty easy to go from a true 2D array to a ** pointer, once you've got the array version working. So I would suggest you start with a really simple known file and try reading those words into a 2D array you know it will fit into.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    you could use something like this and modify it to work for you
    Code:
    int main(){
    char farray[200];
    int x;
    FILE *fp;
    fp = fopen("c:\\t3.txt","r");
    x=0;
    if(fp==NULL) printf("NULL);
              while(fgetc(fp) != EOF){
               
              farray[x]= fgetc(fp);
              printf("%c",farray[x]);
              x++;
    }
    
    getchar();
    }
    Last edited by KoG Metalgod; 04-10-2007 at 06:22 AM.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 04:29 AM
  2. Does realloc save the previous buffer data?
    By Niara in forum C Programming
    Replies: 6
    Last Post: 07-23-2008, 04:40 AM
  3. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Extracting data from an array in C
    By Sparkle1984 in forum C Programming
    Replies: 5
    Last Post: 10-07-2003, 03:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21