I/O

This is a discussion on I/O within the C Programming forums, part of the General Programming Boards category; how can I read a data file to an array: Have tried using fread() but doesnt work Code: int *fp; ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    Unhappy I/O

    how can I read a data file to an array:

    Have tried using fread() but doesnt work
    Code:
    int *fp;
    fp=&array;
    
    if ((input=fopen("data.txt","r"))==NULL)
    {
    error msg;
    }
    fseek(input,0L,SEEK_END);
    lFileLen=ftell(input);
    rewind(input);
    
    fread(fp, lFileLen,1,input);

  2. #2
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    It could be that your file isn't the same size as your array...

    Try
    Code:
    int done;
    done = fread(fp, sizeof(array),1,input);
    The done variable should equal 1 after the fread if successful, because it is reading 1 array 'block'.
    Of course, this will only work if you have outputted the whole array to the file using fwrite.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > if ((input=fopen("data.txt","r"))==NULL)
    When you load data.txt file into your editor, do you see

    - a mess of hexadecimal bytes and lots of \0 ?
    In which case fread is appropriate.

    - a nice easy to read file containing say
    1 2 3 4
    10 20
    30 40
    In which case you should be using fgets + sscanf (or fgets + strtol).
    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.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    got it working

    thanks all

    worked it out;
    Code:
    int main()
    {
    FILE *file;
    char string[50000];
    char *array[50000];
    int i,j;
    file = fopen("data.txt","r");
    if(file==NULL)
    perror("Error opening file");
    else{
    fgets(string,50000,file);
    }
    array[0]=strtok(string, " \n\t");
    for(i=1;i<50000;i++){
    array[i]=strtok(NULL, " \n\t");
    if(array[i]==NULL)
    break;
    }
    for(j=0;j<50000;j++)
    {
    if(array[j]==NULL)
    break;
    printf("%s\n",array[j];
    }
    fclose(file);
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Since you're breaking out of the loop at the first occurrence of NULL, you might as well have stopped doing the strtok() at that point too... saving a lot of work. Instead of blindly going for a full 50000.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System I/O
    By javster87 in forum C Programming
    Replies: 2
    Last Post: 04-15-2010, 04:47 AM
  2. Replies: 8
    Last Post: 03-23-2010, 12:23 AM
  3. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 10:06 AM
  4. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 04:14 PM

Tags for this Thread


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