passing ** value to fscanf

This is a discussion on passing ** value to fscanf within the C++ Programming forums, part of the General Programming Boards category; i'm reading data from a file and putting it into a dynamic multidimensional array (pointer to an array of pointers ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    2

    passing ** value to fscanf

    i'm reading data from a file and putting it into a dynamic multidimensional array (pointer to an array of pointers to arrays). however i dont know how to pass the correct value to fscanf. any suggestions?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    int** p;
    int l, m;
    FILE * fp = fopen("data.txt","r");
    
    // Make a 5 x 5 dynamic array
    
    p = new int*[5];
    for( l = 0; l < 5; ++l )
        p[l] = new int[5];
    
    // Read data into dynamic array.
    
    for( l = 0; l < 5; ++l )
        for( m = 0; m < 5; ++m )
            fscanf(fp,"%d",&p[l][m]);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    2
    hmm, debug assertion error
    debug asks me to find fseek.c

    wouldnt that code give the memory address of the pointer in the subarray of pointers?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    It works for me, I created a test file "data.txt" containing 25 numbers as such:
    Code:
    24 12 6 3 1
    14 12 0 14 9
    9 1 2 5 6
    0 0 4 5 2
    5 5 5 5 5
    Did you create a file to test with? You should also add code to check that the file has been opened properly. After reading in the data I was able to successfully print out all the correct values using another double for loop.

    Quote Originally Posted by e_zealot
    wouldnt that code give the memory address of the pointer in the subarray of pointers?
    &p[l][m] gives the memory address of the individual elements of the subarray. Specifically, the address of element m in subarray l.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > debug asks me to find fseek.c
    Your code is broken somewhere in it's file handling, it's nothing to do with the way you allocated memory (if you did as already suggested by hk_mp5kpdw.

    I suggest you post what you tried in its entirety.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 12:08 PM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

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